【发布时间】:2013-07-22 02:07:31
【问题描述】:
有一个创建 sqlite 数据库的 python 脚本:
import sqlite3
con = sqlite3.connect('some.db',detect_types=sqlite3.PARSE_DECLTYPES)
with con:
cur = con.cursor()
cur.execute("CREATE TABLE Title(ID INT, ...)")
然后用列表中的内容填充数据库:
<ID> = <whatever>
<var1> = <you get the point>
altogether = (ID, var1...)
cur.execute("INSERT INTO Title VALUES(?,?,...)", altogether)
con.commit()
我现在想编写此脚本的修改版本,它采用现有的 some.db 并对其进行更新,但仅当 ID 不存在于数据库中时才会这样做。
我该怎么做:
打开
some.db以进行读/写以仅更新值查询数据库以确保我当前正在迭代的
item中的ID不存在于数据库中?我目前填充变量的方式是对外部服务进行 API 调用,如果条目已经在我的数据库中,我希望能够跳过该服务。
【问题讨论】:
-
我想你可能想要“INSERT IF NOT EXIST”
标签: python sql python-2.7 sqlite