���
� ������
����� ��
����
� ��
�������(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];
}