在iOS中實現(xiàn)SQLite數(shù)據(jù)庫的操作:1.導(dǎo)入框架(libsqlite3.0.tbd) 2.導(dǎo)入頭文件<sqlite3.h> 3.實現(xiàn)數(shù)據(jù)的增刪改查
實現(xiàn)簡單 SQLite數(shù)據(jù)庫操作 的 demo 具體過程:
1.創(chuàng)建名為 SQLite_Manage 的.h .m 文件,導(dǎo)入頭文件 <sqlite3.h>
2.數(shù)據(jù)庫在一個app中只有一個,使用單例模式:(代碼如下)
1 + (SQLite_Manager *)sharedManager{2 static SQLite_Manager *manager = nil;3 static dispatch_once_t onceToken;4 dispatch_once(&onceToken, ^{5 manager = [[SQLite_Manager alloc]init];6 });7 return manager;8 }
3.打開數(shù)據(jù)庫,代碼如下:
1 - (void)open{ 2 //document路徑 3 NSString *docment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 4 //sqlite 路徑 5 NSString *sqlitePath = [docment stringByAppendingPathComponent:@"database.sqlite"]; 6 //打開數(shù)據(jù)庫 7 int result = sqlite3_open(sqlitePath.UTF8String, &db); 8 //判斷數(shù)據(jù)庫是否打開成功 9 if (result == SQLITE_OK) {10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"打開成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"打開失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
4.創(chuàng)建表,代碼如下:
1 - (void)creatTable{ 2 //sql語句 3 NSString *sqlString = @"create table Person (id integer PRimary key,name text,age integer)"; 4 //執(zhí)行SQL語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判斷是否出現(xiàn)了錯誤 9 if (error == nil){10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"創(chuàng)建表成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"創(chuàng)建表失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
5.插入數(shù)據(jù),代碼如下:
1 - (void)insert{ 2 //sql語句 3 NSString *sqlString = @"insert into Person ('name','age') values ('Ager',18)"; 4 //執(zhí)行SQL語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判斷是否出現(xiàn)了錯誤 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"插入數(shù)據(jù)成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }else {12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"插入數(shù)據(jù)失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];13 [alertView show];14 }15 16 }
6.修改數(shù)據(jù),代碼如下:
1 - (void)update{ 2 //sql語句 3 NSString *sqlString = @"update Person set 'name' = 'Arun' where id = 1"; 4 //執(zhí)行sql語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判斷是否出現(xiàn)了錯誤 9 if (error == nil){10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"數(shù)據(jù)更新成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"數(shù)據(jù)更新失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
7.查詢數(shù)據(jù),代碼如下:
1 - (void)select{ 2 //sql語句 3 NSString *sqlString = @"select * from Person"; 4 //準(zhǔn)備sql 5 sqlite3_stmt *stmt = nil; 6 sqlite3_prepare(db, sqlString.UTF8String,-1, &stmt, nil); 7 //單步執(zhí)行語句 8 while (sqlite3_step(stmt) == SQLITE_ROW) { 9 int ID = sqlite3_column_int(stmt, 0);10 const unsigned char *name = sqlite3_column_text(stmt, 1);11 int age = sqlite3_column_int(stmt, 2);12 NSLog(@"%d,%s,%d",ID,name,age);13 }14 sqlite3_finalize(stmt);15 }
8.刪除數(shù)據(jù),代碼如下:
1 - (void)deleteData{ 2 //sql語句 3 NSString *sqlString = @"delete from Person where id = 1"; 4 //執(zhí)行sql語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判斷是否出現(xiàn)了錯誤 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"刪除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }else {12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"刪除失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];13 [alertView show];14 }15 }
9.關(guān)閉數(shù)據(jù)庫,代碼如下:
1 - (void)close{ 2 //關(guān)閉數(shù)據(jù)庫 3 int result = sqlite3_close(db); 4 //判斷數(shù)據(jù)庫是否關(guān)閉成功 5 if (result == SQLITE_OK) { 6 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"關(guān)閉成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil]; 7 [alertView show]; 8 }else { 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"數(shù)據(jù)庫執(zhí)行結(jié)果" message:@"關(guān)閉失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }12 }
新聞熱點
疑難解答