自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā):用SQLite3存儲和讀取數(shù)據(jù)

移動開發(fā) iOS
SQLite,是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫管理系統(tǒng),它的設(shè)計目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時能夠跟很多程序語言相結(jié)合,比如 Tcl、C#、PHP、Java等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源世界著名的數(shù)據(jù)庫管理系統(tǒng)來講,它的處理速度比他們都快。

SQLite3是嵌入在iOS中的關(guān)系型數(shù)據(jù)庫,對于存儲大規(guī)模的數(shù)據(jù)很有效。SQLite3使得不必將每個對象都加到內(nèi)存中。

基本操作:

(1)打開或者創(chuàng)建數(shù)據(jù)庫

  1. sqlite3 *database; 
  2. int result = sqlite3_open("/path/databaseFile", &database); 

如果/path/databaseFile不存在,則創(chuàng)建它,否則打開它。如果result的值是SQLITE_OK,則表明我們的操作成功。

注意上述語句中數(shù)據(jù)庫文件的地址字符串前面沒有@字符,它是一個C字符串。將NSString字符串轉(zhuǎn)成C字符串的方法是:

  1. const char *cString = [nsString UTF8String]; 

(2)關(guān)閉數(shù)據(jù)庫

  1. sqlite3_close(database); 

(3)創(chuàng)建一個表格

  1. char *errorMsg; 
  2. const char *createSQL = "CREATE TABLE IF NOT EXISTS PEOPLE (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIELD_DATA TEXT)"
  3. int result = sqlite3_exec(database, createSQL, NULL, NULL, &errorMsg); 

執(zhí)行之后,如果result的值是SQLITE_OK,則表明執(zhí)行成功;否則,錯誤信息存儲在errorMsg中。

sqlite3_exec這個方法可以執(zhí)行那些沒有返回結(jié)果的操作,例如創(chuàng)建、插入、刪除等。

(4)查詢操作

  1. NSString *query = @"SELECT ID, FIELD_DATA FROM FIELDS ORDER BY ROW"
  2. sqlite3_stmt *statement; 
  3. int result = sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil); 

如果result的值是SQLITE_OK,則表明準(zhǔn)備好statement,接下來執(zhí)行查詢:

  1. while (sqlite3_step(statement) == SQLITE_ROW) { 
  2.     int rowNum = sqlite3_column_int(statement, 0); 
  3.     char *rowData = (char *)sqlite3_column_text(statement, 1); 
  4.     NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData]; 
  5.     // Do something with the data here 
  6. sqlite3_finalize(statement); 

使用過其他數(shù)據(jù)庫的話應(yīng)該很好理解這段語句,這個就是依次將每行的數(shù)據(jù)存在statement中,然后根據(jù)每行的字段取出數(shù)據(jù)。

(5)使用約束變量

實際操作時經(jīng)常使用叫做約束變量的東西來構(gòu)造SQL字符串,從而進行插入、查詢或者刪除等。

例如,要執(zhí)行帶兩個約束變量的插入操作,***個變量是int類型,第二個是C字符串:

  1. char *sql = "insert into oneTable values (?, ?);"
  2. sqlite3_stmt *stmt; 
  3. if (sqlite3_prepare_v2(database, sql, -1, &stmt, nil) == SQLITE_OK) { 
  4.     sqlite3_bind_int(stmt, 1, 235); 
  5.     sqlite3_bind_text(stmt, 2, "valueString", -1, NULL); 
  6. if (sqlite3_step(stmt) != SQLITE_DONE) 
  7.     NSLog(@"Something is Wrong!"); 
  8. sqlite3_finalize(stmt); 

這里,sqlite3_bind_int(stmt, 1, 235);有三個參數(shù):

***個是sqlite3_stmt類型的變量,在之前的sqlite3_prepare_v2中使用的。

第二個是所約束變量的標(biāo)簽index。

第三個參數(shù)是要加的值。

有一些函數(shù)多出兩個變量,例如

  1. sqlite3_bind_text(stmt, 2, "valueString", -1, NULL); 

這句,第四個參數(shù)代表第三個參數(shù)中需要傳遞的長度。對于C字符串來說,-1表示傳遞全部字符串。

第五個參數(shù)是一個回調(diào)函數(shù),比如執(zhí)行后做內(nèi)存清除工作。

接下來,做個小例子吧!

1、運行Xcode 4.3,新建一個Single View Application,名稱為SQLite3 Test:

2、連接SQLite3庫:

按照下圖中的紅色數(shù)字的順序找到加號:

單擊這個加號,打開窗口,在搜索欄輸入sqlite3:

選擇libsqlite3.dylib,單擊Add,添加到工程。

3、進行界面設(shè)計。打開ViewController.xib,使用Interface Builder設(shè)計界面如下:

設(shè)置四個文本框的tag分別是1、2、3、4。

4、在ViewController.h中添加屬性和方法:

  1. @property (copy, nonatomic) NSString *databaseFilePath; 
  2.  
  3. - (void)applicationWillResignActive:(NSNotification *)notification; 

5、打開ViewController.m,向其中添加代碼:

5.1 在開頭添加代碼:

  1. #import "sqlite3.h" 
  2. #define kDatabaseName @"database.sqlite3" 

5.2 在@implementation之后添加代碼:

  1. @synthesize databaseFilePath; 

5.3 在viewDidLoad方法中添加代碼:

  1. - (void)viewDidLoad 
  2.     [super viewDidLoad]; 
  3.     // Do any additional setup after loading the view, typically from a nib. 
  4.     //獲取數(shù)據(jù)庫文件路徑 
  5.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
  6.     NSString *documentsDirectory = [paths objectAtIndex:0]; 
  7.     self.databaseFilePath = [documentsDirectory stringByAppendingPathComponent:kDatabaseName]; 
  8.     //打開或創(chuàng)建數(shù)據(jù)庫 
  9.     sqlite3 *database; 
  10.     if (sqlite3_open([self.databaseFilePath UTF8String] , &database) != SQLITE_OK) { 
  11.         sqlite3_close(database); 
  12.         NSAssert(0, @"打開數(shù)據(jù)庫失??!"); 
  13.     } 
  14.     //創(chuàng)建數(shù)據(jù)庫表 
  15.     NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (TAG INTEGER PRIMARY KEY, FIELD_DATA TEXT);"
  16.     char *errorMsg; 
  17.     if (sqlite3_exec(database, [createSQL UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) { 
  18.         sqlite3_close(database); 
  19.         NSAssert(0, @"創(chuàng)建數(shù)據(jù)庫表錯誤: %s", errorMsg); 
  20.     } 
  21.     //執(zhí)行查詢 
  22.     NSString *query = @"SELECT TAG, FIELD_DATA FROM FIELDS ORDER BY TAG"
  23.     sqlite3_stmt *statement; 
  24.     if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) { 
  25.         //依次讀取數(shù)據(jù)庫表格FIELDS中每行的內(nèi)容,并顯示在對應(yīng)的TextField 
  26.         while (sqlite3_step(statement) == SQLITE_ROW) { 
  27.             //獲得數(shù)據(jù) 
  28.             int tag = sqlite3_column_int(statement, 0); 
  29.             char *rowData = (char *)sqlite3_column_text(statement, 1); 
  30.             //根據(jù)tag獲得TextField 
  31.             UITextField *textField = (UITextField *)[self.view viewWithTag:tag]; 
  32.             //設(shè)置文本 
  33.             textField.text = [[NSString alloc] initWithUTF8String:rowData]; 
  34.         } 
  35.         sqlite3_finalize(statement); 
  36.     } 
  37.     //關(guān)閉數(shù)據(jù)庫 
  38.     sqlite3_close(database); 
  39.     //當(dāng)程序進入后臺時執(zhí)行寫入數(shù)據(jù)庫操作 
  40.     UIApplication *app = [UIApplication sharedApplication]; 
  41.     [[NSNotificationCenter defaultCenter] 
  42.      addObserver:self 
  43.      selector:@selector(applicationWillResignActive:) 
  44.      name:UIApplicationWillResignActiveNotification 
  45.      object:app]; 

5.4 在@end之前實現(xiàn)方法:

  1. //程序進入后臺時的操作,實現(xiàn)將當(dāng)前顯示的數(shù)據(jù)寫入數(shù)據(jù)庫 
  2. - (void)applicationWillResignActive:(NSNotification *)notification { 
  3.     //打開數(shù)據(jù)庫 
  4.     sqlite3 *database; 
  5.     if (sqlite3_open([self.databaseFilePath UTF8String], &database) 
  6.         != SQLITE_OK) { 
  7.         sqlite3_close(database); 
  8.         NSAssert(0, @"打開數(shù)據(jù)庫失??!"); 
  9.     } 
  10.     //向表格插入四行數(shù)據(jù) 
  11.     for (int i = 1; i <= 4; i++) {  
  12.         //根據(jù)tag獲得TextField 
  13.         UITextField *textField = (UITextField *)[self.view viewWithTag:i]; 
  14.         //使用約束變量插入數(shù)據(jù) 
  15.         char *update = "INSERT OR REPLACE INTO FIELDS (TAG, FIELD_DATA) VALUES (?, ?);"
  16.         sqlite3_stmt *stmt; 
  17.         if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) { 
  18.             sqlite3_bind_int(stmt, 1, i); 
  19.             sqlite3_bind_text(stmt, 2, [textField.text UTF8String], -1, NULL); 
  20.         } 
  21.         char *errorMsg = NULL; 
  22.         if (sqlite3_step(stmt) != SQLITE_DONE) 
  23.             NSAssert(0, @"更新數(shù)據(jù)庫表FIELDS出錯: %s", errorMsg); 
  24.         sqlite3_finalize(stmt); 
  25.     } 
  26.     //關(guān)閉數(shù)據(jù)庫 
  27.     sqlite3_close(database); 

6、實現(xiàn)關(guān)閉鍵盤,參考《iOS開發(fā)4:關(guān)閉鍵盤》中的第2步。其中,backgroundTap方法如下:

  1. //關(guān)閉鍵盤 
  2. - (IBAction)backgroundTap:(id)sender { 
  3.     for (int i = 1; i <= 4; i++) { 
  4.         UITextField *textField = (UITextField *)[self.view viewWithTag:i]; 
  5.         [textField resignFirstResponder]; 
  6.     } 

7、運行程序:

剛運行時顯示如下面左圖:

在各個文本框輸入內(nèi)容,如上面右圖。然后按Home鍵,這樣,就執(zhí)行了寫入數(shù)據(jù)的操作。

***次運行程序時,在SandBox的Documents目錄下出現(xiàn)數(shù)據(jù)庫文件database.sqlite3:

此時退出程序,再次運行,則顯示的就是上次退出時的值。

責(zé)任編輯:閆佳明 來源: cocoachina
相關(guān)推薦

2012-03-06 12:59:11

iOS SQLite3iOSSQLite3

2012-03-06 10:17:45

iOS SQLite3iOSSQLite3

2021-02-15 15:40:28

SQLite3數(shù)據(jù)庫

2011-07-07 16:42:38

iPhone Sqlite3 數(shù)據(jù)庫

2013-04-10 14:21:35

2013-04-09 16:47:19

iOS嵌入式數(shù)據(jù)庫SQLit

2012-02-29 10:18:31

SQLite3Android

2019-08-12 11:40:48

數(shù)據(jù)庫SQLite3數(shù)據(jù)類型

2011-08-15 17:20:25

iPhone應(yīng)用Sqlite3FMDB

2012-03-01 20:42:12

iPhone

2011-08-05 16:50:00

iPhone 數(shù)據(jù) Sqlite

2012-03-06 09:50:24

Android SQLAndroidSQLite3

2017-10-26 12:37:24

Pythonsqlite3數(shù)據(jù)庫

2011-08-01 13:32:07

Objective-C Sqlite3 框架

2011-07-05 17:38:52

QT Sqlite

2013-04-01 10:49:51

iOS開發(fā)sqlite數(shù)據(jù)庫

2024-02-19 00:00:00

SQLite3數(shù)據(jù)庫代碼

2011-09-07 15:39:08

iPhoneObjective-CSQLite3

2020-09-24 16:05:44

C語言sqlite3函數(shù)

2013-07-25 14:44:48

sqlite實例教程iOS開發(fā)學(xué)習(xí)sqlite打造詞典
點贊
收藏

51CTO技術(shù)棧公眾號