# coding=utf-
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 = (, ' xiaoming ',, ' 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=)
Cur=conn.cursor ()
#数量
Count (cur)
#查询
Select (cur)
#插入
Insert (cur)
Print "After Insert"
#查询
Select (cur)
#更新
Update (cur)
print "After Update"
#查询
Select (cur)
#删除
Delete (cur)
print "After delete"
#查询
Select (cur)
Cur.close ()
Conn.close ()
Except Mysqldb.error,e:
Print "Mysql Error%d:%s"% (e.args[], e.args[])
- This article is from: Linux Tutorial Network
Python operation MySQL