iPhone開發 第三方SQLITE封裝庫Pldatabase
iPhone開發 第三方SQLITE封裝庫Pldatabase是本文要介紹的內容,不多說,我們先來看內容。花了三周時間,把原來使用原生SqliteAPI寫的代碼都改成了PLSqliteDatabase的操作,下載解壓后把framework導入到項目中. 項目中需要sqlite.dylib,不然無法鏈接成功.。
pldatabase的網站地址:http://plsqlite.narod.ru/http://code.google.com/p/pldatabase/ 在這里可以下載和查看文檔和代碼.
下面我翻譯一下其最簡單的入門知識,在項目過程中, 發現這些其實也夠用, 但異常處理這些我還沒引進來使用.
基本使用指南
創建一個鏈接
為存在數據庫文件打開一個鏈接:
- 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");
查詢操作
執行查詢操作可以使用 -[PLDatabase executeQuery:]. 該操作返回結果集是一個對象為PLResult的NSObject實例.使用方法如下
- 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"]);
- }
- // 如果沒有關閉結果集不會導致內存泄漏, 但會結果集會被保留直到下一次的查詢
- [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"]);
- }
- // 如果沒有關閉結果集不會導致內存泄漏, 但會結果集會被保留直到下一次的查詢
- [results close];
執行準備
PLPreparedStatement支持SQL操作的預編譯和參數優先綁定. 執行準備的操作可以調用:-[PLDatabase prepareStatement:].
- id<PLPreparedStatemet> stmt = [db prepareStatement: @"INSERT INTO example (name, color) VALUES (?, ?)"];
- // 綁定參數 [stmt bindParameters: [NSArray arrayWithObjects: @"Widget", @"Blue", nil]];
- // 執行插入 if ([stmt executeUpdate] == NO) NSLog(@"INSERT failed");
基于命名參數的綁定
當參數很多的時候, 能過命名參數綁定的可讀性強很多
用法如下:
- // 準備
- id<PLPreparedStatement> stmt = [db prepareStatement: @"INSERT INTO test (name, color) VALUES (:name, :color)"];
- // 使用字典綁定參數
- NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity: 2];
- [parameters setObject: @"Widget" forKey: @"name"];
- [parameters setObject: @"Blue" forKey: @"color"];
- [stmt bindParameterDictionary: parameters];
- // 執行插入
- if ([stmt executeUpdate] == NO)
- NSLog(@"INSERT failed");
- // 準備
- id<PLPreparedStatement> stmt = [db prepareStatement: @"INSERT INTO test (name, color) VALUES (:name, :color)"];
- // 使用字典綁定參數
- NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity: 2];
- [parameters setObject: @"Widget" forKey: @"name"];
- [parameters setObject: @"Blue" forKey: @"color"];
- [stmt bindParameterDictionary: parameters];
- // 執行插入
- if ([stmt executeUpdate] == NO)
- NSLog(@"INSERT failed");
小結:詳解第三方SQLITE封裝庫Pldatabase的內容介紹完了,關于PLDatabase的基本操作也完了. 希望本文對你有所幫助。