MATLAB Answers

0

update SQLite database using insert

Asked by Charles on 18 Sep 2017
Latest activity Edited by Guillaume
on 22 Sep 2017
Hello I am trying to update my SQLite database using the following command
insert(conn, 'ClosePriceTable', {'Dates','AUD_CAD', 'AUD_CHF'}, Table_New_datax(2:end,:))
However I rather want to update only if new records are different to current records. Thus I do not want duplicate records in my SQLite database. If the new records are the same as current records then I merely wish to overwrite and not insert the same record again.
The key variable differentiating records is the date.
How can I avoid duplication of records? What command can I use?

  0 Comments

Sign in to comment.

1 Answer

Answer by Han Du
on 22 Sep 2017

Duplicates can be avoid by setting Dates to UNIQUE in your query to create the table.

  2 Comments

Csn you provide an example as I am new to SQL
Guillaume
on 22 Sep 2017
See the SQLite doc
Something like:
CREATE TABLE yourtable (Dates UNIQUE ON CONFLICT REPLACE, ...)
when you create the table.
Note that the ON CONFLICT REPLACE is particular to SQLite, it is not standard SQL. Other database engines will abort the transaction instead of replacing the existing row by your new values. Therefore the other way to do what you want is to first check if your keys already exist in database and do an UPDATE for the ones that do.

Sign in to comment.