标签:
//
// SqlManager.m
// 0427Ibook
//
// Created by jerehedu on 15/4/29.
// Copyright (c) 2015年 jerehedu. All rights reserved.
//
#import "SqlManager.h"
#import "Book.h"
@implementation SqlManager
/**
* 获取链接
* @return 链接对象
*/
+ (sqlite3 *) getConn{
//0 定义链接
static sqlite3 * sqlite;
if(sqlite==nil){
//1> 获取沙盒Documents路径
NSString * path=kPath;
NSLog(@"%@",kPath);
path =[path stringByAppendingPathComponent:@"book.sqlite"];
//2> 如果文件存在,直接打开链接,如果不存在创建并且打开
if(SQLITE_OK==sqlite3_open(path.UTF8String, &sqlite)){
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
}
return sqlite;
}
/**
* 执行语句
*
* @param sql 执行的sql
* @param tips 提示
*
* @return 是否成功
*/
+ (BOOL) exec:(NSString * ) sql withTips:(NSString *) tips{
//1 获取链接
sqlite3 * sqlite=[SqlManager getConn];
//2 执行语句
char *errmsg;
if(SQLITE_OK==sqlite3_exec(sqlite, sql.UTF8String, nil, nil, &errmsg)){
NSLog(@"%@成功",tips);
return YES;
}else{
NSLog(@"%@失败,原因%s",tips,errmsg);
return NO;
}
}
/**
* 查询出人员信息
*
* @param sql <#sql description#>
*
* @return <#return value description#>
*/
+ (NSArray *) getAllBookWithSql:(NSString * ) sql{
//1 获取链接
sqlite3 * sqlite=[SqlManager getConn];
//2 查询数据
NSMutableArray *array=[NSMutableArray array];
sqlite3_stmt *stmt;
if(SQLITE_OK== sqlite3_prepare_v2(sqlite, sql.UTF8String, -1, &stmt, nil)){
//遍历当前结果集,并且取出数据
while (sqlite3_step(stmt)==SQLITE_ROW) {
//取出结果集数据
int bookId= sqlite3_column_int(stmt, 0);
const unsigned char * bookName= sqlite3_column_text(stmt, 1);
const unsigned char * summary= sqlite3_column_text(stmt, 2);
int readNum= sqlite3_column_int(stmt, 3);
int doller= sqlite3_column_int(stmt, 5);
const unsigned char * writer= sqlite3_column_text(stmt, 4);
const unsigned char * bookPic= sqlite3_column_text(stmt, 6);
//将结果集数据放入数组
Book *book=[[Book alloc] init];
book.book_id=bookId;
book.book_name=[NSString stringWithUTF8String:(const char *) bookName];
book.summary=[NSString stringWithUTF8String:(const char *) summary];
book.book_pic=[NSString stringWithUTF8String:(const char *) bookPic];
book.writer=[NSString stringWithUTF8String:(const char *)writer];
book.doller=doller;
book.read_num=readNum;
[array addObject:book];
}
}else{
NSLog(@"查询失败");
}
return array;
}
/**
* 判断是不是存在
*
* @param sql <#sql description#>
*
* @return 是否存在
*/
+ (BOOL) isExistWithSql:(NSString * ) sql{
//1 获取链接
sqlite3 * sqlite=[SqlManager getConn];
//2 查询数据
sqlite3_stmt *stmt;
if(SQLITE_OK== sqlite3_prepare_v2(sqlite, sql.UTF8String, -1, &stmt, nil)){
//遍历当前结果集,并且取出数据
while (sqlite3_step(stmt)==SQLITE_ROW) {
//取出结果集数据
int count= sqlite3_column_int(stmt, 0);
return count>0;
}
}else{
NSLog(@"查询失败");
}
sqlite3_finalize(stmt);
return NO;
}
/**
* 获取账号信息
*
* @return <#return value description#>
*/
+ (NSString *) getAccount{
//将登录的人员信息写入沙盒
NSString *path=[kPath stringByAppendingPathComponent:@"user.tt"];
NSFileManager * manager =[NSFileManager defaultManager];
NSData * data=[manager contentsAtPath:path];
NSString * account=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return account;
}
+(void)closeConn{
sqlite3 * sqlite=[SqlManager getConn];
sqlite3_close(sqlite);
sqlite=nil;
}
@end
标签:
原文地址:http://www.cnblogs.com/yy515700/p/4520673.html