mysql python

#coding=utf-8

import MySQLdb

con = MySQLdb.connect(host='127.0.0.1',user='root',passwd='2',db='nan',port=3306)
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS tableone (id int not null auto_increment primary key,name varchar(20),age int)')
try:
    cur.executemany('INSERT INTO tableone (name,age) values (%s,%s)',[('xiaoming',20),('xiaohong',21)])
    con.commit()
except:
    con.rollback() # 确保一致性
cur.execute('select id,name,age from tableone')
res = cur.fetchall()
#for mess in message:
print res
con.close()

猜你喜欢

转载自blog.csdn.net/nanxiaoting/article/details/82718224