Inserting data and binding values
In SQL, we store data using two statements (usually): INSERT INTO
and UPDATE
. The first is used when you're adding data to a table, and the second is used to update data that already exists. There's also a variant that permits the merging of data: data that doesn't exist is added, and data that does exist is updated. Various SQL database servers do this differently, but SQLite uses INSERT
OR REPLACE INTO
.
Note
The snippets in this section are located at snippets/08/ex4-insert-data/
in the code package of this book. When using the interactive snippet playground, select 8: Web SQL Database and Example 4.
The INSERT INTO
statement looks as follows:
INSERT [OR REPLACE] INTO tableName ( fieldName [, ...] ) VALUES ( values [, ...] )
The UPDATE
statement looks as follows:
UPDATE tableName SET fieldName = value WHERE condition
In many ways, the UPDATE
statement is a lot more readable than the INSERT INTO
statement, mainly because with the latter, you...