当前位置:网站首页>SQLite one line SQL implementation updates if there is one, inserts if there is none, multiple conditions, complex conditions

SQLite one line SQL implementation updates if there is one, inserts if there is none, multiple conditions, complex conditions

2022-06-11 05:32:00 Trace

SQLite a line SQL Update if there is one, insert if there is none , Multiple conditions , Complex conditions

Sample code :https://github.com/miqt/MultiProgressKV/blob/master/MultiProgressKV/src/main/java/com/miqt/multiprogresskv/DBHelper.java

For example, you want to implement the following logic

if (db has name == Xiao Ming  &&  height  == 170cm)
	update ....
else 
	insert ....

You can refer to the following SQL:

CREATE TABLE [TABLE_NAME](
  [id] INTEGER PRIMARY KEY AUTOINCREMENT, 
  [name] TEXT NOT NULL , 
  [attr] TEXT NOT NULL , 
  [value] TEXT NOT NULL );

CREATE UNIQUE INDEX [TABLE_NAME]
ON [TABLE_NAME](
  [name], 
  [attr]);

CREATE UNIQUE INDEX [TABLE_NAME] Declared that only name, and attr The unique index is calculated only when it is repeated , This applies to scenarios like inserting a book , A book can only have one title and author , And a separate title , And the author himself , It may correspond to many books .

The following statement executes , There are updates , If nothing, the effect of inserting , following SQL Executing multiple times will only produce one row of results , And modify name, perhaps attr Any column , A new one will be inserted .

REPLACE INTO TABLE_NAME
  (
    name ,
    attr ,
    value
  )
VALUES
  (
    '《 Journey to the west 》' ,
    ' author ' ,  
    ' Wu chengen '
  ) ;
原网站

版权声明
本文为[Trace]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020538448353.html