資料庫的使用

2021-07-25 16:04:35 字數 4873 閱讀 5314

moviemodel中存放資料

@property(nonatomic,copy)nsstring *moviename;

@property(nonatomic,assign)double movieprice;

@property(nonatomic,assign)int movieid;

@inte***ce sqlistmanager ()

@end

static sqlistmanager *instance = nil;

@implementation sqlistmanager

+(instancetype)shareinstance);

return instance;

}+(instancetype)allocwithzone:(struct _nszone *)zone

//把資料庫移到應用程式資料夾下面,因為在nsbundle中使用資料庫太消耗效能

-(void)copydatabasetodocuments

//    拷貝資料庫到檔案位置下

[manager copyitematpath:atpath topath:_topath error:nil];

}//資料庫的增加

- (bool)adddatabasewithmoviemodel:(moviemodel *)model

//    2.建立sql語句

//    運算元據的控制代碼

sqlite3_stmt *stmt = null;

nsstring *statements = @"insert into movie(movieid,moviename,movieprice) values(?,?,?)";

//    執行準備語句,預編譯過程,看看準備語句是否合法

int prepareres = sqlite3_prepare_v2(_sqlite, [statements utf8string], -1, &stmt, null);

if (prepareres != sqlite_ok)

//    繫結引數 替換statement裡面的?

/*1.運算元據的控制代碼

2.需要替換第幾個問號

3.id

*/sqlite3_bind_int(stmt, 1, model.movieid);

/*1.運算元據的控制代碼

2.需要替換第幾個問號

3.增加的值

4.sql編譯後最大的長度限制  不限制(-1)

5.用不到的函式指標

*/sqlite3_bind_text(stmt, 2, [model.moviename utf8string], -1, null);

sqlite3_bind_double(stmt, 3, model.movieprice);

//    3.執行sql語句

int stepres = sqlite3_step(stmt);

if (stepres != sqlite_done)

//    4.語句完結

sqlite3_finalize(stmt);

//    5.關閉資料庫

sqlite3_close(_sqlite);

return yes;

}//查詢所有資料

-(nsarray *)searchallobjects

//    2.請求語句

nsstring *statements = @"select * from movie";

sqlite3_stmt *stmt = null;

//  請求

int prepareres = sqlite3_prepare_v2(_sqlite, [statements utf8string], -1, &stmt, null);

if (prepareres != sqlite_ok)

//    3.繫結引數(不需要)

//    4.執行語句

nsmutablearray *array = [nsmutablearray array];

int stepres = sqlite3_step(stmt);

//    需要判斷是否有下一條語句是否準備好

while (stepres == sqlite_row)

//        用model儲存資料加入字典中

model.movieprice = movieprice;

[array addobject:model];

stepres = sqlite3_step(stmt);

}//    5.結束語句

sqlite3_finalize(stmt);

//    6.關閉資料庫

sqlite3_close(_sqlite);

return array;

}//查詢某條資料

-(nsarray *)searchobjectwithmoviemodel:(moviemodel *)model

//    2.請求語句

nsstring *statements = @"select * from movie where movieid = ?";

sqlite3_stmt *stmt = null;

//  請求

int prepareres = sqlite3_prepare_v2(_sqlite, [statements utf8string], -1, &stmt, null);

if (prepareres != sqlite_ok)

//    3.繫結引數(不需要)

sqlite3_bind_int(stmt, 1, model.movieid);

//    4.執行語句

nsmutablearray *array = [nsmutablearray array];

int stepres = sqlite3_step(stmt);

//    需要判斷是否有下一條語句是否準備好

while (stepres == sqlite_row)

//        用model儲存資料加入字典中

model.movieprice = movieprice;

[array addobject:model];

stepres = sqlite3_step(stmt);

}//    5.結束語句

sqlite3_finalize(stmt);

//    6.關閉資料庫

sqlite3_close(_sqlite);

return array;

}//刪除資料

-(bool)deleteobjectwithmoviemodel:(moviemodel *)model

//    2.建立sql語句

//    運算元據的控制代碼

sqlite3_stmt *stmt = null;

nsstring *statements = @"delete from movie where movieid = ?";

int prepareres = sqlite3_prepare_v2(_sqlite, [statements utf8string], -1, &stmt, null);

if (prepareres != sqlite_ok)

//    繫結引數 替換statement裡面的?

/*1.運算元據的控制代碼

2.需要替換第幾個問號

3.id

*/sqlite3_bind_int(stmt, 1, model.movieid);

/*1.運算元據的控制代碼

2.需要替換第幾個問號

3.增加的值

4.sql編譯後最大的長度限制  不限制(-1)

5.用不到的函式指標

*///    3.執行sql語句

int stepres = sqlite3_step(stmt);

if (stepres != sqlite_done)

//    4.語句完結

sqlite3_finalize(stmt);

//    5.關閉資料庫

sqlite3_close(_sqlite);

return yes;

}//修改資料

-(bool)updateobjectwithmoviemodel:(moviemodel *)model

//    2.建立sql語句請求

sqlite3_stmt *stmt = null;

nsstring *statement = @"update movie set moviename = ?,movieprice = ? where movieid = ?";

bool issend = sqlite3_prepare_v2(_sqlite, [statement utf8string], -1, &stmt, null);

if (issend != sqlite_ok)

//    3.繫結引數

sqlite3_bind_text(stmt, 1, [model.moviename utf8string], -1, null);

sqlite3_bind_double(stmt, 2, model.movieprice);

sqlite3_bind_int(stmt, 3, model.movieid);

//    4.執行語句

int isstep = sqlite3_step(stmt);

if (isstep != sqlite_done)

//    5.語句完結

sqlite3_finalize(stmt);

//    6.關閉資料庫

sqlite3_close(_sqlite);

return yes;

}

簡單使用資料庫資料庫

查詢 注釋 查詢公式 select 欄位名字1 別名,欄位名字2 as 別名.from 表名 執行順序 from select確定結果集 查詢dept表中的所有資訊 select from dept 查詢雇員表中所有員工的員工編號,員工姓名,上級編號 要查詢的資料 empno,ename,mgr 條...

資料庫使用

資料庫 一種技術,以關係型模型的方式,來儲存資料的倉庫 oracle db2 sqlserver mysql 系統庫 使用者庫 建立 create database 資料庫 識別符號可以為 數字符號或下劃線 刪除 drop database 資料庫 使用 use 資料庫 tinyint 1位元組 0...

資料庫使用

使用sqlcommand物件可以使你連線資料庫,處理資料 sqlcommand sqlcommand new sqlcommand executescalar 方法 executescalar 0這個方法會立即執行並且生效在句子內。executenonquery 0而如果使用這個方法並不會立即執行查...