Hello, community members, I ran into the problem that I need to execute a sql query, like this: (python 3.5, sqlite)

cur = db.cursor() cur.execute("CREATE TABLE contact_info(ID INTEGER PRIMARY KEY autoincrement," firstname VARCHAR(60), lastname VARCHAR(60), middlename VARCHAR(60)," "UNIQUE (firstname, lastname, middlename) ON CONFLICT REPLACE);") cur.execute("INSERT OR REPLACE INTO contact_info VALUES" "((SELECT ID FROM contact_info WHERE firstname=? AND" "lastname=? AND middlename=?), ?, ?, ?);", [fname, lname, mname, fname, lname, mname]) 

After that, I need to know whether INSERT or REPLACE was executed, but I do not understand how this can be done without writing additional requests. And I would like to understand how the most correct thing is to find out?

  • And that module sql does not swear at impossibility to execute request? Usually, after all, an exception is thrown ... - gil9red
  • not quite the right answer, but still. you can simply put a boolean flag and set it to True when the condition is met, and then just do a check - A. Shorokhov

1 answer 1

For example :

 SELECT last_insert_rowid() 

Without additional requests will not work, unfortunately. In PostgreSQL, for example, there is RETURNING <id_name> , which returns all modified IDs.