python - 蟒蛇 MySQL。如何从字典中插入重复键更新
问题描述
我正在从 Python 字典更新 MySQL 数据库表,其中键是数据库字段。我有一个有效的插入语句,但我真正需要的是 INSERT ON DUPLICATE KEY UPDATE。这是插入语句:
for d in r.json()['mydict'].items():
d = d[1] #the dictionary is the 2nd element in the tuple
placeholders = ', '.join(['%s'] * len(d))
columns = ', '.join(d.keys())
sql = "INSERT INTO %s ( %s ) VALUES ( %s )" % ("my_table", columns, placeholders)
c = create_connection() #from function create tables
cur = c[0] #function returns cursor
db = c[1] #function returns db
cur.execute(sql, list(d.values()))
db.commit()
db.close()
一如既往,任何帮助表示赞赏。
解决方案
推荐阅读
- css - 为什么滚动时这个固定的标题会移动?
- outlook-addin - 打包使用 Yeoman 创建的 Outlook 加载项
- hyperledger - 调用 Composer 休息服务器时跨域请求被阻止
- java - Spring - server.connection-timeout 不起作用
- php - 无法在 PhpStorm 中使用 Symfony 安装作曲家
- java - 错误:验证失败,退出主清单和属性 uses-feature#20000@glEsVersion 在 AndroidManifest.xml
- python - 使用 GPU 加速和 cuda 进行 fft 的最佳方法
- c# - SSIS WebClient() 下载文件脚本任务问题
- django - 如何创建在时间表中实施课程的 django 模型?
- r - 使用“officer”包插入外部文档