基本数据持久性

前端之家收集整理的这篇文章主要介绍了基本数据持久性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

获取Documents目录

  1. NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  2. NSString*documentsDirectory=[pathobjectAtIndex:0];
  3. NSString*filename=[documentsDirectorystringByAppendingFormat:@"thefFile.txt"];

获取tmp目录

copy
    NSString*tempPath=NSTemporaryDirectory();
  1. NSString*tempFile=[tempFilestringByAppendingFormat:@"tempFile.txt"];

属性列表序列化

许多应用程序都使用了属性列表,比如使用属性列表来指定程序首选项,只要字典或数组仅包含特定可序列化的对象,就可以将NSDictionary和NSarray实例写入属性列表及从属性列表创建他们。序列化对象已被转换为字节流,以便存储到文件中,或通过网络进行传输。尽管可以让任何对象可序列化,但是只能将某些对象放置到某个集合类(如NSDictionary或NSArray)中,然后使用该集合的writeToFile方法将他们存储到属性列表

如果你打算使用属性列表持久保存程序数据,则可以使用NSArray或NDDirectionary容纳需要持久化的数据,假设你放到NSArray或NSDirectionary的所有对象都是可序列化的对象,则可以通过字典或数组实例调用writeToFile:atnomically:方法来编写属性列表

copy
    [myArraywriteToFile:@"/some/file/location/output.plist"atomically:YES];

1、新建single view application

2、打开viewController.xib,添加控件如图


2、修改控制器

copy
    #import<UIKit/UIKit.h>
  1. #definekFilename@"data.plist"
  2. @interfaceViewController:UIViewController
  3. {
  4. IBOutletUITextField*field1;
  5. IBOutletUITextField*field2;
  6. IBOutletUITextField*field3;
  7. IBOutletUITextField*field4;
  8. }
  9. @property(nonatomic,retain)UITextField*field1;
  10. @property(nonatomic,retain)UITextField*field2;
  11. -(NSString*)dataFilePath;
  12. -(void)applicationWillTerminate:(NSNotification*)notification;
  13. @end

copy
    @implementationViewController
  1. @synthesizefield1;
  2. @synthesizefield2;
  3. @synthesizefield3;
  4. @synthesizefield4;
  5. -(NSString*)dataFilePath
  6. {
  7. NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
  8. return[documentsDirectorystringByAppendingFormat:@"thefFile.txt"];
  9. void)applicationWillTerminate:(NSNotification*)notifaction
  10. NSMutableArray*array=[[NSMutableArrayalloc]init];
  11. [arrayaddObject:field1.text];
  12. [arrayaddObject:field2.text];
  13. [arrayaddObject:field3.text];
  14. [arrayaddObject:field4.text];
  15. [arraywriteToFile:[selfdataFilePath]atomically:YES];
  16. [arrayrelease];
  17. }
  18. -(void)viewDidLoad
  19. NSString*filePath=[selfdataFilePath];
  20. if([[NSFileManagerdefaultManager]fileExistsAtPath:filePath]){
  21. NSArray*array=[[NSArrayalloc]initWithContentsOfFile:filePath];
  22. field1.text=[arrayobjectAtIndex:0];
  23. field2.text=[arrayobjectAtIndex:1];
  24. field3.text=[arrayobjectAtIndex:2];
  25. field4.text=[arrayobjectAtIndex:3];
  26. UIApplication*app=[UIApplicationsharedApplication];
  27. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotificationobject:app];
  28. [superviewDidLoad];
  29. void)dealloc{
  30. [field1release];
  31. [field2release];
  32. [field3release];
  33. [field4release];
  34. [superdealloc];
  35. @end

3、重新打开.xib 按下Control的同时,将File's Owner图标拖放到第一个文本字段,选择field1,第二个连接到field2 ......

对象归档

[csharp] copy
    #definekField1Key@"Field1"
  1. #definekField2Key@"Field2"
  2. #definekField3Key@"Field3"
  3. #definekField4Key@"Field4"
  4. #import<UIKit/UIKit.h>
  5. @interfaceFourLines:NSObject<NSCoding,NSCopying>
  6. NSString*field1;
  7. NSString*field2;
  8. NSString*field3;
  9. NSString*field4;

copy
    @implementationFourLines
  1. void)encodeWithCoder:(NSCoder*)aCoder
  2. [aCoderencodeObject:field1forKey:kField1Key];
  3. [aCoderencodeObject:field2forKey:kField2Key];
  4. [aCoderencodeObject:field3forKey:kField3Key];
  5. [aCoderencodeObject:field4forKey:kField4Key];
  6. -(id)initWithCoder:(NSCoder*)aDecoder
  7. if(self=[superinit]){
  8. self.field1=[aDecoderdecodeObjectForKey:kField1Key];
  9. self.field2=[aDecoderdecodeObjectForKey:kField2Key];
  10. self.field3=[aDecoderdecodeObjectForKey:kField3Key];
  11. self.field4=[aDecoderdecodeObjectForKey:kField4Key];
  12. returnself;
  13. -(id)copyWithZone:(NSZone*)zone
  14. FourLines*copy=[[[selfclass]allocWithZone:zone]init];
  15. field1=[self.field1copy];
  16. field2=[self.field2copy];
  17. field3=[self.field3copy];
  18. field4=[self.field4copy];
  19. returncopy;
  20. @end

copy
    #definekFilename@"archive"
  1. #definekDataKey@"Data"
  2. interfacePersistenceViewController:UIViewController
  3. IBOutletUITextField*field1;
  4. IBOutletUITextField*field2;
  5. IBOutletUITextField*field3;
  6. IBOutletUITextField*field4;
  7. -(NSString*)dataFilePath;
  8. void)applicationWillTerminate:(NSNotification*)notifacation;
  9. copy
      #import"ViewController.h"
    1. #import"FourLines.h"
    2. @implementationPersistenceViewController
    3. return[documentsDirectorystringByAppendingPathComponent:kFilename];
    4. void)applicationWillTerminate:(NSNotification*)notifacation
    5. FourLines*fourLines=[[FourLinesalloc]init];
    6. fourLines.field1=field1.text;
    7. fourLines.field2=field2.text;
    8. fourLines.field3=field3.text;
    9. fourLines.field4=field4.text;
    10. NSMutableData*data=[[NSMutableDataalloc]init];
    11. NSKeyedArchiver*archiver=[[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
    12. [archiverencodeObject:fourLinesforKey:kDataKey];
    13. [archiverfinishEncoding];
    14. [datawriteToFile:[selfdataFilePath]atomically:YES];
    15. [fourLinesrelease];
    16. [archiverrelease];
    17. [datarelease];
    18. NSData*data=[[NSMutableDataalloc]initWithContentsOfFile:[selfdataFilePath]];
    19. NSKeyedUnarchiver*unarchiver=[[NSKeyedUnarchiveralloc]initForReadingWithData:data];
    20. FourLines*fourLines=[unarchiverdecodeObjectForKey:kDataKey];
    21. field1.text=fourLines.field1;
    22. field2.text=fourLines.field2;
    23. field3.text=fourLines.field3;
    24. field4.text=fourLines.field4;
    25. [unarchiverrelease];
    26. UIApplication*app=[UIApplicationsharedApplication];
    27. [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(applicationWillTerminate:)name:UIApplicationWillTerminateNotificationobject:app];
    28. [superviewDidLoad];
    29. void)dealloc{
    30. [field1release];
    31. [field2release];
    32. [field3release];
    33. [field4release];
    34. [superdealloc];
    35. @end

    sqlite3

    打开数据库

    sqlite3 *database;

    int result = sqlite3_open("/path/to/database/file",&database);

    如果result等于sqlITE_OK,则表示数据库已经成功打开。sqlite3是采用可以移植的C,所有它不知道什么是NSString,所幸,,有一个NSString方法,该方法从NSString实例生成C字符串

    char *cStringPath = [pathString UTF8String];

    关闭数据库

    sqlite3_close(database)

    创建表


    查询


    结果集


    copy
      #import"/usr/include/sqlite3.h"
    1. #definekFilename@"data.sqlite3"
    2. sqlite3*database;
    3. copy
        for(inti=1;i<=4;i++){
      1. NSString*fieldName=[[NSStringalloc]initWithFormat:@"field%d",i];
      2. UITextField*field=[selfvalueForKey:fieldName];
      3. [fieldNamerelease];
      4. NSString*update=[[NSStringalloc]initWithFormat:@"insertorreplaceintofields(row,field_data)values(%d,'%@');",i,field.text];
      5. char*errorMsg;
      6. if(sqlite3_exec(database,[updateUTF8String],NULL,&errorMsg)!=sqlITE_OK){
      7. NSAssert(0,@"Errorupdatingtable:%s",errorMsg);
      8. sqlite3_free(errorMsg);
      9. sqlite3_close(database);
      10. if(sqlite3_open([[selfdataFilePath]UTF8String],&database)!=sqlITE_OK){
      11. NSAssert(0,@"");
      12. NSString*createsql=@"createtableifnotexistfields(rowintegerprimarykey,field_datatext);";
      13. sqlUTF8String],&errorMsg)!=sqlITE_OK){
      14. sqlite3_close(database);
      15. "");
      16. NSString*query=@"selectrow,field_datafromfieldsorderbyrow";
      17. sqlite3_stmt*statement;
      18. if(sqlite3_prepare_v2(database,[queryUTF8String],-1,&statement,nil)==sqlITE_OK){
      19. while(sqlite3_step(statement)==sqlITE_OK){
      20. introw=sqlite3_column_int(statement,0);
      21. char*rowData=(char*)sqlite3_column_text(statement,1);
      22. NSString*fieldValue=[[NSStringalloc]initWithUTF8String:rowData];
      23. UITextField*field=[selfvalueForKey:fieldName];
      24. field.text=fieldValue;
      25. [fieldValuerelease];
      26. sqlite3_finalize(statement);
      27. @end

      猜你在找的Sqlite相关文章