微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python操作MySQL

下面是编程之家 jb51.cc 通过网络收集整理的代码片段。

编程之家小编现在分享给大家,也给大家做个参考。

# 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)"
   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])

以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐