Python pydobc SQL ODBC Microsoft Access Drive参数太少

时间:2016-12-12 05:13:36

标签: sql python-2.7 ms-access pyodbc

尝试获取匹配TableA和TableB的筛选结果,其中列col1和WHERE都是python变量“Bingo”not in TableB col3

 bingo = 'Data1'
 SQL = 'SELECT a.* FROM TABLEA a WHERE a.col1 NOT IN (SELECT col1 FROM TABLEB) AND (?) NOT IN (SELECT col3 FROM TABLEB);'
 rows = cur.execute(SQL,bingo).fetchall()

1 个答案:

答案 0 :(得分:2)

您无法参数化该字段的名称。最好使用Python str.format()替换/格式化,例如:

bingo = 'Data1'
SQL = 'SELECT a.* FROM TABLEA a WHERE a.col1 NOT IN (SELECT col1 FROM TABLEB) AND {} NOT IN (SELECT col3 FROM TABLEB);'.format(bingo)
rows = cur.execute(SQL).fetchall()