gpt4 book ai didi

python - SQLite Python 中的变量替换

转载 作者:行者123 更新时间:2023-12-03 00:59:31 25 4
gpt4 key购买 nike

我有以下Python代码,当我执行它时,它给了我错误,错误说

sqlite3.OperationalError: near "%": syntax error

statement = "INSERT INTO %s (%s) VALUES " % (table,columns) + "(%s,%s,%s);"
cur.execute(statement, (index,fullpath,filename))

最佳答案

SQL参数由数据库处理,而不是由Python处理,因此语法不一定与Python相同。

在 SQLite(以及大多数其他数据库)中,参数用 ? 标记:

statement = "INSERT INTO %s (%s) VALUES (?,?,?);" % (table,columns)
cur.execute(statement, (index,fullpath,filename))

关于python - SQLite Python 中的变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13799901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com