奇怪的mysql-ins

2024-06-16 13:39:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将dict值插入数据库。但我有个错误。here is my dictionary。我用的是这个密码。你知道吗

       `cols = filter_dict.keys()
        vals = filter_dict.values()
        ("INSERT INTO table (%s) VALUES (%s)", [cols, vals])
`

当我只打印这个查询时("INSERT INTO table (%s) VALUES (%s)", [cols, vals])。 我得到以下输出。 ('INSERT INTO table (%s) VALUES (%s)', [['cinematography', 'name', 'producer', 'caption', 'studio', 'editing'], ['Venkat Prasad', '100% Love', 'Bunny Vasu', '', '100% Love Poster.jpg', 'Chandra Sekhar T Ramesh,Hari Prasad']])

但是当我执行这个查询时,我得到了奇怪的输出。 the manual that corresponds to your MySQL server version for the right syntax to use near \'table (("\'cinematography\'", "\'name\

为什么要执行向每列添加“\”的查询??有人能帮我吗?谢谢


Tags: thetoname数据库tablefilterdictinsert
1条回答
网友
1楼 · 发布于 2024-06-16 13:39:03

反斜杠可能是由于调试器造成的。您是否在交互式提示中运行代码?你知道吗

无论如何,实际的问题是:

  • tablereserved word。你应该把table放在反记号里。你知道吗
  • 如果表包含多个列,则需要有多个%s和多个值。你知道吗

例如:

"INSERT INTO `table` (%s, %s, &s) VALUES (%s, %s, %s)"

您还需要更改(cols, vals)以列出各个值。你知道吗

(cols[0], cols[1], cols[2] , vals[0], vals[1], vals[2])

我也强烈建议你为你的桌子找一个更好的名字,最好是:

1)描述表中包含的数据类型,以及

2)不是保留字

相关问题 更多 >