如何使用sqlite3 python从JSON插入值,同时检查值是否已经存在?
我的目标是从JSON插入值,这已经有效。但是,如何检查我从JSON中插入的值是否已经在我的桌子中?
不幸的是,当前代码不起作用。错误是: sqlite.3.operationalerror:附近“ where”:语法错误
我的代码:
def add_authors_to_table(self, authors):
self.cursor.executemany('''
INSERT OR IGNORE INTO author (AuthorID, FirstName, LastName)
VALUES (:Author_ID, :FirstName, :LastName)
WHERE NOT EXISTS (SELECT AuthorID FROM author WHERE AuthorID =
authors[Author_ID])''', authors)
self.db.saveChanges()
return 200
My goal is to insert values from a JSON, this already works. But how can I check if the values I insert from my JSON are already in my table?
Unfortunately, the current code does not work. The error is:sqlite3.OperationalError: near "WHERE": syntax error
My Code:
def add_authors_to_table(self, authors):
self.cursor.executemany('''
INSERT OR IGNORE INTO author (AuthorID, FirstName, LastName)
VALUES (:Author_ID, :FirstName, :LastName)
WHERE NOT EXISTS (SELECT AuthorID FROM author WHERE AuthorID =
authors[Author_ID])''', authors)
self.db.saveChanges()
return 200
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论