Python sqlite3 string variable in execute

后端 未结 3 1222
小鲜肉
小鲜肉 2020-11-27 18:18

I try to execute this sqlite3 query in Python. I reduced the code to the minimum, sqlite.connect, etc works.

column = \'Pron_1_Pers_Sg\'
goal = \'gender\' 
c         


        
3条回答
  •  盖世英雄少女心
    2020-11-27 18:41

    Parameter markers can be used only for expressions, i.e., values. You cannot use them for identifiers like table and column names.

    Use this:

    cur.execute("SELECT "+column+" FROM Data where "+goal+"=?", (constrain,))
    

    or this:

    cur.execute("SELECT %s FROM Data where %s=?" % (column, goal), (constrain,))
    

    (And don't commit before you have actually finished accessing the data.)

提交回复
热议问题