iOS 中SQLite資料庫操作,iossqlite

來源:互聯網
上載者:User

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 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.