iOS開發資料庫篇—SQLite常用的函式

文頂頂水水發表於2014-07-27

iOS開發資料庫篇—SQLite常用的函式

一、簡單說明

1.開啟資料庫

int sqlite3_open(

    const char *filename,   // 資料庫的檔案路徑

    sqlite3 **ppDb          // 資料庫例項

);

 

2.執行任何SQL語句

int sqlite3_exec(

    sqlite3*,                                  // 一個開啟的資料庫例項

    const char *sql,                           // 需要執行的SQL語句

    int (*callback)(void*,int,char**,char**),  // SQL語句執行完畢後的回撥

    void *,                                    // 回撥函式的第1個引數

    char **errmsg                              // 錯誤資訊

);

 

3.檢查SQL語句的合法性(查詢前的準備)

int sqlite3_prepare_v2(

    sqlite3 *db,            // 資料庫例項

    const char *zSql,       // 需要檢查的SQL語句

    int nByte,              // SQL語句的最大位元組長度

    sqlite3_stmt **ppStmt,  // sqlite3_stmt例項,用來獲得資料庫資料

    const char **pzTail

);

 

4.查詢一行資料

int sqlite3_step(sqlite3_stmt*); // 如果查詢到一行資料,就會返回SQLITE_ROW

 

5.利用stmt獲得某一欄位的值(欄位的下標從0開始)

double sqlite3_column_double(sqlite3_stmt*, int iCol);  // 浮點資料

int sqlite3_column_int(sqlite3_stmt*, int iCol); // 整型資料

sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); // 長整型資料

const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); // 二進位制文字資料

const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);  // 字串資料

 

二、SQLite編碼

1.建立、開啟、關閉資料庫

建立或開啟資料庫

// path是資料庫檔案的存放路徑

sqlite3 *db = NULL;

int result = sqlite3_open([path UTF8String], &db); 

 

程式碼解析:

sqlite3_open()將根據檔案路徑開啟資料庫,如果不存在,則會建立一個新的資料庫。如果result等於常量SQLITE_OK,則表示成功開啟資料庫

sqlite3 *db:一個開啟的資料庫例項

資料庫檔案的路徑必須以C字串(而非NSString)傳入

 

關閉資料庫:sqlite3_close(db);

 

2.執行不返回資料的SQL語句

執行創表語句

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);

 

程式碼解析:

sqlite3_exec()可以執行任何SQL語句,比如創表、更新、插入和刪除操作。但是一般不用它執行查詢語句,因為它不會返回查詢到的資料

sqlite3_exec()還可以執行的語句:

(1)開啟事務:begin transaction;

(2)回滾事務:rollback;

(3)提交事務:commit;

 

3.帶佔位符插入資料

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(@”插入資料錯誤”);

}

sqlite3_finalize(stmt);

 

程式碼解析:

sqlite3_prepare_v2()返回值等於SQLITE_OK,說明SQL語句已經準備成功,沒有語法問題

sqlite3_bind_text():大部分繫結函式都只有3個引數

(1)第1個引數是sqlite3_stmt *型別

(2)第2個引數指佔位符的位置,第一個佔位符的位置是1,不是0

(3)第3個引數指佔位符要繫結的值

(4)第4個引數指在第3個引數中所傳遞資料的長度,對於C字串,可以傳遞-1代替字串的長度

(5)第5個引數是一個可選的函式回撥,一般用於在語句執行後完成記憶體清理工作

sqlite_step():執行SQL語句,返回SQLITE_DONE代表成功執行完畢

sqlite_finalize():銷燬sqlite3_stmt *物件

 

4.查詢資料

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);

程式碼解析:

sqlite3_step()返回SQLITE_ROW代表遍歷到一條新記錄

sqlite3_column_*()用於獲取每個欄位對應的值,第2個引數是欄位的索引,從0開始


相關文章