时间:2021-07-01 10:21:17 帮助过:10人阅读
2、Python连接MYSQL
import pymysql db = pymysql.connect(host=‘localhost‘,user=‘root‘, password=‘123‘, port=3306, db=‘aaa‘) data = { ‘username‘: ‘abc333d‘, ‘password‘: ‘123456‘, ‘age‘: 21, ‘height‘:176 } table = ‘t_user‘ keys = ‘, ‘.join(data.keys()) values = ‘, ‘.join([‘%s‘] * len(data)) sql = ‘INSERT INTO {table}({keys}) VALUES ({values})‘.format(table=table, keys=keys, values=values) try: cursor = db.cursor() cursor.execute(sql, tuple(data.values())) print(‘Successful‘) db.commit() except: print(‘Failed‘) db.rollback() cursor.close() db.close()
Python连接SQLServer2000或连接mysql代码示例
标签:com lse oca tuple fail cut mysq server 游标