首先須要建立4個textfield,目的是把4個text存入到數據庫中去,觸發事件是在點擊home鍵的時候。當再次進入到程序的時候再從數據庫中讀取text的值並顯示。 html
而後命名之因此採用1234這種類型是由於遍歷的時候比較方便 sql
@property (nonatomic, retain) IBOutlet UITextField *field1; 數據庫
@property (nonatomic, retain) IBOutlet UITextField *field2; app
@property (nonatomic, retain) IBOutlet UITextField *field3; 編碼
@property (nonatomic, retain) IBOutlet UITextField *field4; atom
最後放上代碼,有很詳細的註釋#pragma mark - - (void)viewDidLoad { //先轉化 //NSString *path=[self dataFilePath]; //const char *pat=[path UTF8String];//const之後就不要再賦值了,常量 if (sqlite3_open([[self dataFilePath] UTF8String], &database)!= SQLITE_OK) {//不是0就是錯 sqlite3_close(database); NSAssert(0, @"Failed to open database");//若是是0, 失敗,報錯 } char *errorMsg; //建立表語句 NSString *createSQL = @"CREATE TABLE IF NOT EXISTS FIELDS (ROW INTEGER PRIMARY KEY, FIELD_DATA TEXT);";//設主鍵PRIMARY KEY //執行這個語句,使用sqlite3_exec的緣由就是一執行就完事,沒有後續操做了,可是查詢語句就比較複雜 if (sqlite3_exec (database, [createSQL UTF8String],NULL, NULL, &errorMsg) != SQLITE_OK) { sqlite3_close(database); NSAssert1(0, @"Error creating table: %s", errorMsg); } //查詢語句 NSString *query = @"SELECT ROW, FIELD_DATA FROM FIELDS ORDER BY ROW"; sqlite3_stmt *statement;//緩衝地帶,查詢完的結果,放在這個緩衝池裏 //-1是不限長度 if (sqlite3_prepare_v2( database, [query UTF8String],-1, &statement, nil) == SQLITE_OK) { //while按逐行循環取值,結束條件 while (sqlite3_step(statement) == SQLITE_ROW) { /*************分別取出一行中不一樣列的值************/ //從結果表中去出第1列的值,由於此列是int型,因此就用sqlite3_column_int int row = sqlite3_column_int(statement, 0); //從結果表中去出第2列的值,由於此列是char型,因此就用sqlite3_column_text第二列是text,類型要對應 char *rowData = (char *)sqlite3_column_text(statement, 1); //把c的字符串轉化爲NSString,項目中取使用 NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", row]; //把c的字符串轉化爲NSString,項目中取使用.編碼是utf8 NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData]; UITextField *field = [self valueForKey:fieldName]; field.text = fieldValue; [fieldName release]; [fieldValue release]; } //把緩衝的結果表關閉掉 sqlite3_finalize(statement); } UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app]; [super viewDidLoad]; }
- (NSString *)dataFilePath { spa
NSArray *paths = NSSearchPathForDirectoriesInDomains( code
NSDocumentDirectory, NSUserDomainMask, YES); orm
NSString *documentsDirectory = [paths objectAtIndex:0]; server
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
//此方法完成對數據進行保存,把textField的值輸入數據庫 - (void)applicationWillTerminate:(NSNotification *)notification { for (int i = 1; i <= 4; i++) { NSString *fieldName = [[NSString alloc] initWithFormat:@"field%d", i]; UITextField *field = [self valueForKey:fieldName]; [fieldName release]; //錯誤提示 char *errorMsg; //插入語句 .(?, ?)意思是先不說,之後再說 char *update = "INSERT OR REPLACE INTO FIELDS (ROW, FIELD_DATA) VALUES (?, ?);"; sqlite3_stmt *stmt; //由於參數是?,須要滯後給"?"賦值,因此此處用sqlite3_prepare_v2 if (sqlite3_prepare_v2(database, update, -1, &stmt,nil) == SQLITE_OK) { //給第一個問號賦值,第一個對應的位置的是row字段,因此用sqlite3_bind_int,bind是綁定 sqlite3_bind_int(stmt, 1, i);//注意第一個問號從1開始的 //給第二個問號賦值,第二個對應的位置的是FIRLD_DATA字段,因此用sqlite3_bind_text sqlite3_bind_text(stmt, 2, [field.text UTF8String], -1, NULL); } //真正致使插入的地方 if (sqlite3_step(stmt) != SQLITE_DONE)//不帶大括號只管一行 NSAssert1(0, @"Error updating table: %s", errorMsg);//斷言 sqlite3_finalize(stmt); } //for循環執行完畢才關閉,數據庫,說明完全用完了,由於這個方法執行了說明結束了 sqlite3_close(database); }