sqlite3基本操作

2021-09-21 17:23:50 字數 3170 閱讀 5180

一、終端操作

1.先進入專案的目錄裡

//

找到documents路徑

nslog(@"

%@", nshomedirectory());

終端:

cd documents/

2.建立資料庫

sqlite3 資料庫名稱(字尾可以隨便加)

sqlite3 db_student.sql

4.建立表(sql語句)

create table if not exists t_student(id integer primary key autoincrement,name text,age integer);

5.刪除表

drop table t_student;

6.增刪改查(sql語句)

insert into t_student(name,age) values('

jianze

',11);

delete from student where

id=2;

update student set name='

jianze

'where name='

jianze

';

select * from t_student

注意:

.help 獲取幫助

.quit 退出資料庫

.schema 檢視資料庫

二、**實現

1.建立資料庫

開啟資料庫,如果資料庫不存在就建立

db_student.sql"];

int result = sqlite3_open([path utf8string], &_sql);

if (result ==sqlite_ok)

else

2.建立表

//

1.建立sql語句

nsstring *createsql = @"

create table t_student(id integer primary key autoincrement, name text, age integer)";

//2.執行語句 除了select之外都是通過這個執行

int result =sqlite3_exec(_sql, [createsql utf8string], null, null, null);

if (result ==sqlite_ok)

else

3.增加資料

已知資料來源

nsstring *insertsql = @"

insert into t_student(name,age) values('jianze',20)";

if (sqlite3_exec(_sql, [insertsql utf8string], null, null, null) ==sqlite_ok)

else

需要外部傳遞資料

//

佔位符 ?

nsstring *insertsql = @"

insert into t_student(name,age,icon) values(?,?,?)";

//預處理

sqlite3_stmt *stmt =null;

if (sqlite3_prepare(_sql, [insertsql utf8string], -1, &stmt, null) ==sqlite_ok)

else

//釋放記憶體空間

sqlite3_finalize(stmt);

} else

4.改動資料

nsstring *updatesql = @"

update t_student set age=21 where id=1";

if (sqlite3_exec(_sql, [updatesql utf8string], null, null, null) ==sqlite_ok)

else

5.查詢資料

//

關鍵字 limit 0,2 從哪個位置讀 每次讀幾個

nsstring *selectsql = @"

select * from t_student limit 0,2";

//1.預處理 -1表示語句長度自己計算

sqlite3_stmt *stmt =null;

if (sqlite3_prepare(_sql, [selectsql utf8string], -1, &stmt, null) ==sqlite_ok)

} else

6.在已有的表中新增字段

nsstring *altersql = @"

alter table t_student add icon blob";

if (sqlite3_exec(_sql, [altersql utf8string], null, null, null) ==sqlite_ok)

else

幫助文件:

posted @

2019-05-07 23:38

健澤 閱讀(

...)

編輯收藏

sqlite3基本操作

sqlite3對很多通過的sql語句都支援,像select,update,insert,delete等等都支援地很好,只要懂sql語句就可以用sqlite3。1,下面是幾個比較重要的api函式 開啟資料庫,如果不存在則建立乙個 int sqlite3 open const char sqlite3 ...

sqlite3基本操作

1.sqlite3 db.sqlite3 進入名為db.sqlite3的資料庫 2.sqlite tables 檢視所有表 3 sqlite schema 表名 檢視表結構 4 sqlite database 檢視目前掛載的資料庫 5 sqlite quit 退出資料庫 6 root wzz sql...

sqlite3 基本操作

安裝sqlite3 sudo dnf install sqlite3 fedora sudo apt get install sqlite3 ubuntu 1 開啟資料庫,如果沒有則建立 sqlite3 test.db 2 建立 格式,student 中 有 integer型的 id 作為主鍵,不能...