iPhone開發(fā) 第三方SQLITE封裝庫Pldatabase
iPhone開發(fā) 第三方SQLITE封裝庫Pldatabase是本文要介紹的內(nèi)容,不多說,我們先來看內(nèi)容?;巳軙r(shí)間,把原來使用原生SqliteAPI寫的代碼都改成了PLSqliteDatabase的操作,下載解壓后把framework導(dǎo)入到項(xiàng)目中. 項(xiàng)目中需要sqlite.dylib,不然無法鏈接成功.。
pldatabase的網(wǎng)站地址:http://plsqlite.narod.ru/http://code.google.com/p/pldatabase/ 在這里可以下載和查看文檔和代碼.
下面我翻譯一下其最簡(jiǎn)單的入門知識(shí),在項(xiàng)目過程中, 發(fā)現(xiàn)這些其實(shí)也夠用, 但異常處理這些我還沒引進(jìn)來使用.
基本使用指南
創(chuàng)建一個(gè)鏈接
為存在數(shù)據(jù)庫文件打開一個(gè)鏈接:
- PLSqliteDatabase *db = [[PLSqliteDatabase alloc] initWithPath: @"/path/to/database"];
- if (![db open])
- NSLog(@"Could not open database");
- PLSqliteDatabase *db = [[PLSqliteDatabase alloc] initWithPath: @"/path/to/database"];
- if (![db open])
- NSLog(@"Could not open database");
更新操作(即沒有返回記錄集)
更新操作可以使用 -[PLDatabase executeUpdate:]
- if (![db executeUpdate: @"CREATE TABLE example (id INTEGER)"])
- NSLog(@"Table creation failed");
- if (![db executeUpdate: @"INSERT INTO example (id) VALUES (?)", [NSNumber numberWithInteger: 42]])
- NSLog(@"Data insert failed");
- if (![db executeUpdate: @"CREATE TABLE example (id INTEGER)"])
- NSLog(@"Table creation failed");
- if (![db executeUpdate: @"INSERT INTO example (id) VALUES (?)", [NSNumber numberWithInteger: 42]])
- NSLog(@"Data insert failed");
查詢操作
執(zhí)行查詢操作可以使用 -[PLDatabase executeQuery:]. 該操作返回結(jié)果集是一個(gè)對(duì)象為PLResult的NSObject實(shí)例.使用方法如下
- id<PLResultSet> results = [db executeQuery: @"SELECT id FROM example WHERE id = ?", [NSNumber numberWithInteger: 42]];
- while ([results next]) {
- NSLog(@"Value of column id is %d", [results intForColumn: @"id"]);
- }
- // 如果沒有關(guān)閉結(jié)果集不會(huì)導(dǎo)致內(nèi)存泄漏, 但會(huì)結(jié)果集會(huì)被保留直到下一次的查詢
- [results close];
- id<PLResultSet> results = [db executeQuery: @"SELECT id FROM example WHERE id = ?", [NSNumber numberWithInteger: 42]];
- while ([results next]) {
- NSLog(@"Value of column id is %d", [results intForColumn: @"id"]);
- }
- // 如果沒有關(guān)閉結(jié)果集不會(huì)導(dǎo)致內(nèi)存泄漏, 但會(huì)結(jié)果集會(huì)被保留直到下一次的查詢
- [results close];
執(zhí)行準(zhǔn)備
PLPreparedStatement支持SQL操作的預(yù)編譯和參數(shù)優(yōu)先綁定. 執(zhí)行準(zhǔn)備的操作可以調(diào)用:-[PLDatabase prepareStatement:].
- id<PLPreparedStatemet> stmt = [db prepareStatement: @"INSERT INTO example (name, color) VALUES (?, ?)"];
- // 綁定參數(shù) [stmt bindParameters: [NSArray arrayWithObjects: @"Widget", @"Blue", nil]];
- // 執(zhí)行插入 if ([stmt executeUpdate] == NO) NSLog(@"INSERT failed");
基于命名參數(shù)的綁定
當(dāng)參數(shù)很多的時(shí)候, 能過命名參數(shù)綁定的可讀性強(qiáng)很多
用法如下:
- // 準(zhǔn)備
- id<PLPreparedStatement> stmt = [db prepareStatement: @"INSERT INTO test (name, color) VALUES (:name, :color)"];
- // 使用字典綁定參數(shù)
- NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity: 2];
- [parameters setObject: @"Widget" forKey: @"name"];
- [parameters setObject: @"Blue" forKey: @"color"];
- [stmt bindParameterDictionary: parameters];
- // 執(zhí)行插入
- if ([stmt executeUpdate] == NO)
- NSLog(@"INSERT failed");
- // 準(zhǔn)備
- id<PLPreparedStatement> stmt = [db prepareStatement: @"INSERT INTO test (name, color) VALUES (:name, :color)"];
- // 使用字典綁定參數(shù)
- NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity: 2];
- [parameters setObject: @"Widget" forKey: @"name"];
- [parameters setObject: @"Blue" forKey: @"color"];
- [stmt bindParameterDictionary: parameters];
- // 執(zhí)行插入
- if ([stmt executeUpdate] == NO)
- NSLog(@"INSERT failed");
小結(jié):詳解第三方SQLITE封裝庫Pldatabase的內(nèi)容介紹完了,關(guān)于PLDatabase的基本操作也完了. 希望本文對(duì)你有所幫助。