Python3.6实现MySQL数据库的数据插入
2019-07-26 本文已影响0人
我是李小胖
import pymysql
def insert_db(sql_insert):
'''插入操作'''
db = pymysql.connect(host='47.104.x.x',
port=3306,
user='root',
passwd='123456',
db='test')
# 使用cursor()方法获取操作游标
cur = db.cursor()
try:
cur.execute(sql_insert)
# 提交
db.commit()
except Exception as e:
print("错误信息:%s" % str(e))
# 错误回滚
db.rollback()
finally:
db.close()
if __name__ == "__main__":
sql_insert = "insert into user(id, name, psw) values(10, 'yoyo_10', '123456')"
insert_db(sql_insert)