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

深度解析iPhone開發(fā)之?dāng)?shù)據(jù)持久化

移動開發(fā) iOS
在iPhone上面,有兩種方法可以來存儲數(shù)據(jù),我們可以使用SQLite進(jìn)行數(shù)據(jù)的持久化,另外值得一提的是Firefox是使用數(shù)據(jù)庫的方式保存的,同樣也是SQLite。

iphone在開發(fā)應(yīng)用程序的時候,當(dāng)然需要經(jīng)常的實(shí)用數(shù)據(jù)庫進(jìn)行數(shù)據(jù)的保存了,在移動設(shè)備上,我們可以使用文件,數(shù)據(jù)庫等方式去保存,為了能夠讓用戶無法使用其他的程序去修改,我這里認(rèn)為使用數(shù)據(jù)庫的方式是一個很好的方式。在iPhone上面,我們可以使用SQLite進(jìn)行數(shù)據(jù)的持久化。另外值得一提的是Firefox是使用數(shù)據(jù)庫的方式保存的,同樣也是SQLite。

在iPhone開發(fā)重,我們需要首先添加一個SQLite的庫,XCode本身就支持的,我們在左邊的Frameworks里面選擇Add,然后選擇Existing Frameworks,在彈出窗口中選擇SQLite的庫libsqlite3.0.dylib。

添加之后,我們就可以使用SQLite在iPhone中進(jìn)行數(shù)據(jù)的保存,查詢,刪除等操作了。

現(xiàn)在我們可以寫一個SQLite的Helper文件,方便我們在其他的代碼中使用,頭文件(SqliteHelper.h)如下。

  1. #import <Foundation/Foundation.h> 
  2. #import “sqlite3.h“  
  3. #define kFileName @”mydatabase.sql”    
  4. @interface SqliteHelper : NSObject {  
  5. sqlite3 *database;   
  6. }  
  7. //創(chuàng)建表  
  8. - (BOOL)createTable;  
  9. //插入數(shù)據(jù)  
  10. (BOOL)insertMainTable:(NSString*) username insertPassword:(NSString*) password;  
  11. //查詢表  
  12. (BOOL)checkIfHasUser;   
  13. @end 

我們的代碼文件如下。

  1. #import “SqliteHelper.h“   
  2. @implementation SqliteHelper  
  3. (BOOL)createTable  
  4. {  
  5. NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  6. NSString *paths = [[path objectAtIndex:0] stringByAppendingPathComponent:kFileName];  
  7. NSFileManager *fileManager = [NSFileManager defaultManager];  
  8. BOOL fileFinded = [fileManager fileExistsAtPath:paths];  
  9. NSLog(@”Database file path is %@“,paths);  
  10. if(fileFinded)  
  11. {  
  12.    NSLog(@”Database file existed“);  
  13. if(sqlite3_open([paths UTF8String],&database)!=SQLITE_OK)  
  14. {  
  15.    sqlite3_close(database);  
  16.    NSLog(@”Open Failed“);  
  17.    return NO;  
  18.   }  
  19. }else{  
  20.     NSLog(@”Database file is not existed“);  
  21. if(sqlite3_open([paths UTF8String],&database)!=SQLITE_OK)  
  22. {  
  23.    sqlite3_close(database);  
  24.    NSLog(@”Open Failed“);  
  25.    return NO;  
  26.   }  
  27. }  
  28. char *errorMsg;  
  29. NSString *createSQL = @”create table if not exists fields (userid integer primary key,username text,password text)“;  
  30. if(sqlite3_exec(database,[createSQL UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)  
  31. {  
  32.    sqlite3_close(database);  
  33.    NSLog(@”Open failed or init filed“);  
  34.    return NO;  
  35. }  
  36.    return YES;  
  37. }  
  38. (BOOL)insertMainTable:(NSString*) username insertPassword:(NSString*) password  
  39. {  
  40.    char *errorMsg;  
  41.     NSString *createSQL = @”create table if not exists fields (userid integer primary key,username text,password text)“;  
  42.    if(sqlite3_exec(database,[createSQL UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)  
  43. {  
  44.    sqlite3_close(database);  
  45.    NSLog(@”Open failed or init filed“);  
  46.    return NO;  
  47. }  
  48. NSString *insertData = [[NSString alloc] initWithFormat:@”insert or replace into fields 
  49. (userid,username,password) values (%d,’%@’,'%@’)“,0,username,password];  
  50. if(sqlite3_exec(database,[insertData UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)  
  51. {  
  52.    sqlite3_close(database);  
  53.    NSLog(@”Open failed or failed to insert“);  
  54.    return NO;  
  55. }  
  56.    return YES;  
  57. }  
  58. (BOOL)checkIfHasUser  
  59. {  
  60.    NSString *getUserCountSQL = @”select * from fields“;  
  61.    sqlite3_stmt *statement;  
  62.    NSLog(@”checkIfHasUser“);  
  63. if(sqlite3_prepare_v2(database,[getUserCountSQL UTF8String],-1,&statement,nil)==SQLITE_OK)  
  64. {  
  65. //while(sqlite3_step(statement) == SQLITE_ROW)  
  66. //{  
  67. // int row = sqlite3_column_int(statement,0);  
  68. // char* rowData = (char*)sqlite3_column_text(statement,2);  
  69. // NSString *fieldName = [[NSString alloc] initWithFormat:@”show%d”,row];  
  70. // NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];  
  71. //  
  72. // NSLog(@”fieldName is :%@,fieldValue is :%@”,fieldName,fieldValue);  
  73. // return [[NSString alloc] initWithFormat:@”fieldName is :%@,fieldValue is :%@”,fieldName,fieldValue];  
  74. //  
  75. // [fieldName release];  
  76. // [fieldValue release];  
  77. //}  
  78. //sqlite3_finalize(statement);  
  79. if(sqlite3_step(statement) == SQLITE_ROW)  
  80. {  
  81. NSLog(@”Have user“);  
  82. return YES;  
  83. }  
  84. }  
  85. NSLog(@”No user“);  
  86. return NO;  
  87. }  
  88. @end 

其中checkIfHasUser是檢查數(shù)據(jù),這個方法中我注釋的是得到數(shù)據(jù),因?yàn)槲覀冞@里只是check,所以不需要得到數(shù)據(jù),直接看是否存在數(shù)據(jù)即可。上面的代碼雖然沒有過多的注釋,但是代碼本身已經(jīng)很簡單了,上下文也非常清楚,所以我就不寫過多的注釋了。

【編輯推薦】

解析iphone多線程

智能數(shù)據(jù)倉庫的設(shè)計方法

非結(jié)構(gòu)化數(shù)據(jù)庫與異構(gòu)數(shù)據(jù)庫區(qū)別

讓數(shù)據(jù)庫飛起來 10大DB2優(yōu)化技巧

WWDC傳聞分析:神秘的iPhone 4S以及iOS 5

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-08-17 15:19:38

iPhone應(yīng)用數(shù)據(jù)

2011-07-07 15:45:45

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

2023-10-12 13:01:29

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

2011-08-12 14:54:45

iPhone委托

2011-07-29 14:55:25

iPhone開發(fā) 動畫過渡

2011-08-19 13:45:14

iPhone應(yīng)用iPhone OS數(shù)據(jù)

2011-08-02 18:07:03

iPhone 內(nèi)省 Cocoa

2011-07-29 15:09:48

iPhone Category

2025-03-14 12:30:00

Redis RDBRedis數(shù)據(jù)庫

2022-03-02 21:53:57

Spring數(shù)據(jù)庫持久化Jar包

2011-07-18 14:23:40

iPhone 多任務(wù)

2011-08-12 11:23:47

iPhone窗口視圖

2024-03-26 00:03:08

Redis數(shù)據(jù)RDB

2011-08-11 17:00:33

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

2011-08-10 10:10:21

iPhoneUIPopoverCo

2011-08-01 18:27:58

iPhone開發(fā) UISearchBa

2011-07-27 17:02:12

Xcode iPhone 單元測試

2021-03-18 08:18:15

ZooKeeper數(shù)據(jù)持久化

2010-08-11 08:44:01

Flex對象

2009-09-23 17:00:07

Hibernate持久
點(diǎn)贊
收藏

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