python下mysql连接并使用

2017-10-13  本文已影响0人  不懒狮Blaise

首先得安装支持python的mysql驱动,
目前有2个mysql驱动可以选择:
$ easy_install mysql-connector-python
$ easy_install MySQL-python
下面会使用MySQL-python驱动(包名很难记)

import MySQLdb
conn= MySQLdb.connect(
        host='localhost',
        port = 3306,
        user='root',
        passwd='123456',
        db ='test',
        )
# 取连接的游标
cur = conn.cursor()

#创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10)), PRIMARY KEY (id)")

#插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

#修改查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'")
#删除查询条件的数据
#cur.execute("delete from student where age='9'")

#关闭游标
cur.close()
# 连接提交操作
conn.commit()
#关闭连接
conn.close()

参考地址:http://www.cnblogs.com/fnng/p/3565912.html

上一篇 下一篇

猜你喜欢

热点阅读