iOS OC SQLite 資料庫儲存

2021-06-27 04:18:46 字數 1838 閱讀 9883

1、環境配置

2、開啟資料庫

3、建立資料表

4、插入記錄

5、修改記錄

6、刪除記錄

7、查詢記錄

8、關閉資料庫

配置資料庫路徑

// 設定資料庫檔案路徑

// 建立資料庫控制代碼

sqlite3 *db;

char *error;

// 開啟資料庫,資料庫檔案不存在時,自動建立檔案

if (sqlite3_open([databasefilepath utf8string], &db) == sqlite_ok) else

/*

sql 語句,專門用來運算元據庫的語句。

create table if not exists 是固定的,如果表不存在就建立。

mytable() 表示乙個表,mytable 是表名,小括號裡是字段資訊。

字段之間用逗號隔開,每乙個欄位的第乙個單詞是欄位名,第二個單詞是資料型別,primary key 代表主鍵,autoincrement 是自增。

*/nsstring *createsql = @"create table if not exists mytable(id integer primary key autoincrement, name text, age integer, address text)";

if (sqlite3_exec(db, [createsql utf8string], null, null, &error) == sqlite_ok) else

nsstring *insertsql = @"insert into mytable(name, age, address) values('小新', '8', '東城區')";

if (sqlite3_exec(db, [insertsql utf8string], null, null, &error) == sqlite_ok) else

nsstring *updatesql = @"update mytable set name = '小白', age = '10', address = '西城區' where id = 2";

if (sqlite3_exec(db, [updatesql utf8string], null, null, &error) == sqlite_ok) else

nsstring *deletesql = @"delete from mytable where id = 3";

if (sqlite3_exec(db, [deletesql utf8string], null, null, &error) == sqlite_ok) else

sqlite3_stmt *statement;

// @"select * from mytable" 查詢所有 key 值內容

nsstring *selectsql = @"select id, name, age, address from mytable";

if (sqlite3_prepare_v2(db, [selectsql utf8string], -1, &statement, nil) == sqlite_ok)

} else

sqlite3_finalize(statement);

sqlite3_close(db);

iOS OC SQLite 資料庫儲存

配置資料庫路徑 設定資料庫檔案路徑 建立資料庫控制代碼 sqlite3 db char error 開啟資料庫,資料庫檔案不存在時,自動建立檔案 if sqlite3 open databasefilepath utf8string db sqlite ok else sql 語句,專門用來運算元據...

Hibernate HQL基礎 呼叫資料庫儲存過程

在hibernate中也可以通過sqlquery物件呼叫資料庫的儲存過程,但是要求儲存過程必須返回乙個結果集。如在oracle資料庫的乙個儲存過程為 create or replace procedure selectguestbookbyid sp ref out sys refcursor,in...

Android學習之SQLite資料庫儲存

今天學習sqlite資料庫儲存,sqlite資料庫是輕量級的,非常小,只有幾百k大小,非常 移動裝置使用,幾乎所有的手機使用的都是sqlite資料庫。sqlite儲存的資料型別 db 資料儲存的路徑 data data packagename databases db 然後是最重要的api的學習 s...