首页 > 解决方案 > 无法将变量添加到表中

问题描述

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

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

我该怎么办?

标签: pythonsqlsqlite

解决方案


c.execute("INSERT INTO ? (testColumn) VALUES (?)", (tableName, testVariable))

推荐阅读