无法将变量添加到表

Lutty2029

因此,我正在尝试将变量(字符串)添加到另一个变量(表)中。我的代码如下所示:

tableName = '123456789'
testVariable = 'test'
c.execute('INSERT INTO ' + tableName + ' (testColumn) VALUES (' + testVariable + ')')
conn.commit()

但是由于某种原因,这给了我这个错误

    c.execute('INSERT INTO ' + tableName + ' (testColumn) VALUES (' + testVariable + ')')
sqlite3.OperationalError: near "123456789": syntax error

我该怎么办?

Lutty2029

好的,我设法自己修复了。我必须在变量的开头和结尾添加单引号,然后使用以下命令:c.execute("INSERT INTO {} (testColumn) VALUES ({})".format(tableName, testVariable))

感谢您的回答!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章