iPhone开发 第三方SQLITE封装库Pldatabase

移动开发 iOS
本文介绍的是iPhone开发 第三方SQLITE封装库Pldatabase,很详细的介绍了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"); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

更新操作(即没有返回记录集)

更新操作可以使用 -[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"); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

查询操作

执行查询操作可以使用 -[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]; 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

执行准备

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"); 
  • 1.
  • 2.
  • 3.

基于命名参数的绑定

当参数很多的时候, 能过命名参数绑定的可读性强很多

用法如下:

// 准备    
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"); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

小结:详解第三方SQLITE封装库Pldatabase的内容介绍完了,关于PLDatabase的基本操作也完了. 希望本文对你有所帮助。

责任编辑:zhaolei 来源: 互联网
相关推荐

2019-07-30 11:35:54

AndroidRetrofit

2011-08-15 17:20:25

iPhone应用Sqlite3FMDB

2015-11-05 16:44:37

第三方登陆android源码

2012-03-01 20:42:12

iPhone

2011-08-05 16:50:00

iPhone 数据 Sqlite

2014-07-22 10:56:45

Android Stu第三方类库

2014-07-23 08:55:42

iOSFMDB

2022-01-14 09:57:14

鸿蒙HarmonyOS应用

2010-03-03 15:10:49

第三方Python库

2013-08-14 09:50:32

iOS类库

2022-05-23 13:50:20

开发封装

2019-09-03 18:31:19

第三方支付电商支付行业

2021-10-11 06:38:52

Go开源库语言

2009-12-31 14:38:34

Silverlight

2017-12-11 15:53:56

2016-10-21 14:09:10

2011-07-25 16:22:44

iPhone RegexKitLi 表达式

2012-04-03 13:42:03

iPhone

2009-03-12 15:32:49

WEB开发

2014-02-10 10:22:33

微信开发者
点赞
收藏

51CTO技术栈公众号