SQLite資料庫操作流程

2021-07-03 19:36:41 字數 3428 閱讀 1314

sqlite常用函式:

開啟資料庫: sqlite3_open();

關閉資料庫: sqlite3_close();

執行sql語句: sqlite3_exec();

編譯sql語句: sqlite3_prepare_v2();

執行查詢sql語句: sqlite3_setp();

結束sql語句: sqlite3_finalize();

繫結引數: sqlite3_bind_text();

查詢欄位上的資料: sqlite3_column_text();

新建乙個類繼承於nsobject 新增系統庫libsqlite3.dylib 並匯入檔案

#import

//建立表
- (bool)createtable

// 2.寫sql資料

nsstring *sql = @"create table if not exists user(user_name text,user_age integer,user_id integer )";

// (1)執行語句

char *error = nil;

int exec = sqlite3_exec(sqlite, [sql utf8string], null, null, &error);

// (2)如果執行語句失敗了

if (exec != sqlite_ok)

// 3.關閉資料庫

sqlite3_close(sqlite);

return yes;

}//插入資料

- (bool)insertdata:(usermodel *)model

// 2.編寫sql資料 ?是佔位符

nsstring *sql = @"insert into user(user_name,user_age,user_id) values(?,?,?)";

// 3.編譯sql語句

// (1)宣告資料庫控制代碼

sqlite3_stmt *stmt = nil;

// (2)編譯sql語句

result = sqlite3_prepare_v2(sqlite, [sql utf8string], -1, &stmt, null);

// (3)如果編譯失敗了

if (result != sqlite_ok)

// (4)繫結資料 int:?的位置 從1開始

// 需要繫結的資料

nsstring *name = model.name;

nsinteger age = model.age;

nsinteger id = model.id;

sqlite3_bind_text(stmt, 1, [name utf8string], -1, nil);

sqlite3_bind_int64(stmt, 2, age);

sqlite3_bind_int64(stmt, 3, id);

// 4.執行sql語句

result = sqlite3_step(stmt);

if (result != sqlite_done)

// 5.關閉資料庫和控制代碼

//關閉資料庫

sqlite3_close(sqlite);

//關閉控制代碼

sqlite3_finalize(stmt);

return yes;

}//更新資料

- (bool)updatedata

// 2.編寫sql資料 ?是佔位符

nsstring *sql = @"update user set user_name=? where user_age=? ";

// 3.編譯sql語句

// (1)宣告資料庫控制代碼

sqlite3_stmt *stmt = nil;

// (2)編譯sql語句

result = sqlite3_prepare_v2(sqlite, [sql utf8string], -1, &stmt, null);

// (3)如果編譯失敗了

if (result != sqlite_ok)

// (4)繫結資料 int:?的位置 從1開始

// 需要繫結的資料

nsstring *name = @"孔子";

nsinteger age = 100;

sqlite3_bind_text(stmt, 1, [name utf8string], -1, nil);

sqlite3_bind_int64(stmt, 2, age);

// 4.執行sql語句

result = sqlite3_step(stmt);

if (result != sqlite_done)

// 5.關閉資料庫和控制代碼

//關閉資料庫

sqlite3_close(sqlite);

//關閉控制代碼

sqlite3_finalize(stmt);

return yes;

}//查詢語句

- (void)selectdata

// 2.編寫sql資料 ?是佔位符

nsstring *sql = @"select * from user where user_name=?";

// 3.編譯sql語句

// (1)宣告資料庫控制代碼

sqlite3_stmt *stmt = nil;

// (2)編譯sql語句

result = sqlite3_prepare_v2(sqlite, [sql utf8string], -1, &stmt, null);

// (3)如果編譯失敗了

if (result != sqlite_ok)

// (4)繫結資料 int:?的位置 從1開始

// 需要繫結的資料

nsstring *name = @"孔子";

sqlite3_bind_text(stmt, 1, [name utf8string], -1, nil);

// 4.執行sql語句

result = sqlite3_step(stmt);

//迴圈遍歷後的資料列表

while (result == sqlite_row)

sqlite3_close(sqlite);

sqlite3_finalize(stmt);

}

資料庫操作 SQLite

sqlite 是乙個輕量級的關聯式資料庫。sqlite最初的設計目標是用於嵌入式系統,它占用資源非常少,在嵌入式裝置中,只需要幾百k的記憶體就夠了,目前應用於android ios windows phone等智慧型手機。ios 使用時sqlite,只需要加入 libsqlite3.dylib 依賴...

資料庫操作 SQLite

sqlite 是乙個輕量級的關聯式資料庫。sqlite最初的設計目標是用於嵌入式系統,它占用資源非常少,在嵌入式裝置中,只需要幾百k的記憶體就夠了,目前應用於android ios windows phone等智慧型手機。ios 使用時sqlite,只需要加入 libsqlite3.dylib 依賴...

SQLite資料庫操作

建立資料庫需要使用的api sqliteopenhelper 必須定義乙個構造方法 arg1 資料庫的名字 people.db arg2 游標工廠 通常直接傳人null,則系統會使用預設的工廠 arg3 資料庫版本號 從1開始 方便公升級使用,不斷設定更大的值會呼叫,onupgrade方法 publ...