0

I want to insert into a table as long as that entry doesn't already exist. This is my line of code:

cursor.execute("INSERT INTO author (name) VALUES(?) SELECT * FROM author WHERE NOT EXISTS (SELECT * FROM author)", (aname, ))

Also, I'm not sure if this query is correct since I haven't had the opportunity to test it, reason being I keep getting a syntax error.

aperez121
  • 33
  • 4

1 Answers1

1

I don't use Python, but that definitely looks like invalid SQL syntax. When supplying values for your INSERT, you can use either "VALUES(...)" or "SELECT ...", but not both. I'm not quite sure from your description what you intend, but if the name field in the author table is meant to be unique, then you should specify that in your schema. e.g.,

create table author(name text unique, ...)

The you can use something like:

cursor.execute("INSERT OR IGNORE INTO author(name) VALUES(?)", aname)

(Not sure about the Python syntax, since I don't use Python.)

varro
  • 2,382
  • 2
  • 16
  • 24