在Sqlite3语句中使用变量的问题

2 投票
1 回答
1020 浏览
提问于 2025-04-16 20:54

我在一个Python 2.7的项目中遇到了问题。

我想把一个变量设置为从sqlite3数据库中获取的值,但遇到了一些麻烦。下面是我目前的代码和我收到的错误信息。是的,连接打开得很好,表格、列和指定的行也都存在,没问题。

import sqlite3 import Trailcrest

conn = sqlite3.connect('roster.paw')
c = conn.cursor()

def Lantern(AI):
    """Pulls all of the data for the selected user."""
    Trailcrest.FireAutoHelp = c.execute("""select fireautohelp 
                                             from roster 
                                            where index = ?;""", (AI,) ).fetchall()

错误信息是:

> Traceback (most recent call last):   
> File "<pyshell#4>", line 1, in <module> Lantern(1)
> File "C:\Users\user\MousePaw Games\Word4Word\PYM\Glyph.py", line 20, 
> in Lantern
>     Trailcrest.FireAutoHelp = c.execute("""select fireautohelp from roster where index = ?;""", (AI,)).fetchall()
> OperationalError: near "index": syntax error

1 个回答

6

正如Thomas K在评论中提到的,index是一个SQL的关键字。
你可以选择重命名这个列,或者用反引号把它包起来:

Trailcrest.FireAutoHelp = c.execute("""select fireautohelp 
                                         from roster 
                                        where `index` = ?;""", (AI,) ).fetchall()

撰写回答