#coding=utf-8
import MySQLdb #加载mysql模块
conn=MySQLdb.connect(host='localhost',port=3306,user='root',passwd='fabregas4',db='test') #连接数据库
cur=conn.cursor() #得到数据库游标
cur.execute("create table student(id int,name varchar(20),class varchar(30),age varchar(10))")#创建一个表
conn.commit() #提交后数据库才会发生变化
cur.execute("insert into student values(2,'tom','3 year 2 class','9')")#插入一条数据
conn.commit()
aa=cur.execute("select * from student") #查询语句
info=cur.fetchmany(aa)
for i in info:
print i
cur.execute("update student set class='3 year 1 class' where name='tom'") #修改数据
conn.commit()
cur.execute("delete from student where age='9'") #删除数据
conn.commit()
cur.close() #关闭连接
conn.close()
import MySQLdb #加载mysql模块
conn=MySQLdb.connect(host='localhost',port=3306,user='root',passwd='fabregas4',db='test') #连接数据库
cur=conn.cursor() #得到数据库游标
cur.execute("create table student(id int,name varchar(20),class varchar(30),age varchar(10))")#创建一个表
conn.commit() #提交后数据库才会发生变化
cur.execute("insert into student values(2,'tom','3 year 2 class','9')")#插入一条数据
conn.commit()
aa=cur.execute("select * from student") #查询语句
info=cur.fetchmany(aa)
for i in info:
print i
cur.execute("update student set class='3 year 1 class' where name='tom'") #修改数据
conn.commit()
cur.execute("delete from student where age='9'") #删除数据
conn.commit()
cur.close() #关闭连接
conn.close()