Python操作Mysql实例

eg:
#coding:utf=8
import MySQLdb
conn=MySQLdb.Connect(host='localhost',port=3306,user='root',passwd='1997huansql',db='school',charset='utf8')
cursor = conn.cursor()
sql = 'select * from students'
sql_insert="insert into students(name,age) values('Lincose',10)"
sql_update="update students set id=1 where name='Lincose'"
sql_delete="delete from students where age=10"
try:
cursor.execute(sql_insert)#增
cursor.execute(sql_update)#改
cursor.execute(sql_delete)#删
cursor.execute(sql)#查
conn.commit()#提交事务
rs = cursor.fetchall()
print rs
except Exception as e:
print e conn.rollback()#回滚事务
cursor.close()conn.close()

你可能感兴趣的:(Python操作Mysql实例)