iOS資料持久化—SQLite常用的函數

來源:互聯網
上載者:User

標籤:style   blog   io   ar   os   sp   strong   on   檔案   

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開始

 

  

iOS資料持久化—SQLite常用的函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.