我尝试在 Python中执行此sqlite3查询.我将代码减少到最小,sqlite.connect等工作. column = 'Pron_1_Pers_Sg'goal = 'gender' constrain = 'Mann'with con: cur = con.cursor() cur.execute("SELECT ? FROM Data where ?=?", (column, g
column = 'Pron_1_Pers_Sg' goal = 'gender' constrain = 'Mann' with con: cur = con.cursor() cur.execute("SELECT ? FROM Data where ?=?", (column, goal, constrain)) con.commit() rows = cur.fetchall() for element in rows: values.append(element)
这将返回一个空列表.
如果我对字符串进行硬编码,它会起作用并返回值.
您不能将它们用于表格和列名称等标识符.
用这个:
cur.execute("SELECT "+column+" FROM Data where "+goal+"=?", (constrain,))
或这个:
cur.execute("SELECT %s FROM Data where %s=?" % (column, goal), (constrain,))
(并且在实际访问数据之前不要提交.)