update SQLite database using insert

3 次查看(过去 30 天)
Charles
Charles 2017-9-18
编辑: Guillaume 2017-9-22
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?

回答(1 个)

Han Du
Han Du 2017-9-22
Duplicates can be avoid by setting Dates to UNIQUE in your query to create the table.
  2 个评论
Charles
Charles 2017-9-22
Csn you provide an example as I am new to SQL
Guillaume
Guillaume 2017-9-22
编辑:Guillaume 2017-9-22
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by