1. 怎么导入PlausibleDatabase.framework框架
先下载PlausibleDatabase.framework框架包,然后把该包直接加入到库中,然后再加入#import <PlausibleDatabase/PlausibleDatabase.h>头文件
下载地址:http://code.google.com/p/pldatabase/ 在这里可以下载和查看文档和代码.
第三方SQLITE封装库Pldatabase
基本使用指南
创建一个链接
为存在数据库文件打开一个链接:
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");
查询操作
执行查询操作可以使用 -[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];
执行准备
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");
连接sqlite数据库的代码
#import <Foundation/Foundation.h>
#import <PlausibleDatabase/PlausibleDatabase.h>
@interface DataBase : NSObject {
}
+ (PLSqliteDatabase *) setup;
+ (void) close;
@end
#import "DataBase.h"
#import <PlausibleDatabase/PlausibleDatabase.h>
static PLSqliteDatabase * dbPointer;
@implementation DataBase
//单例
+ (PLSqliteDatabase *) setup{
if (dbPointer) {
return dbPointer;
}
NSLog(@"%@",NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES));
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *realPath = [documentPath stringByAppendingPathComponent:@"book.sqlite"];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"Book" ofType:@"sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:realPath]) {
NSError *error;
if (![fileManager copyItemAtPath:sourcePath toPath:realPath error:&error]) {
NSLog(@"%@",[error localizedDescription]);
}
}
NSLog(@"复制sqlite到路径:%@成功。",realPath);
//把dbpointer地址修改为可修改的realPath。
dbPointer = [[PLSqliteDatabase alloc] initWithPath:realPath];
[dbPointer open];
return dbPointer;
}
+ (void) close{
if (dbPointer) {
[dbPointer close];
dbPointer = NULL;
}
}
@end
PLSqliteDatabase *dataBase = [DataBase setup];
id<PLResultSet>rs;
rs = [dataBase executeQuery:@"SELECT *FROM Book"];
//定义一个数组存放所有书籍的信息
NSMutableArray *books = [[NSMutableArray alloc]init];
//把rs中的数据库信息遍历到books数组中
while ([rs next]) {
NSString *ID = [rs objectForColumn:@"ID"];
NSString *name = [rs objectForColumn:@"Name"];
NSString *author = [rs objectForColumn:@"Author"];
NSString *publishHouse = [rs objectForColumn:@"PublishHouse"];
NSString *date = [rs objectForColumn:@"Date"];
NSString *briefIntroducation = [rs objectForColumn:@"BriefIntroducation"];
//初始化book存放到books里面
Book *book = [[Book alloc]initWithID:ID name:name publishHouse:publishHouse author:author briefIntroducation:briefIntroducation date:date];
[books addObject:book];
}
[rs close];
return books;
}