#!/usr/bin/python
# !coding=utf-8
import pymysql
db = pymysql.connect(host='192.168.1.230', user='root', password='root',db='spiders', port=3306)
cursor = db.cursor()
data = {"id": "20181203", "name": "ldh", "age": "23"}
table = "students"
keys = ','.join(data.keys())
values = ','.join(["%s"] * len(data))
sql = 'insert into {table}({keys}) values({values})'.format(table=table, keys=keys, values=values)
update = ','.join(["{key} = %s".format(key=key) for key in data])
try:
if cursor.execute(sql, tuple(data.values())):
print("Successful")
db.commit()
except:
print("Failed")
db.rollback()
db.close()
python插入mysql
最新推荐文章于 2024-10-10 09:26:11 发布