iOS中原生的SQLite API在使用上至關不友好,在使用時,很是不便。因而,就出現了一系列將SQLite API進行封裝的庫,例如FMDB、PlausibleDatabase、sqlitepersistentobjects等,FMDB (https://github.com/ccgus/fmdb) 是一款簡潔、易用的封裝庫,這一篇文章簡單介紹下FMDB的使用。
android
在FMDB下載文件後,工程中必須導入以下文件,並使用 libsqlite3.dylib 依賴包。git
FMDB同時兼容ARC和非ARC工程,會自動根據工程配置來調整相關的內存管理代碼。
github
FMDatabase : 一個單一的SQLite數據庫,用於執行SQL語句。
FMResultSet :執行查詢一個FMDatabase結果集,這個和android的Cursor相似。
FMDatabaseQueue :在多個線程來執行查詢和更新時會使用這個類。
sql
[cpp] view plaincopyprint?數據庫
db = [FMDatabase databaseWithPath:database_path]; 安全
一、當數據庫文件不存在時,fmdb會本身建立一個。多線程
二、 若是你傳入的參數是空串:@"" ,則fmdb會在臨時文件目錄下建立這個數據庫,數據庫斷開鏈接時,數據庫文件被刪除。閉包
三、若是你傳入的參數是 NULL,則它會創建一個在內存中的數據庫,數據庫斷開鏈接時,數據庫文件被刪除。
app
[cpp] view plaincopyprint?async
[db open]
返回BOOL型。
[cpp] view plaincopyprint?
[db close]
除了查詢操做,FMDB數據庫操做都執行executeUpdate方法,這個方法返回BOOL型。
看一下例子:
[cpp] view plaincopyprint?
if ([db open]) {
NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
BOOL res = [db executeUpdate:sqlCreateTable];
if (!res) {
NSLog(@"error when creating db table");
} else {
NSLog(@"success to creating db table");
}
[db close];
}
[cpp] view plaincopyprint?
if ([db open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"張三", @"13", @"濟南"];
BOOL res = [db executeUpdate:insertSql1];
NSString *insertSql2 = [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES ('%@', '%@', '%@')",
TABLENAME, NAME, AGE, ADDRESS, @"李四", @"12", @"濟南"];
BOOL res2 = [db executeUpdate:insertSql2];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
[cpp] view plaincopyprint?
if ([db open]) {
NSString *updateSql = [NSString stringWithFormat:
@"UPDATE '%@' SET '%@' = '%@' WHERE '%@' = '%@'",
TABLENAME, AGE, @"15" ,AGE, @"13"];
BOOL res = [db executeUpdate:updateSql];
if (!res) {
NSLog(@"error when update db table");
} else {
NSLog(@"success to update db table");
}
[db close];
}
[cpp] view plaincopyprint?
if ([db open]) {
NSString *deleteSql = [NSString stringWithFormat:
@"delete from %@ where %@ = '%@'",
TABLENAME, NAME, @"張三"];
BOOL res = [db executeUpdate:deleteSql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
查詢操做使用了executeQuery,並涉及到FMResultSet。
[cpp] view plaincopyprint?
if ([db open]) {
NSString * sql = [NSString stringWithFormat:
@"SELECT * FROM %@",TABLENAME];
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
int Id = [rs intForColumn:ID];
NSString * name = [rs stringForColumn:NAME];
NSString * age = [rs stringForColumn:AGE];
NSString * address = [rs stringForColumn:ADDRESS];
NSLog(@"id = %d, name = %@, age = %@ address = %@", Id, name, age, address);
}
[db close];
}
FMDB的FMResultSet提供了多個方法來獲取不一樣類型的數據:
若是應用中使用了多線程操做數據庫,那麼就須要使用FMDatabaseQueue來保證線程安全了。 應用中不可在多個線程中共同使用一個FMDatabase對象操做數據庫,這樣會引發數據庫數據混亂。 爲了多線程操做數據庫安全,FMDB使用了FMDatabaseQueue,使用FMDatabaseQueue很簡單,首先用一個數據庫文件地址來初使化FMDatabaseQueue,而後就能夠將一個閉包(block)傳入inDatabase方法中。 在閉包中操做數據庫,而不直接參與FMDatabase的管理。
[cpp] view plaincopyprint?
FMDatabaseQueue * queue = [FMDatabaseQueue databaseQueueWithPath:database_path];
dispatch_queue_t q1 = dispatch_queue_create("queue1", NULL);
dispatch_queue_t q2 = dispatch_queue_create("queue2", NULL);
dispatch_async(q1, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db2) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",
TABLENAME, NAME, AGE, ADDRESS];
NSString * name = [NSString stringWithFormat:@"jack %d", i];
NSString * age = [NSString stringWithFormat:@"%d", 10+i];
BOOL res = [db2 executeUpdate:insertSql1, name, age,@"濟南"];
if (!res) {
NSLog(@"error to inster data: %@", name);
} else {
NSLog(@"succ to inster data: %@", name);
}
}];
}
});
dispatch_async(q2, ^{
for (int i = 0; i < 50; ++i) {
[queue inDatabase:^(FMDatabase *db2) {
NSString *insertSql2= [NSString stringWithFormat:
@"INSERT INTO '%@' ('%@', '%@', '%@') VALUES (?, ?, ?)",
TABLENAME, NAME, AGE, ADDRESS];
NSString * name = [NSString stringWithFormat:@"lilei %d", i];
NSString * age = [NSString stringWithFormat:@"%d", 10+i];
BOOL res = [db2 executeUpdate:insertSql2, name, age,@"北京"];
if (!res) {
NSLog(@"error to inster data: %@", name);
} else {
NSLog(@"succ to inster data: %@", name);
}
}];
}
});