Python操作MySQL
2018-07-20 来源:open-open
安装MySQL
安装MySQL不用多说了,下载下来安装就是,没有特别需要注意的地方。
# coding=utf-8 import MySQLdb #查询数量 def Count(cur): count=cur.execute('select * from Student') print 'there has %s rows record' % count #插入 def Insert(cur): sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)" param = (2,'xiaoming',24,'boy') cur.execute(sql,param) #查询 def Select(cur): n = cur.execute("select * from Student") print "------" for row in cur.fetchall(): for r in row: print r print "------" #更新 def Update(cur): sql = "update Student set Name = %s where ID = 2" param = ("xiaoxue") count = cur.execute(sql,param) #删除 def Delete(cur): sql = "delete from Student where Name = %s" param =("xiaoxue") n = cur.execute(sql,param) try: conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='python',port=3306) cur=conn.cursor() #数量 Count(cur) #查询 Select(cur) #插入 Insert(cur) print "插入之后" #查询 Select(cur) #更新 Update(cur) print "更新之后" #查询 Select(cur) #删除 Delete(cur) print "删除之后" #查询 Select(cur) cur.close() conn.close() except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])
标签: Mysql
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐