我一直收到一个OperationalError:无法识别的令牌。当我试图使用SQLite insert命令将数据插入到SQLite数据库中时,出现了这个错误。我需要做什么来纠正这个错误,或者有没有更好的方法来将数据插入到数据库中?这些数据是以海图基准以上米为单位测量的水位数据,并从加拿大和美国的五大湖地区的水位计数据记录器中收集。该脚本使用Pandas库,并被硬编码为合并来自彼此非常接近的水位测量站的数据。我想使用insert命令,以便在向数据库添加将来的数据时可以处理重叠数据。我甚至不会假装我知道我正在谈论的数据库和编程,所以如果能帮助我解决这个错误,我将不胜感激!
我试着修改参数化查询中的脚本,试图解决这个问题,但没有任何运气,因为我的研究表明,这可能是罪魁祸首
# Tecumseh. Merges station in steps due to inability of operation to merge all stations at once. Starts by merging PCWL station to hydromet station followed by remaining PCWL station and 3 minute time series
final11975 = pd.merge(hydrometDF["Station11975"], pcwlDF["station11995"], how='outer', left_index=True,right_index=True)
final11975 = pd.merge(final11975, pcwlDF["station11965"], how='outer', left_index=True,right_index=True)
final11975 = pd.merge(final11975, cts, how='outer', left_index=True,right_index=True)
final11975.to_excel("C:/Users/Andrew/Documents/CHS/SeasonalGaugeAnalysis_v2/SeasonalGaugeAnalysis/Output/11975_Tecumseh.xlsx")
print "-------------------------------"
print "11975 - Tecumseh"
print(final11975.info())
final11975.index = final11975.index.astype(str)
#final11975.to_sql('11975_Tecumseh', conn, if_exists='replace', index=True)
#Insert and Ignore data into database to eliminate overlaps
testvalues = (final11975.index, final11975.iloc[:,0], final11975.iloc[:,1], final11975.iloc[:,2])
c.execute("INSERT OR IGNORE INTO 11975_Tecumseh(index,11975_VegaRadar(m),11995.11965), testvalues")
conn.commit()我希望使用insert和Ignore命令将数据插入到数据库中,因为数据在下载时通常是重叠的。我刚接触数据库,但我的印象是Insert和Ignore命令会删除重叠的数据。我在运行脚本时收到的消息是:
</> <Exception has occurred: OperationalError
unrecognized token: "11975_Tecumseh"
File "C:\Users\Documents\CHS\SeasonalGaugeAnalysis_v2\SeasonalGaugeAnalysis\Script\CombineStations.py", line 43, in <module>>
c.execute("INSERT OR IGNORE INTO 11975_Tecumseh(index,11975_VegaRadar(m),11995.11965), testvalues") </>发布于 2019-07-13 16:48:52
根据SQL标准,您可以创建诸如"11975_Tecumseh“和Tecumseh_11975之类的表名或列名,但不能在不使用双引号的情况下创建以数字开头的表名或列名。
c.execute("INSERT OR IGNORE INTO '11975_Tecumseh'(index,'11975_VegaRadar(m)',11995.11965), testvalues")发布于 2019-07-13 17:58:43
您得到的错误是因为表名11975_Tecumseh是无效的,因为它没有被适当地封闭。
如果要使用关键字作为名称,则需要对其进行引号。在SQLite中有四种引用关键字的方法:
关键字‘
标准SQL。MS Access和SQL Server使用此报价机制,并将其包含在SQLite中以实现兼容性。
keywordA
用重音(ASCII码96)括起来的
如果在允许标识符但不允许字符串文字的上下文中使用单引号中的关键字(例如:'key‘或'glob'),则将令牌理解为标识符而不是字符串文字。
如果在不能解析为标识符但允许字符串文字的上下文中使用双引号中的关键字(例如:"key“或"glob"),则该标记被理解为字符串文字而不是标识符。
提醒程序员不要使用前面项目中描述的两个异常。我们强调,它们的存在只是为了让旧的和格式错误的SQL语句能够正确运行。SQLite的未来版本可能会引发错误,而不是接受上述异常所涵盖的格式错误的语句。
SQL As Understood By SQLite - SQLite Keywords
中包含非数字的名称
如果11975_Tecumseh是实际的表名称,则必须将其括起来,例如11975_Tecumseh
与此类似,列
也必须适当地封闭。
如果这样做,你最终会得到
"INSERT OR IGNORE INTO [11975_Tecumseh]([index],[11975_VegaRadar(m)],[11995.11965]), testvalues"问题在于,testvalues在语法上是错误的。在要插入的列之后,即([index],[11975_VegaRadar(m)],[11995.11965]),应该使用带有三个值的关键字值。
有效语句的示例如下:
"INSERT INTO [11975_Tecumseh] ([index],[11975_VegaRadar(m)],[11995.11965]) VALUES('value1','value2','value3')"正因如此
c.execute("INSERT INTO [11975_Tecumseh] ([index],[11975_VegaRadar(m)],[11995.11965]) VALUES('value1','value2','value3')")将插入新行(除非发生约束冲突,否则为)
然而,我怀疑你想要根据变量插入值,在这种情况下,你可以使用:
"INSERT INTO [11975_Tecumseh] ([index],[11975_VegaRadar(m)],[11995.11965]) VALUES(?,?,?)"的问号是占位符/绑定值
然后使用以下命令调用上面的代码:
c.execute("INSERT INTO [11975_Tecumseh] ([index],[11975_VegaRadar(m)],[11995.11965]) VALUES(?,?,?)",testvalues);#工作示例:
import sqlite3
drop_sql = "DROP TABLE IF EXISTS [11975_Tecumseh]"
crt_sql = "CREATE TABLE IF NOT EXISTS [11975_Tecumseh] ([index],[11975_VegaRadar(m)],[11995.11965])"
testvalues = ("X","Y","Z")
c = sqlite3.connect("test.db")
c.execute(drop_sql)
c.execute(crt_sql)
insert_sql1 = "INSERT INTO [11975_Tecumseh] " \
"([index],[11975_VegaRadar(m)],[11995.11965]) " \
"VALUES('value1','value2','value3')"
c.execute(insert_sql1)
insert_sql2 = "INSERT OR IGNORE INTO '11975_Tecumseh'" \
"('index','11975_VegaRadar(m)',[11995.11965])" \
" VALUES(?,?,?)"
c.execute(insert_sql2,(testvalues))
cursor = c.cursor()
cursor.execute("SELECT * FROM [11975_Tecumseh]")
for row in cursor:
print(row[0], "\n" + row[1], "\n" + row[2])
c.commit()
cursor.close()
c.close()#结果
##Row 1
value1 value2 value3
##Row 2
X Y Z
https://stackoverflow.com/questions/57017469
复制相似问题