iOS開發中SQlite常用語句

SQlite常用語句

注:SQlite語句 不區分大小寫

1.創建表語句
create table Student (Student 是表名)
IF NOT EXISTS 表不存在 才創建
gender text 表示其類型 字符串

存儲類型:
NULL 值是空值
INTEGER 值是整型
REAL 值是浮點數
TEXT 值是文本字符串
BLOB 值是一個二進制類型

number integer primary key not NULL 主鍵值 如果不操作 自增

create table IF NOT EXISTS lanOuStudent(number integer primary key not NULL, name text not NULL, gender text not NULL, age integer not NULL)


2.插入語句
insert into lanOuStudent 表名
注:單引號 與 順序對應

insert into lanOuStudent(name ,gender ,age , number) values('%@' ,'%@' , '%ld' , '%ld')


3.刪除語句
delete from lanOuStudent 表名
where 根據條件刪除

delete from lanOuStudent where age > '%ld'

4.更新語句
update lanOuStudent 表名
where 根據條件更新
set age 更新的字段

update lanOuStudent set age = '%ld' where name = '%@'

5.查詢語句
where 根據條件查詢  多條件用 and 連接
*表示 查詢所有字段

select * from lanOuStudent where name = '%@' and age = '%ld'

select * from lanOuStudent 查詢所有


重要函數參數:

sqlite3_exec(sqlite3 *, const char *sql, int (*callback)(void *, int, char **, char **), void *, char **errmsg)

第1個參數  是前面open函數得到的指針。
第2個參數  是一條sql語句。
第3個參數  是回調,當這條語句執行之後,sqlite3會去調用你提供的這個函數。
第4個參數  是你所提供的指針,你可以傳遞任何一個指針參數到這裏,這個參數最終會傳到回調函數裏面,如果不需要傳遞指針給回調函數,可以填NULL。等下我們再看回調函數的寫法,以及這個參數的使用。
第5個參數  是錯誤信息。


sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **ppStmt, const char **pzTail)

int nByte -1 指sql語句長度 可以無限長
sqlite3_stmt 跟隨指針 地址
const char **pzTail 截取sql語句未使用部分

綁定查詢值
第二個參數 指查詢的第幾個問號 從1開始
sqlite3_bind_text(stmt, 1, name.UTF8String, -1, NULL);

讀取數據
第二個參數 指的是 表中的列數 從0開始
char *name = (char *)sqlite3_column_text(stmt, 1);

發佈了21 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章