小编kuk*_*cin的帖子

如何使用 Python 在 MySQL 中插入列表作为列?

所以基本上我有这个列表:

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)

python mysql sql list python-2.7

2
推荐指数
1
解决办法
3031
查看次数

标签 统计

list ×1

mysql ×1

python ×1

python-2.7 ×1

sql ×1