【发布时间】:2021-02-10 00:52:57
【问题描述】:
使用索引访问数据库时弹出错误。
我认为 'index' 在 sql 查询 'WHERE index =?' 中不可用
如何使用索引列更新数据库?
sqlite3.OperationalError: near "index": syntax error
import pandas as pd
import sqlite3
# Create DB file
con = sqlite3.connect("./tester.db")
data = {'date': ['20210210', '20210209'], 'value': [15, 17]}
df_add = pd.DataFrame(data, columns=['value'], index=data['date'])
df_add.to_sql('placeA', con, if_exists='replace')
con.close()
# Update DB using index
con = sqlite3.connect("./tester.db")
cur = con.cursor()
cur.execute("UPDATE placeA SET value = ? WHERE index = ?", (232, "20210210"))
con.commit()
cur.execute("SELECT * FROM placeA")
f = cur.fetchall()
print(f)
【问题讨论】:
-
我想这可能是使用“cur.execute("ALTER TABLE placeA RENAME COLUMN 'index' to date")”更改索引列名称的解决方案,但我仍然不确定为什么'index ' 不起作用。
标签: python sqlite indexing where-clause using