# coding=utf-html
import MySQLdbpython
#查詢數量 def Count(cur):linux
count=cur.execute( 'select * from Student' )sql
print 'there has %s rows record' % countide
#插入 def Insert(cur):fetch
sql = "insert into Student(ID,Name,Age,Sex)values(%s,%s,%s,%s)"spa
param = ( , 'xiaoming' , , 'boy' )orm
cur.execute(sql,param)htm
#查詢 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= )
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[ ], e.args[ ])
本文來自:Linux教程網