FMDB使用

FMDB 简单的使用

 GitHub链接地址:https://github.com/ccgus/fmdb

需要引入的文件有:

并导入libsqlite2.dylib

 
View Code
 1  1     //  Library/Caches
 2  2     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
 3  3     NSString *cachePath = [paths lastObject];
 4  4     NSLog(@"%@", cachePath);
 5  5     
 6  6     // new a database
 7  7     NSString *dbPath = [cachePath stringByAppendingPathComponent:@"MyDatabase.db"];
 8  8     FMDatabase *db = [FMDatabase databaseWithPath:dbPath];
 9  9     if (![db open]) {
10 10         NSLog(@"Could not open db.");
11 11         return;
12 12     }
13 13     
14 14     // create a table
15 15 //    NSLog(@"%d", [db executeUpdate:@"create table PersonList (Name text, Age integer, Sex integer, Phone text, Address text, Photo blob)"]);
16 16     
17 17     // insert data
18 18 //    NSLog(@"%d", [db executeUpdate:@"insert into PersonList (Name, Age, Sex, Phone, Address, Photo) values (?, ?, ?, ?, ?, ?) ",
19 19 //                  @"Lawrence", [NSNumber numberWithInt:20],[NSNumber numberWithInt:0], @"091234567", @"Jiangsu, NJ", nil]);
20 20     
21 21     // delete data
22 22 //    NSLog(@"%d", [db executeUpdate:@"delete from PersonList where name = ?", @"Lawrence"]);
23 23     
24 24     // updata data
25 25 //    NSLog(@"%d", [db executeUpdate:@"update PersonList set Age = ? where Name = ?", [NSNumber numberWithInt:23], @"Jone"]);
26 26     
27 27     // search data
28 28 //    FMResultSet *rs = [db executeQuery:@"select Name, Age from PersonList"];
29 29 //    while ([rs next]) {
30 30 //        NSString *name = [rs stringForColumn:@"Name"];
31 31 //        int age = [rs intForColumn:@"Age"];
32 32 //        NSLog(@"name = %@, age = %d", name, age);
33 33 //    }
34 34 //    [rs close];
35 35 
36 36 //    NSString *address = [db stringForQuery:@"select Address from PersonList where Name = ?", @"Lawrence"];
37 37 //    
38 38 //    int age = [db intForQuery:@"select Age from PersonList where Name = ?", @"Lawrence"];
39 39 //    
40 40 //    NSLog(@"address = %@, age = %d", address, age);

 

 
 
posted @ 2012-10-30 14:52  Lawerence  阅读(244)  评论(0编辑  收藏  举报