python3操作mysql数据库增删改查
- import pymysql
- import types
-
- db=pymysql.connect("localhost","root","123456","python");
-
- cursor=db.cursor()
-
- cursor.execute("drop table if exists user")
- sql=
-
- cursor.execute(sql)
-
-
- sql=
-
- try:
-
- cursor.execute(sql)
-
- db.commit()
- except:
-
- db.rollback()
-
-
- id=1
- sql="update user set age=100 where id='%s'" % (id)
- try:
- cursor.execute(sql)
- db.commit()
- except:
- db.rollback()
-
- id=2
- sql="delete from user where id='%s'" % (id)
- try:
- cursor.execute(sql)
- db.commit()
- except:
- db.rollback()
-
-
- cursor.execute("select * from user")
-
- results=cursor.fetchall()
-
- for row in results:
- name=row[0]
- age=row[1]
-
-
- print ("name=%s,age=%s" %
- (age, name))
原文地址:https://www.cnblogs.com/meng-wei-zhi/p/8214750.html