python mysql增删改查

来源:互联网 发布:windows更改用户 编辑:程序博客网 时间:2024/06/07 14:17
#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()
0 0
原创粉丝点击