FMDB数据库中的一些操作
#pragma mark - 数据库的操作
- (BOOL)judgeModel:(TaskResourceModel *)model isInArray:(NSArray *)shopArray
{
for (TaskResourceModel *shopModel in shopArray) {
if ([shopModel.resourceId isEqualToString:model.resourceId]) {
return YES;
}
}
return NO;
}
//初始化数据库数据
- (void)initTableDatas
{
//创建table
[self creatSqliteTable];
NSMutableArray *purchaceArray = [self selectPurchaceSqliteOfAllEntity];
for (TaskResourceModel *model in purchaceArray) {
NSMutableArray *shopArray = [self selectSqliteOfAllEntity];
if (![self judgeModel:model isInArray:shopArray]) {
[self insertEntityOfSourceStr:[model toJSONString] andSourceId:model.resourceId];
}
}
}
- (NSString *)sqlitePath
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * doc = documentPath;
NSString * path;
User *user = [[User alloc] getUserInformation];
switch ([self.productCategoryId integerValue]) {
case 69:
{
NSString *userSqlitePath = [NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_wc.sqlite"];
path = [doc stringByAppendingPathComponent:userSqlitePath];
break;
}
case 68:
{
NSString *userSqlitePath = [NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_sina.sqlite"];
path = [doc stringByAppendingPathComponent:userSqlitePath];
break;
}
case 88:
{
NSString *userSqlitePath = [NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_qq.sqlite"];
path = [doc stringByAppendingPathComponent:userSqlitePath];
break;
}
default:
break;
}
return path;
}
- (NSString *)purchaseSqlitePath
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * doc = documentPath;
NSString * path = [doc stringByAppendingPathComponent:@"taskResource.sqlite"];
return path;
}
//插
- (void)insertEntityOfSourceStr:(NSString *)sourceStr andSourceId:(NSString *)sourceId
{
NSFileManager * fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:[self sqlitePath]];
// if (![fileManager fileExistsAtPath:[self sqlitePath]]){
[self creatSqliteTable];
// }
// 'sourceId' TEXT, 'sourceModelStr'
FMDatabase * db = [FMDatabase databaseWithPath:[self sqlitePath]];
if ([db open]) {
NSString *insertSql1= [NSString stringWithFormat:
@"INSERT INTO 'TaskSource' ('%@', '%@') VALUES ('%@', '%@')",
@"sourceId", @"sourceModelStr", sourceId, sourceStr];
BOOL res = [db executeUpdate:insertSql1];
if (!res) {
NSLog(@"error when insert db table");
} else {
NSLog(@"success to insert db table");
}
[db close];
}
}
//查所有的
- (NSMutableArray *)selectPurchaceSqliteOfAllEntity
{
NSMutableArray *array = [NSMutableArray array];
FMDatabase * db = [FMDatabase databaseWithPath:[self purchaseSqlitePath]];
if ([db open]) {
NSString *sql = @"SELECT * FROM TaskSource";
// NSString * sql = @"select * from user";
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
NSString *json = [rs stringForColumn:@"sourceModelStr"];
TaskResourceModel *model = [[TaskResourceModel alloc] initWithString:json error:nil];
[array addObject:model];
}
[db close];
}
return array;
}
//查所有的
- (NSMutableArray *)selectSqliteOfAllEntity
{
NSMutableArray *array = [NSMutableArray array];
FMDatabase * db = [FMDatabase databaseWithPath:[self sqlitePath]];
if ([db open]) {
NSString *sql = @"SELECT * FROM TaskSource";
// NSString * sql = @"select * from user";
FMResultSet * rs = [db executeQuery:sql];
while ([rs next]) {
NSString *json = [rs stringForColumn:@"sourceModelStr"];
TaskResourceModel *model = [[TaskResourceModel alloc] initWithString:json error:nil];
[array addObject:model];
}
[db close];
}
return array;
}
//删除
- (void)deletePurchaceSqliteOfSourceId:(NSString *)sourceId
{
FMDatabase * db = [FMDatabase databaseWithPath:[self purchaseSqlitePath]];
if ([db open]) {
NSString *sql = [NSString stringWithFormat:@"delete from TaskSource where sourceId = '%@'",sourceId];
// NSString * sql = @"select * from user";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
}
//删除
- (void)deleteSqliteOfSourceId:(NSString *)sourceId
{
FMDatabase * db = [FMDatabase databaseWithPath:[self sqlitePath]];
if ([db open]) {
NSString *sql = [NSString stringWithFormat:@"delete from TaskSource where sourceId = '%@'",sourceId];
// NSString * sql = @"select * from user";
BOOL res = [db executeUpdate:sql];
if (!res) {
NSLog(@"error when delete db table");
} else {
NSLog(@"success to delete db table");
}
[db close];
}
}
//查特定sourceid
- (TaskResourceModel *)selectSqliteOfSourceId:(NSString *)sourceId
{
FMDatabase * db = [FMDatabase databaseWithPath:[self sqlitePath]];
NSString *json;
if ([db open]) {
json = [db stringForQuery:@"SELECT sourceModelStr FROM TaskSource WHERE sourceId = ?",sourceId];
//// NSString * sql = @"select * from user";
// FMResultSet * rs = [db executeQuery:sql];
// while ([rs next]) {
// NSString *json = [rs stringForColumn:@"sourceModelStr"];
//
// model = [[TaskResourceModel alloc] initWithString:json error:nil];
//// return
// }
[db close];
}
TaskResourceModel *model = [[TaskResourceModel alloc] initWithString:json error:nil];
return model;
}
- (void)creatSqliteTable
{
NSFileManager * fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:[self sqlitePath]];
// if (flag == NO) {
// create it
FMDatabase * db = [FMDatabase databaseWithPath:[self sqlitePath]];
if ([db open]) {
// NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT, '%@' INTEGER, '%@' TEXT)",TABLENAME,ID,NAME,AGE,ADDRESS];
// BOOL res = [db executeUpdate:sqlCreateTable];
NSString * sql = @"CREATE TABLE IF NOT EXISTS 'TaskSource' ('sourceId' TEXT, 'sourceModelStr' TEXT)";
BOOL res = [db executeUpdate:sql];
if (!res) {
// debugLog(@"error when creating db table");
} else {
// debugLog(@"succ to creating db table");
}
[db close];
} else {
// debugLog(@"error when open db");
}
// }
}
+ (void)deleteAllSqliteData
{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * doc = documentPath;
// NSString *userSqlitePath = [NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_qq.sqlite"];
// User *user = [[User alloc] getUserInformation];
// NSString * wcPath = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_wc.sqlite"]];
// NSString * sinaPath = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_sina.sqlite"]];
// NSString * qqPath = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@",user.userAccountId,@"shopTaskResource_qq.sqlite"]];
// FMDatabase * db = [FMDatabase databaseWithPath:wcPath];
// if ([db open]) {
// NSString *sql = [NSString stringWithFormat:@"delete from TaskSource "];
// // NSString * sql = @"select * from user";
// BOOL res = [db executeUpdate:sql];
// if (!res) {
// NSLog(@"error when delete db table");
// } else {
// NSLog(@"success to delete db table");
// }
// [db close];
// }
//
// FMDatabase * sinaDb = [FMDatabase databaseWithPath:sinaPath];
// if ([sinaDb open]) {
// NSString *sql = [NSString stringWithFormat:@"delete from TaskSource "];
// // NSString * sql = @"select * from user";
// BOOL res = [sinaDb executeUpdate:sql];
// if (!res) {
// NSLog(@"error when delete db table");
// } else {
// NSLog(@"success to delete db table");
// }
// [sinaDb close];
// }
//
// FMDatabase * qqDb = [FMDatabase databaseWithPath:qqPath];
// if ([qqDb open]) {
// NSString *sql = [NSString stringWithFormat:@"delete from TaskSource "];
// // NSString * sql = @"select * from user";
// BOOL res = [qqDb executeUpdate:sql];
// if (!res) {
// NSLog(@"error when delete db table");
// } else {
// NSLog(@"success to delete db table");
// }
// [qqDb close];
// }
//
// NSString *purchasePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// NSString * doc = documentPath;
NSString * purchasePath = [documentPath stringByAppendingPathComponent:@"taskResource.sqlite"];
// FMDatabase * purchaseDb = [FMDatabase databaseWithPath:purchasePath];
// if ([purchaseDb open]) {
// NSString *sql = [NSString stringWithFormat:@"delete from TaskSource "];
// // NSString * sql = @"select * from user";
// BOOL res = [purchaseDb executeUpdate:sql];
// if (!res) {
// NSLog(@"error when delete db table");
// } else {
// NSLog(@"success to delete db table");
// }
// [purchaseDb close];
// }
NSFileManager * fileManager = [NSFileManager defaultManager];
//// if ([fileManager fileExistsAtPath:wcPath]){
// NSError *error;
// [fileManager removeItemAtPath:wcPath error:&error];
//// NSLog(@"-------:%@",error);
//// }
//// if ([fileManager fileExistsAtPath:sinaPath]){
// [fileManager removeItemAtPath:sinaPath error:nil];
//// }
//// if ([fileManager fileExistsAtPath:qqPath]){
// [fileManager removeItemAtPath:qqPath error:nil];
//// }
//// if ([fileManager fileExistsAtPath:purchasePath]){
[fileManager removeItemAtPath:purchasePath error:nil];
// }
}
FMDB数据库中的一些操作的更多相关文章
- MySQL 数据库中的基础操作
数据库中的表操作 1.创建表 表的表名命名规则: -- 数据库表命名规则重要说明: -- (1)数据库表名称可以支持大写字母A-Z,小写字母a-z,数字0-9,字符包括下划线 _ ,可以组合使用; - ...
- JDBC上关于数据库中多表操作一对多关系和多对多关系的实现方法
黑马程序员 我们知道,在设计一个Javabean的时候,要把这些BEAN 的数据存放在数据库中的表结构,然而这些数据库中的表直接又有些特殊的关系,例如员工与部门直接有一对多的关系,学生与老师直接又多对 ...
- oracle数据库中常见的操作语句(一)
一 创建表空间 create tablespace lfdc_data logging datafile 'D:\Database\lfdc_data.dbf' size 50m autoextend ...
- mongodb 数据库中 的聚合操作
- sql 指定数据库中的信息操作
查是否有该表名 SELECT * FROM sys.objects WHERE name='表名'查表字段的信息select * from syscolumns where id=Object_Id( ...
- 图解数据库中的join操作
1.所有的join都从cross join衍生而来 2.所有join图示 转自Say NO to Venn Diagrams When Explaining JOINs
- 数据库中DDL、DML、DCL和TCP概念
1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...
- 涂抹mysql笔记-数据库中的权限体系
涂抹mysql笔记-数据库中的权限体系<>能不能连接,主机名是否匹配.登陆使用的用户名和密码是否正确.mysql验证用户需要检查3项值:用户名.密码和主机来源(user.password. ...
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
随机推荐
- SVN操作手册
目 录 第1章 简介 1 第2章 SVN服务端 2 2.1 安装VisualSVN 2 2.2 VisualSVN服务 3 2.3 版本库 4 2.3.1 创建版本库 ...
- java视频格式转换代码
http://blog.163.com/zzf_fly/blog/static/20958915820127217443816/ package com.gkzx.online.action; imp ...
- hibernate4.3.10使用注解映射方式样例
1.调用例子 import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.boot.re ...
- Android 应用开发耗电量控制。。
当程序启动手机越多的模块,那耗电就越快 当你的程序运行时只占用CPU的时候,这时候耗电量是最少的. 当然这时候如果cpu的运行速度很慢那是最好的.. 程序耗电量控制首要从下面3个方面抓起: 1.频繁的 ...
- C# Socket编程(1)基本的术语和概念
计算机程序能够相互联网,相互通讯,这使一切都成为可能,这也是当今互联网存在的基础.那么程序是如何通过网络相互通信的呢?这就是我记录这系列的笔记的原因.C#语言从一开始就是为了互联网而设计的,它为实现程 ...
- 《Java程序设计》第四周学习总结
20145224-陈颢文 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 ·继承就是面向对象中,子类继承父类,避免重复的行为定义.重复再程序设计上是非常不好的信号 ...
- 《构建之法》第8、9、10章读后感和Sprint总结
<构建之法>第8.9.10章读后感 第八章重点讲了需求分析,在一个项目中,需求分析是最基础也是最重要的,只有充分了解了用户需求,我们才不会走弯路,才能做出正确的规划,保证项目的进行是按照 ...
- 笔记3:关于VBS整人代码的浅谈
今天又看到有人在群里刷屏了.就想到了以前玩过的发QQ骚扰信息程序了.其实蛮简单的 和网上很多的整人代码差不多 一.直接在网上搜索“VBS整人代码”,然后找到有用的代码复制着. ps:在网上有很多有意思 ...
- Andriod使用webview控件往APP里内嵌网页
转自博文:http://www.cnblogs.com/JuneZhang/p/4148542.html 1.布局文件片段:res-layout <WebView android:id=&quo ...
- DOM之兄弟节点
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...