Python sqlite - insert if not exists

时间:2018-04-18 18:02:05

标签: python sqlite

I have a database table place like below:

command_create_table = """CREATE TABLE if not exists place (
name VARCHAR(100),
lat DOUBLE(100, 10),
lng DOUBLE(100,10),
vicinity VARCHAR(100),
typeOfPlace VARCHAR(100));""

I want to insert into this table only if certain entry doesn't exist. But there is no such command like below.

cursor.execute("INSERT if not exists into place (name, lat, lng, vicinity, typeOfPlace) values (?, ?, ?, ?, ?)",(name, lat, lng, vicinity, typeOfPlace))

I tried making name as PRIMARY KEY in the place table and using command

cursor.execute("INSERT into place (name, lat, lng, vicinity, typeOfPlace) values (?, ?, ?, ?, ?)",(name, lat, lng, vicinity, typeOfPlace))

But getting error sqlite3.IntegrityError: UNIQUE constraint failed: place.name

What am I missing? Any suggestion?

0 个答案:

没有答案
相关问题