标签:
1.SQLite,为个人应用程序和设备提供本地数据存储。SQLite,强调经济性,效率性,可靠性,独立性,和简单。SQLite的竞争对手fopen()函数。
2.SQLite only supports one writer at a time per database file.But in most cases, a write transaction only takes milliseconds and so multiple writers can simply take turns.SQLite will handle more write concurrency that many people suspect.Nevertheless, client/server database systems, because they have a long-running server process at hand to coordinate access, can usually handle far more write concurrency than SQLite ever will.
官方文档中摘下来的,大概是说 sqlite 在工作的时候是锁库的。
3.用法:
(1)三个主要的类
FMDatabase
-代表一个单一的SQLite数据库。用于执行SQL语句。
FMResultSet
-代表了在执行查询的结果FMDatabase。
FMDatabaseQueue -如果你想在多个线程执行查询和更新,你会想使用这个类。
(2)创建
FMDatabase *db = [FMDatabase databaseWithPath:@"/tmp/tmp.db"];
if(![db open]) { [db release]; return ; }
(3) 多条语句批次处理
NSString *sql = @"create table bulktest1 (id integer primary key autoincrement, x text);"
"create table bulktest2 (id integer primary key autoincrement, y text);"
"create table bulktest3 (id integer primary key autoincrement, z text);"
"insert into bulktest1 (x) values (‘XXX‘);"
"insert into bulktest2 (y) values (‘YYY‘);"
"insert into bulktest3 (z) values (‘ZZZ‘);";
success = [db executeStatements:sql];
sql = @"select count(*) as count from bulktest1;"
"select count(*) as count from bulktest2;"
"select count(*) as count from bulktest3;";
success = [self.db executeStatements:sql withResultBlock:^int(NSDictionary *dictionary) {
NSInteger count = [dictionary[@"count"] integerValue];
XCTAssertEqual(count, 1, @"expected one record for dictionary %@", dictionary);
return 0;
}];
(4)数据处理
INSERT INTO myTable的VALUES(?,?,?)
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", @"this has \" lots of ‘ bizarre \" quotes ‘"];
“?” 插入数值的占位符
另外,也可以使用命名参数语法:
INSERT INTO myTable的VALUES(:ID,:姓名,:值)
NSDictionary *argsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"My Name", @"name", nil];
[db executeUpdate:@"INSERT INTO myTable (name) VALUES (:name)" withParameterDictionary:argsDict];
提供给所有的参数-executeUpdate:方法(或任何接受该变种的va_list作为参数)必须是对象。下面将不起作用(并会导致崩溃):
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", 42];
正确的应该是:
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:42]];
或者
[db executeUpdateWithFormat:@"INSERT INTO myTable VALUES (%d)", 42];
(5)在多线程中,使用FMDatabase的实例并不是明智的做法,这时候就应该想到FMDatabaseQueue了。
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:aPath];
[queue inDatabase:^(FMDatabase *db) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
FMResultSet *rs = [db executeQuery:@"select * from foo"];
while ([rs next]) {
…
}
}];
[queue inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:1]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:2]];
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:3]];
if (whoopsSomethingWrongHappened) {
*rollback = YES;
return;
}
// etc…
[db executeUpdate:@"INSERT INTO myTable VALUES (?)", [NSNumber numberWithInt:4]];
}];
标签:
原文地址:http://blog.csdn.net/learnios/article/details/44341607