If the table already exists, no action will be taken. We call it twice to get the alter table command to fail but the program succeeds because we remove the alter table command from the sql and re-execute the updated code. INSERT INTO PaymentInformation(NAME, Start, End) VALUES('Tina','','') WHERE NOT EXISTS ( SELECT FROM PaymentInformation WHERE name 'Tina' AND durationstart '' ) Is this how it should be done It shouldn't insert if Tina is already in the table with a Start day of. The IF NOT EXISTS syntax will only proceed with the creation of the table if it does not already exist. We iterate the array executing each script. Heres some example code where we have sql scripts in an array. The idea is to execute the sql if it exceptions on an alter table call we can find the alter table line in the sql and return the remaining lines and execute until it either succeeds or no more matching alter table lines can be found. Hi, When I use INSERT OR IGNORE, if insertion fail (record exists), then sqlite3lastinsertrowid does return nothing. Luckily the error message gives us all the information we need to do this. Note that if the ID field is set to autoincrement, then the IDs. So the idea of catching an exception is acceptable and then moving on. You can do this with a unique constraint & insert or ignore. Typically you are not going to run this in realtime or often. However, the original question was to input a sql script that would be executed by a single db execute and all the solutions ( like checking to see if the column is there ahead of time ) would require the executing program either have knowledge of what tables and columns are being altered/added or do pre-processing and parsing of the input script to determine this information. to INSERT rows after determining whether that row is, in fact, new or already exists. All these answers are fine if you execute one line at a time. Learn how to INSERT an If Row Does Not Exist (UPSERT) in MySQL.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |