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

时间:2011-07-05 23:51:13

标签: python sqlite

我遇到了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 个答案:

答案 0 :(得分:6)

正如Thomas K在评论中提到的,index是一个SQL关键字 您可以重命名该列,也可以用反引号括起来:

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