Sqlite - SELECT or INSERT and SELECT in one statement

前端 未结 1 1362
南方客
南方客 2021-02-05 17:21

I\'m trying to avoid writing separate SQL queries to achieve the following scenario:

I have a Table called Values:

Values:

id INT (PK)
data TEXT
         


        
1条回答
  •  攒了一身酷
    2021-02-05 17:48

    Your command does not work because in SQL, INSERT does not return a value.

    If you have a unique constraint/index on the data column, you can use that to prevent duplicates if you blindly insert the value; this uses SQLite's INSERT OR IGNORE extension:

    INSERT OR IGNORE INTO "Values"(data) VALUES('SOME_DATE');
    SELECT id FROM "Values" WHERE data = 'SOME_DATA';
    

    0 讨论(0)
提交回复
热议问题