1.添加使用sqlite的庫libsqlite3.dylib
2.操作sqlite的方法
1.打開數(shù)據(jù)庫
?sqlite3_open(
const char *filename,? // 數(shù)據(jù)庫的文件路徑
sqlite3 **ppDb? ? ? ? ? // 數(shù)據(jù)庫實例
);
sqlite3_open()將根據(jù)文件路徑打開數(shù)據(jù)庫,如果不存在,則會創(chuàng)建一個新的數(shù)據(jù)庫。如果result等于常量SQLITE_OK,則表示成功打開數(shù)據(jù)庫
sqlite3 *db:一個打開的數(shù)據(jù)庫實例
數(shù)據(jù)庫文件的路徑必須以C字符串(而非NSString)傳入
關(guān)閉數(shù)據(jù)庫:sqlite3_close(db);
2.執(zhí)行任何SQL語句
sqlite3_exec(
sqlite3*,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 一個打開的數(shù)據(jù)庫實例
const char *sql,? ? ? ? ? ? ? ? ? ? ? ? ? // 需要執(zhí)行的SQL語句
int (*callback)(void*,int,char**,char**),? // SQL語句執(zhí)行完畢后的回調(diào)
void *,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 回調(diào)函數(shù)的第1個參數(shù)
char **errmsg? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 錯誤信息
);
sqlite3_exec()可以執(zhí)行任何SQL語句,比如創(chuàng)表、更新、插入和刪除操作。但是一般不用它執(zhí)行查詢語句,因為它不會返回查詢到的數(shù)據(jù)
sqlite3_exec()還可以執(zhí)行的語句:
(1)開啟事務(wù):begin transaction;
(2)回滾事務(wù):rollback;
(3)提交事務(wù):commit;
3.檢查SQL語句的合法性(查詢前的準(zhǔn)備)
sqlite3_prepare_v2(
sqlite3 *db,? ? ? ? ? ? // 數(shù)據(jù)庫實例
const char *zSql,? ? ? // 需要檢查的SQL語句
int nByte,? ? ? ? ? ? ? // SQL語句的最大字節(jié)長度
sqlite3_stmt **ppStmt,? // sqlite3_stmt實例,用來獲得數(shù)據(jù)庫數(shù)據(jù)
const char **pzTail
);
sqlite3_prepare_v2()返回值等于SQLITE_OK,說明SQL語句已經(jīng)準(zhǔn)備成功,沒有語法問題
4.查詢一行數(shù)據(jù)
sqlite3_step(sqlite3_stmt*);?
sqlite3_step()返回SQLITE_ROW代表遍歷到一條新記錄
sqlite3_column_*()用于獲取每個字段對應(yīng)的值,第2個參數(shù)是字段的索引,從0開始
5.利用stmt獲得某一字段的值(字段的下標(biāo)從0開始)
sqlite3_column_double(sqlite3_stmt*, int iCol);? // 浮點數(shù)據(jù)
sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型數(shù)據(jù)
sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 長整型數(shù)據(jù)
sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二進制文本數(shù)據(jù)
sqlite3_column_text(sqlite3_stmt*, int iCol);? // 字符串?dāng)?shù)據(jù)
3.常用建表語句
1.創(chuàng)建數(shù)據(jù)庫:create database 數(shù)據(jù)庫名稱
2.刪除數(shù)據(jù)庫:drop database 數(shù)據(jù)庫名稱
3.創(chuàng)建數(shù)據(jù)表:create table if not exists 表名(id integer primary key autoincrement, 屬性名稱 屬性類別(text int ...), ....)
4.刪除數(shù)據(jù)表:drop table 表名
5.增加一個新的字段:alter table 表名 add column 列名 type
6.插入語句:insert into 表名(field1,field2...)values(value1,value2...)
7.刪除語句:delete form 表名 where 范圍
8.修改語句:update 表名 set field1 = value1 where 范圍
9.查找并排序(desc 降序,默認升序):select *form 表名 order by field1,field2 desc
10.查找語句: select *from 表名 where范圍;select *from 表名 where field1 like %?%%
11.查找總數(shù):select count as total count from 表名
12.求和:select sum(field) as sum value from 表名
13.求平均:select AVG (field) as AVG value from 表名
14.求最大:select max(field) as max values from ?表名
15.求最?。簊elect min(field) as min values from 表名
4.sqlite中常用編碼示列
1.創(chuàng)建、打開、關(guān)閉數(shù)據(jù)庫
創(chuàng)建或打開數(shù)據(jù)庫
// path是數(shù)據(jù)庫文件的存放路徑
sqlite3 *db = NULL;
int result = sqlite3_open([path UTF8String], &db);
2.執(zhí)行不返回數(shù)據(jù)的SQL語句
執(zhí)行創(chuàng)表語句
char *errorMsg = NULL;? // 用來存儲錯誤信息
char *sql = "create table if not exists t_person(id integer primary key autoincrement, name text, age integer);";
int result = sqlite3_exec(db, sql, NULL, NULL, &errorMsg);
3.帶占位符插入數(shù)據(jù)
char *sql = "insert into t_person(name, age) values(?, ?);";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_text(stmt, 1, "母雞", -1, NULL);
sqlite3_bind_int(stmt, 2, 27);
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
NSLog(@"插入數(shù)據(jù)錯誤");
}
sqlite3_finalize(stmt);
4.查詢數(shù)據(jù)
char *sql = "select id,name,age from t_person;";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
int _id = sqlite3_column_int(stmt, 0);
char *_name = (char *)sqlite3_column_text(stmt, 1);
NSString *name = [NSString stringWithUTF8String:_name];
int _age = sqlite3_column_int(stmt, 2);
NSLog(@"id=%i, name=%@, age=%i", _id, name, _age);
}
}
sqlite3_finalize(stmt);