本篇就來談談如何使用 SQLite 。 SQLite 與 SQL 不同的地方在於 SQLite 不是一個用戶端/伺服器結構的資料庫引擎,而是用 C 語言整合在用戶程式中。
SQLite 本身遵守 ACID 協定,實現了大多數 SQL 標準語法,在下一篇實作開始前先來講本次實作會用到的語法。
建立資料表:
create table if not exists table_name (
column1 datatype PRIMARY KEY(one or more columns),
column2 datatype,
column3 datatype,
…..
columnN datatype,
);
新增資料:
insert into table_name values (value1,value2,value3,…valueN);
讀取資料:
select column1, column2, columnN from table_name
where [condition]
order by [column1, column2, .. columnN][ASC | DESC]
limit [no of rows]
offset [row num]
更新資料:
update table_name
SET column1 = value1, column2 = value2…., columnN = valueN
where [condition];
刪除資料:
delete from table_name
where [condition];
語法說明:
where 用於從 table 中獲取數據的條件。
order 是用來對於一個或多個 column 按升序或降序順序排列數據。
limit 用於限制由 select 語句返回的資料數量。
offset 是搭配 limit 使用,使用時會回傳從 limit 條件的下一行開始直到給定的 offset 為止的所有資料