kuk*_*cin 2 python mysql sql list python-2.7
所以基本上我有这个列表:
L1 = ['hel-lo', 'world123', 'bye', 'python']
Run Code Online (Sandbox Code Playgroud)
我想将此列表插入到我的 MySQL 表中,如下所示:
+-------------------------+
| words |
+-------------------------+
| hel-lo |
| world123 |
| bye |
| python |
+-------------------------+
Run Code Online (Sandbox Code Playgroud)
实际上,我的列表由大约 1000 个元素组成。我尝试了很多我在 StackOverflow 上找到的东西,但没有任何效果。(似乎元素正试图以这种格式“['string']”插入)。
我试过的最后一个解决方案:
values = [list([item]) for item in L1]
cursor.executemany(u"INSERT INTO `table`(`data`) VALUES %s", values)
Run Code Online (Sandbox Code Playgroud)
返回此错误:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''hel-lo')' at line 1
Run Code Online (Sandbox Code Playgroud)
欢迎任何建议!
您在查询中的值列表周围缺少括号(应该是VALUES (%s)
)。此外,list([item])
可以简化为[item]
:
L1 = ['hel-lo', 'world', 'bye', 'python']
values = [[item] for item in L1]
cursor.executemany(u"INSERT INTO `instability`(`ap_name`) VALUES (%s)", values)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3031 次 |
最近记录: |