标签:ios 数据库 sqlite 锁死 database is closed
在数据库创建时应判断数据是否存在(不为空nil)
if(sqlite==nil){// 判断数据库是否存在,若存在就不创建,若不存在才创建,以防数据库锁死
if (SQLITE_OK == sqlite3_open(dpPath.UTF8String , &sqlite)) {
NSLog(@"数据库创建成功");
}else{
NSLog(@"数据库创建失败");
}
}
#import "SqlManager.h"
#import "ScrollImage.h"
#import "MyTableViewCell.h"
#include "HeadPageCell.h"
@interface SqlManager ()
{
NSMutableArray * _array;
NSMutableArray * _arrayForHeadCell;
}
@end
@implementation SqlManager
#pragma mark - 创建数据库
+ (sqlite3 *)createSql{
static sqlite3 * sqlite;
// 1.获取数据库document文件地址
NSString * appPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",appPath);
//2.拼接新建数据库路径
NSString * dpPath = [appPath stringByAppendingPathComponent:@"allUsers.sqlite"];
if(sqlite==nil){// 判断数据库是否存在,若存在就不创建,若不存在才创建,以防数据库锁死
if (SQLITE_OK == sqlite3_open(dpPath.UTF8String , &sqlite)) {
NSLog(@"数据库创建成功");
}else{
NSLog(@"数据库创建失败");
}
}
return sqlite;
}
#pragma mark - 创建table,增加、删除、修改、添加table数据(抽成一个方法)
+ (void)fixTableDataSentence:(NSString *)sentence{
sqlite3 * sqlite = [self createSql];
char * errmsg;
if (SQLITE_OK == sqlite3_exec(sqlite, sentence.UTF8String, nil, nil, &errmsg)) {
NSLog(@"创建成功");
}else{
NSLog(@"%s",errmsg);
}
/*
// 2.插入数据(增),因为设置id为自增,所以不输入直接为1
insert into t_class (class_name,person_count) values(‘class1‘,10)
// 3.删除数据(删)
// table里的全删 deleta from t_class
// 删除指定id=5的数据 delete from t_class where class_id=5
// 4.修改数据
name全改成newclassname update t_class set class_name=‘newclassname‘
修改某一行的某个数据 update t_class set class_name=‘newclassname‘ where class_id=7
*/ //增删改语句
}
#pragma mark - 查询数据
- (BOOL)queryDataSentence:(NSString *)sentence{
sqlite3_stmt * stmt;// 结果集(查询数据的结果存放的地方)
sqlite3 * sqlite = [SqlManager createSql];
// 遍历结果集
if(SQLITE_OK == sqlite3_prepare_v2(sqlite, sentence.UTF8String, -1, &stmt, nil)){
// 1.遍历当前结果集,并取出数据
_array = [NSMutableArray array];// 初始化可变数组
while (sqlite3_step(stmt)==SQLITE_ROW ) {
int count = sqlite3_column_int(stmt, 0);
NSLog(@"%i",count);
return count==0;
// int personId = sqlite3_column_int(stmt, 0);// 0第一列
// const unsigned char * personName = sqlite3_column_text(stmt, 1);
// const unsigned char * psw = sqlite3_column_text(stmt, 2);
// NSLog(@"%i========%s========%s",personId,personName,psw);
/*
// 创建模型
Person * per = [[Person alloc]init];
// 给模型赋值
per.Id = personId;
per.account = [NSString stringWithUTF8String:(const char *)personName];
per.psw = [NSString stringWithUTF8String:(const char *)psw];
// 将模型加入可变数组
[_personArray addObject:per];
*/
}
}else{
NSLog(@"账号重复");
}
// NSLog(@"%@",_personArray);
sqlite3_finalize(stmt);
return NO;
}
#pragma mark - 查询数据调用数据
- (NSArray * )useSqliteData{
sqlite3 * sqlite = [SqlManager createSql];
sqlite3_stmt * stmt;// 结果集(查询数据的结果存放的地方)
NSString * querySql = @"select * from t_scroll";
// sqlite3_step(stmt)==SQLITE_ROW 一步步的走到结果集
if (SQLITE_OK == sqlite3_prepare_v2(sqlite, querySql.UTF8String, -1, &stmt, nil)) {
// 1.便利当前结果集,并且取出数据
_array = [NSMutableArray array];
while (sqlite3_step(stmt)==SQLITE_ROW) {
int Id = sqlite3_column_int(stmt, 0);// 0 第一列
const unsigned char * path = sqlite3_column_text(stmt, 1);
const unsigned char * desc = sqlite3_column_text(stmt, 2);
NSLog(@"%i======%s========%s",Id,path,desc);
ScrollImage * scroll = [[ScrollImage alloc]init];
scroll.Id = Id ;
scroll.imagePath = [NSString stringWithUTF8String:(const char *)path];
scroll.imageDecs = [NSString stringWithUTF8String:(const char *)desc];
// Person * per = [[Person alloc]init];
// per.name =[NSString stringWithUTF8String:(const char *)personName];
// scroll.Id = personId;
// per.psw = [NSString stringWithUTF8String:(const char *)psw] ;
[_array addObject:scroll];
}
}else{
NSLog(@"查询失败");
}
NSLog(@"%@",_array);
sqlite3_finalize(stmt);
return (NSArray *)_array;
}
#pragma mark - 查询数据调用数据
- (NSArray * )useSqliteDataForHeadCell{
sqlite3 * sqlite = [SqlManager createSql];
sqlite3_stmt * stmt;// 结果集(查询数据的结果存放的地方)
NSString * querySql = @"select * from t_headCell";
// sqlite3_step(stmt)==SQLITE_ROW 一步步的走到结果集
_arrayForHeadCell = [NSMutableArray array];// 可变数组初始化
if (SQLITE_OK == sqlite3_prepare_v2(sqlite, querySql.UTF8String, -1, &stmt, nil)) {
// 1.便利当前结果集,并且取出数据
while (sqlite3_step(stmt)==SQLITE_ROW) {
int Id = sqlite3_column_int(stmt, 0);// 0 第一列
const unsigned char * path = sqlite3_column_text(stmt, 1);
const unsigned char * title = sqlite3_column_text(stmt, 2);
const unsigned char * subtitle = sqlite3_column_text(stmt, 3);
const unsigned char * content = sqlite3_column_text(stmt, 4);
int nums = sqlite3_column_int(stmt, 5);
HeadPageCell * cell = [[HeadPageCell alloc]init];
cell.Id = Id ;
cell.imagePath = [NSString stringWithUTF8String:(const char *)path];
cell.title = [NSString stringWithUTF8String:(const char *)title];
cell.subTitle = [NSString stringWithUTF8String:(const char *)subtitle];
cell.content = [NSString stringWithUTF8String:(const char *)content];
cell.nums = nums;
[_arrayForHeadCell addObject:cell];
}
}else{
NSLog(@"查询失败");
}
NSLog(@"%@",_arrayForHeadCell);
return (NSArray *)_arrayForHeadCell;
}
#pragma mark - 关闭数据库文件
+ (void)closeSql{
sqlite3 * sqlite = [self createSql];
sqlite3_close(sqlite);
// sqlite = nil;
}
@end
ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios
sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防
ios sqlite3数据库的封装-自己封装的,针对锁死database is closed的解决和预防
标签:ios 数据库 sqlite 锁死 database is closed
原文地址:http://blog.csdn.net/zx6268476/article/details/45555845