ARC还是手工管理内存
在你的cocoa项目中,可以任意地使用,fmd在编译选项中判断作出正确的选择
There are three main classes in FMDB:
FMDatabase
- Represents a single SQLite database. Used for executing SQL statements.FMResultSet
- Represents the results of executing a query on an FMDatabase
.FMDatabaseQueue
- If you're wanting to perform queries and updates on multiple threads, you'll want to use this class. It's described in the "Thread Safety" section below. An FMDatabase
is created with a path to a SQLite database file. This path can be one of these three:
@""
). An empty database is created at a temporary location. This database is deleted with the FMDatabase
connection is closed.NULL
. An in-memory database is created. This database will be destroyed with the FMDatabase
connection is closed.CREATE
, PRAGMA
, UPDATE
,INSERT
, ALTER
, COMMIT
, BEGIN
, DETACH
, DELETE
, DROP
, END
, EXPLAIN
, VACUUM
这些函数的返回是一个BOOL,如果是NO。检查更多NSError的信息
对于Select,就需要用Resultset去接收返回的records:
FMResultSet *s = [db executeQuery:@"SELECT * FROM myTable"];
while ([s next]) {
//retrieve values for each record
}
语法看起来也很简单,遍历,然后获取每行中各字段的值(有一大堆方法/体力活):
intForColumn:
longForColumn:
longLongIntForColumn:
boolForColumn:
doubleForColumn:
stringForColumn:
dateForColumn:
dataForColumn:
dataNoCopyForColumn:
UTF8StringForColumnIndex:
objectForColumn:
- (IBAction)doButton:(id)sender { NSFileManager* fm = [[NSFileManager alloc] init]; NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"]; [fm removeItemAtPath:dbpath error:nil]; // in case we already did this once FMDatabase* db = [FMDatabase databaseWithPath:dbpath]; if (![db open]) { NSLog(@"Ooops"); return; } [db executeUpdate:@"create table people (lastname text, firstname text)"]; [db beginTransaction]; [db executeUpdate:@"insert into people (firstname, lastname) values ('Matt', 'Neuburg')"]; [db executeUpdate:@"insert into people (firstname, lastname) values ('Snidely', 'Whiplash')"]; [db executeUpdate:@"insert into people (firstname, lastname) values ('Dudley', 'Doright')"]; [db commit]; NSLog(@"I think I created it"); [db close]; } - (IBAction)doButton2:(id)sender { NSString* docsdir = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSString* dbpath = [docsdir stringByAppendingPathComponent:@"people.db"]; FMDatabase* db = [FMDatabase databaseWithPath:dbpath]; if (![db open]) { NSLog(@"Ooops"); return; } FMResultSet *rs = [db executeQuery:@"select * from people"]; while ([rs next]) { NSLog(@"%@ %@", [rs stringForColumn:@"firstname"], [rs stringForColumn:@"lastname"]); } [db close]; }
感谢This is an Objective-C wrapper around SQLite: http://sqlite.org/:
https://github.com/ccgus/fmdb