iOS中原生的SQLite API在使用上相當不友好,在使用時坟瓢,非常不便勇边。于是,就出現(xiàn)了一系列將SQLite API進行封裝的庫折联,例如FMDB粒褒、PlausibleDatabase、sqlitepersistentobjects等诚镰,F(xiàn)MDB (https://github.com/ccgus/fmdb) 是一款簡潔奕坟、易用的封裝庫祥款,這一篇文章簡單介紹下FMDB的使用。
FMDB同時兼容ARC和非ARC工程月杉,會自動根據(jù)工程配置來調(diào)整相關(guān)的內(nèi)存管理代碼刃跛。
FMDB常用類:
FMDatabase : 一個單一的SQLite數(shù)據(jù)庫,用于執(zhí)行SQL語句苛萎。
FMResultSet :執(zhí)行查詢一個FMDatabase結(jié)果集桨昙,這個和Android的Cursor類似。
FMDatabaseQueue :在多個線程來執(zhí)行查詢和更新時會使用這個類腌歉。
創(chuàng)建數(shù)據(jù)庫:
db = [FMDatabase databaseWithPath:database_path];
1蛙酪、當數(shù)據(jù)庫文件不存在時,fmdb會自己創(chuàng)建一個究履。
2滤否、 如果你傳入的參數(shù)是空串:@"" ,則fmdb會在臨時文件目錄下創(chuàng)建這個數(shù)據(jù)庫最仑,數(shù)據(jù)庫斷開連接時藐俺,數(shù)據(jù)庫文件被刪除。
3泥彤、如果你傳入的參數(shù)是 NULL欲芹,則它會建立一個在內(nèi)存中的數(shù)據(jù)庫,數(shù)據(jù)庫斷開連接時吟吝,數(shù)據(jù)庫文件被刪除菱父。
打開數(shù)據(jù)庫:
[db open]
返回BOOL型。
關(guān)閉數(shù)據(jù)庫:
[db close]
數(shù)據(jù)庫增刪改等操作:
除了查詢操作剑逃,F(xiàn)MDB數(shù)據(jù)庫操作都執(zhí)行executeUpdate方法浙宜,這個方法返回BOOL型。
數(shù)據(jù)庫操作(使用FMDB)
看一下例子:
創(chuàng)建表:
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];
}
添加數(shù)據(jù):
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];
}
修改數(shù)據(jù):
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];
}
刪除數(shù)據(jù):
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];
}
數(shù)據(jù)庫查詢操作:
查詢操作使用了executeQuery蛹磺,并涉及到FMResultSet粟瞬。
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提供了多個方法來獲取不同類型的數(shù)據(jù):
數(shù)據(jù)庫操作(使用FMDB)
數(shù)據(jù)庫多線程操作:
如果應用中使用了多線程操作數(shù)據(jù)庫,那么就需要使用FMDatabaseQueue來保證線程安全了萤捆。 應用中不可在多個線程中共同使用一個FMDatabase對象操作數(shù)據(jù)庫裙品,這樣會引起數(shù)據(jù)庫數(shù)據(jù)混亂。 為了多線程操作數(shù)據(jù)庫安全俗或,F(xiàn)MDB使用了FMDatabaseQueue市怎,使用FMDatabaseQueue很簡單,首先用一個數(shù)據(jù)庫文件地址來初使化FMDatabaseQueue辛慰,然后就可以將一個閉包(block)傳入inDatabase方法中区匠。 在閉包中操作數(shù)據(jù)庫,而不直接參與FMDatabase的管理帅腌。
FMDatabase,---代表了輕量的SQLite數(shù)據(jù)庫驰弄,執(zhí)行數(shù)據(jù)庫的那些語句去使用這個類蝠筑。
FMDatabaseQueue---如果你想在多線程中使用查詢和更新操作,你會用到這個類揩懒。
和FMResultSet---代表了在FMDatabase中執(zhí)行一個查詢操作的結(jié)果。
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);
}
}];
}
});