import MySQLdb
conn = MySQLdb.connect(host='192.168.1.21',user='yangqw',passwd='1',db='free')
cur = conn.cursor()
# sql = 'insert into student(name,age) values(%s,%s)'
# params = ('alex',21)
# reCount = cur.execute(sql,params)
# conn.commit() # 增,改,删,都要commit,查,不需要
# sql1 = 'select * from student'
# reCount1 = cur.execute(sql1)
# data = cur.fetchall()
# print data
sql2='delete from student where id_1= %s'
params2=(1)
reCount2 = cur.execute(sql2,params2)
conn.commit()
sql1 = 'select * from student'
reCount1 = cur.execute(sql1)
data = cur.fetchall()
print data
# print reCount1
cur.close()
conn.close()
=========================
# 一次批量插入多条
import MySQLdb
conn = MySQLdb.connect(host='192.168.1.21',user='yangqw',passwd='1',db='free')
cur = conn.cursor()
li =[
('alex',22),
('sb',33),
]
reCount = cur.executemany('insert into student(name,age) values(%s,%s)',li)
conn.commit()
cur.close()
conn.close()
print reCount
=========================
带列名
cur = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)