为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

第12章:数据持久性存储

2012-03-17 22页 pdf 1002KB 11阅读

用户头像

is_242982

暂无简介

举报
第12章:数据持久性存储 ��� � ������ ����� �� ���� � �� �������(SQLite3) Core Data(��� ����� ��) • • • • �26�/4 .��26�@$+��Doucuments)��B�B0�#8?:'&3=����B "+! ;������9�Documents5��tmp5 � • • • • Applications�����iPhone� �!&�#���/�Application��� �/��$��(�#������/...
第12章:数据持久性存储
��� � ������ ����� �� ���� � �� �������(SQLite3) Core Data(��� ����� ��) • • • • �26�/4 .��26�@$+��Doucuments)��B<)� ��;2 � �26�3'& Documents 2 � �26�'& tmp 2 � �26�3�*)� �,;�6���1%� � B�A;-7�9�� 5�3�(>�B0�#8?:'&3=����B "+! ;������9�Documents5��tmp5 � • • • • Applications�����iPhone� �!&�#���/�Application��� �/��$��(�#������/��#���*-#�'�.+��%,� "XCode) �#.+�����#�����,�����!&��3���� (Documents , tmp , Library) . Documents*���0� 1,.�Documents*�0� ���3 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsPath = [paths objectAtIndex:0]; %3 'NSSearchPathForDirectoriesInDomains(C�� "�*�2NSDocumentDirectory/�#�"� Documents*�(0�2�1NSUserDomainMask���!�+�2. *���12'���,-�&��$)- ��� ���3 NSString *documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 2,.�Documents*��(� 0� NSString *filePath = [documentsPath stringByAppendingPathComponent:@"data.db"]; ��tmp���� � 1,��tmp�� � NSString *tempPath = NSTemporaryDirectory(); 2,��tmp��� � � NSString *filePath = [tempPath stringByAppendingPathComponent:@"data.db"]; NSString���1�&�$ -lastPathComponent:�1�'���������(�"� 0*/6) stringByAppendingPathComponent:%�����7�� 1�& � pathExtension:3��- pathComponents:3���):4��) ���1�&# �)�5 9 stringByStandardizingPath: 81�� �&�(� (2.��+,!) • • • • • NSBundle ��� [[NSBundle mainBundle]loadNibNamed:@"nib���" owner:self options:nil]��� nib����������NSArray� [[NSBundle mainBundle]pathForResource:@"���" ofType:@"����"]���� ����� �������� �NSString� [[NSBundle mainBundle] resourcePath] ������ ���� �NSString� • • • '�������')��� 1 0������������� -(BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag &1 1 . NSArray-NSDictionary-NSString,"+��%0��,���+��%�� ���� 2 . $# ������� 0��� !����.�����(/ 2 0�����*��� -(id)initWithContentsOfFile:(NSString *)path ��SQLite�� ���� ������libsqlite3.0.dylib � ������FMDB � • • FMDB �$FMDB�� (https://github.com/ccgus/fmdb) " src ��������#��!� (%fmdb.m) • • FMDB�iOS� ��SQLite����&��� ���������� FMDB ����� FMDatabase������ FMResultSet��� ���� • • FMDatabase�$�# 7databaseWithPath:"����������FMDatabase�/ 9open:�����8�&���2�5� ������� 8 �) ��6 9setShouldCacheStatements:-('�5�*-(�YES6 9close:�3���8�3���1� 9executeUpdate:�+�4 4�%SQL���,%SQL 9executeQuery:�+!.%SQL50���FMResultSet%� /6 • • • • • • demo1����� ����� ��� FMDatabase *db = [FMDatabase databaseWithPath:@"/hello.db"]; [db open];//���� ������fmdatabase ����� �� [db setShouldCacheStatements:YES]; [db executeUpdate:@"create table t1 (a integer,b text)"]; [db executeUpdate:@"insert into t1 values(1,'hello world')"]; [db close]; demo2� �10��� for(int i=9;i<20;i++) { //� � [db executeUpdate:@"insert into t1 values( ? , 'hello world')",[NSNumber numberWithInt:i]]; //� ���sql NSString *sql = [NSString stringWithFormat:@"insert into t1 values(%d,'hello world')",i]; [db executeUpdate:sql]; } ����������� ����� demo3����� FMResultSet *rs = [db executeQuery:@"select * from t1"]; while ([rs next]) { NSLog(@"%d",[rs intForColumn:@"a"]); NSLog(@"%@",[rs stringForColumn:@"b"]); } demo4� � �� ���� ������� FMResultSet *rs = [db executeQuery:@"select * from t1"]; NSMutableArray *array = [NSMutableArray arrayWithCapacity:20]; while ([rs next]) { NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:5]; [dictionary setObject:[NSNumber numberWithInt:[rs intForColumn:@"a"]] forKey:@"a"]; [dictionary setObject:[rs stringForColumn:@"b"] forKey:@"b"]; [array addObject:dictionary]; [dictionary release]; } //���� //NSLog(@"%@",array); for(NSDictionary *temp in array) { NSLog(@"%@",[temp objectForKey:@"a"]); NSLog(@"%@",[temp objectForKey:@"b"]); } FMResultSet ��� next�� ������ intForColumn������������int� � stringForColumn� �� intForColumn doubleForColumn� �� intForColumn dataForColumn� �� intForColumn • • • • • NSFileManager����� +defaultManager:����������� � –fileExistsAtPath:������ � –createFileAtPath:contents:attributes:���� �� –removeItemAtPath:error:���� –copyItemAtPath:toPath:error: ��� • • • • • ������UIApplication� UIApplication *application = [UIApplication sharedApplication]; ������UIApplicationDelegate� id delegate = [application delegate]; � id delegate = [[UIApplication sharedApplication]delegate]; � ���� * ���&$� ��� �� ��� � � �*!���� '� )!��� � '�� �% � *( #" • • • [db beginTransaction];//��� [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)"]; [db executeUpdate:@"insert into test (a, b, c, d, e) values (?, ?, ?, ?, ?)"]; [db commit];//��� � [db rollback];//�� �������� FMDatabase *db = [FMDatabase databaseWithPath:@"/hello.db"]; [db open]; [db setShouldCacheStatements:YES]; [db executeUpdate:@"create table t1 (a text, b blob)"]; NSData *data = [NSData dataWithContentsOfFile:@"/g.png"]; [db executeUpdate:@"insert into t1 values (?,?)",@"hello",data]; FMResultSet *rs = [db executeQuery:@"select * from t1"]; if ([rs next]) { NSData *temp = [rs dataForColumn:@"b"]; [temp writeToFile:[@"~/Desktop/s.png" stringByStandardizingPath] atomically:NO]; }
/
本文档为【第12章:数据持久性存储】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索