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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容