iOS 中SQLite資料庫操作,iossqlite
在iOS中實現SQLite資料庫的操作:1.匯入架構(libsqlite3.0.tbd) 2.匯入標頭檔<sqlite3.h> 3.實現資料的增刪改查
實現簡單 SQLite資料庫操作 的 demo 具體過程:
1.建立名為 SQLite_Manage 的.h .m 檔案,匯入標頭檔 <sqlite3.h>
2.資料庫在一個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.開啟資料庫,代碼如下:
1 - (void)open{ 2 //document路徑 3 NSString *docment = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 4 //sqlite 路徑 5 NSString *sqlitePath = [docment stringByAppendingPathComponent:@"database.sqlite"]; 6 //開啟資料庫 7 int result = sqlite3_open(sqlitePath.UTF8String, &db); 8 //判斷資料庫是否開啟成功 9 if (result == SQLITE_OK) {10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"開啟成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"開啟失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
4.建立表,代碼如下:
1 - (void)creatTable{ 2 //sql語句 3 NSString *sqlString = @"create table Person (id integer primary key,name text,age integer)"; 4 //執行SQL語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判斷是否出現了錯誤 9 if (error == nil){10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"建立表成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"建立表失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
5.插入資料,代碼如下:
1 - (void)insert{ 2 //sql語句 3 NSString *sqlString = @"insert into Person ('name','age') values ('Ager',18)"; 4 //執行SQL語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判斷是否出現了錯誤 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"插入資料成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }else {12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"插入資料失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];13 [alertView show];14 }15 16 }
6.修改資料,代碼如下:
1 - (void)update{ 2 //sql語句 3 NSString *sqlString = @"update Person set 'name' = 'Arun' where id = 1"; 4 //執行sql語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 8 //判斷是否出現了錯誤 9 if (error == nil){10 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"資料更新成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];11 [alertView show];12 }else {13 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"資料更新失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];14 [alertView show];15 }16 }
7.查詢資料,代碼如下:
1 - (void)select{ 2 //sql語句 3 NSString *sqlString = @"select * from Person"; 4 //準備sql 5 sqlite3_stmt *stmt = nil; 6 sqlite3_prepare(db, sqlString.UTF8String,-1, &stmt, nil); 7 //逐步執行語句 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.刪除資料,代碼如下:
1 - (void)deleteData{ 2 //sql語句 3 NSString *sqlString = @"delete from Person where id = 1"; 4 //執行sql語句 5 char *error = nil; 6 sqlite3_exec(db, sqlString.UTF8String, nil, nil, &error); 7 //判斷是否出現了錯誤 8 if (error == nil){ 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"刪除成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }else {12 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"刪除失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];13 [alertView show];14 }15 }
9.關閉資料庫,代碼如下:
1 - (void)close{ 2 //關閉資料庫 3 int result = sqlite3_close(db); 4 //判斷資料庫是否關閉成功 5 if (result == SQLITE_OK) { 6 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"關閉成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil]; 7 [alertView show]; 8 }else { 9 UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"資料庫執行結果" message:@"關閉失敗" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"確定", nil];10 [alertView show];11 }12 }