#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
//查找table所有书籍的方法
+ (NSArray *) findAll{
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];
[book release];
}
//关闭数据库
[rs close];
return books;
}
//根据ID搜索相关书籍的方法
+ (id) find:(NSInteger) ID{
PLSqliteDatabase *dataBase = [DataBase setup];
id<PLResultSet> rs;
NSString *findSql = [NSString stringWithFormat:@"select * FROM Book WHERE ID = %d",ID];
rs = [dataBase executeQuery:findSql];
Book *book = nil;
if([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 = [[Book alloc] initWithID:ID name:name publishHouse:publishHouse author:author briefIntroducation:briefIntroducation date:date];
}
else {
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can not find!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
NSLog(@"Error!!Can not find.");
[errorAlert release];
}
[rs close];
return [book autorelease];
}
//查询当前有本书的方法
+ (int) count{
PLSqliteDatabase *dataBase = [DataBase setup];
int bookCount = 0;
id<PLResultSet>rs;
rs = [dataBase executeQuery:@"SELECT COUNT(*) AS EventCount FROM Event"];
if ([rs next]) {
bookCount = [[rs objectForColumn:@"BookCount"] intValue];
}
[rs close];
return bookCount;
}
//修改某一本书籍的方法
+ (int) update:(NSString *)ID withName:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = -1;
@try {
[dataBase beginTransaction];
bResult = [dataBase executeUpdate: @"UPDATE Book SET name = ?, publishHouse = ?, author = ?, briefIntroducation = ?, date = ?",
ID,
name,
publishHouse,
author,
briefIntroducation,
date];
[dataBase commitTransaction];
}
@catch (NSException *exception) {
[dataBase rollbackTransaction];
}
@finally {
}
return bResult;
}
//创建新书籍的方法
+ (int) createWithID:(NSString *) ID name:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date1{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = [dataBase executeUpdate: @"INSERT INTO Book (ID, Name, PublishHouse, Author, BriefIntroducation, Date) VALUES (?,?,?,?,?,?)",
ID,
name,
publishHouse,
author,
briefIntroducation,
date1];
return bResult;
}
//删除书籍的方法
+ (int) delete:(NSString *) ID{
PLSqliteDatabase *dataBase = [DataBase setup];
BOOL bResult = [dataBase executeUpdate:@"DELETE FROM Book WHERE ID = ?",
ID];
return bResult;
}
//书籍这个类的初始化方法
- (id) initWithID:(NSString *) ID name:(NSString *) name publishHouse:(NSString *) publishHouse author:(NSString *) author briefIntroducation:(NSString *)briefIntroducation date:(NSString *) date{
if (self = [super init]) {
_id = [ID retain];
_name = [name retain];
_publishHouse = [publishHouse retain];
_author = [author retain];
_briefIntroducation = [briefIntroducation retain];
_date = [date retain];
}
return self;
}