#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数据库中的一些操作的更多相关文章

  1. MySQL 数据库中的基础操作

    数据库中的表操作 1.创建表 表的表名命名规则: -- 数据库表命名规则重要说明: -- (1)数据库表名称可以支持大写字母A-Z,小写字母a-z,数字0-9,字符包括下划线 _ ,可以组合使用; - ...

  2. JDBC上关于数据库中多表操作一对多关系和多对多关系的实现方法

    黑马程序员 我们知道,在设计一个Javabean的时候,要把这些BEAN 的数据存放在数据库中的表结构,然而这些数据库中的表直接又有些特殊的关系,例如员工与部门直接有一对多的关系,学生与老师直接又多对 ...

  3. oracle数据库中常见的操作语句(一)

    一 创建表空间 create tablespace lfdc_data logging datafile 'D:\Database\lfdc_data.dbf' size 50m autoextend ...

  4. mongodb 数据库中 的聚合操作

  5. sql 指定数据库中的信息操作

    查是否有该表名 SELECT * FROM sys.objects WHERE name='表名'查表字段的信息select * from syscolumns where id=Object_Id( ...

  6. 图解数据库中的join操作

    1.所有的join都从cross join衍生而来 2.所有join图示 转自Say NO to Venn Diagrams When Explaining JOINs

  7. 数据库中DDL、DML、DCL和TCP概念

    1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...

  8. 涂抹mysql笔记-数据库中的权限体系

    涂抹mysql笔记-数据库中的权限体系<>能不能连接,主机名是否匹配.登陆使用的用户名和密码是否正确.mysql验证用户需要检查3项值:用户名.密码和主机来源(user.password. ...

  9. 数据库mysql的常规操作

    1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...

随机推荐

  1. PV UV IP含义及区别

    --------首先来看看ip.uv和pv的定义---------- PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次. UV(独立访客):即Unique Vis ...

  2. 【bzoj1053】反素数

    [bzoj1053]反素数 题意 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x,则称x为反质数.例 ...

  3. AndroidManifest.xml清单文件要点

    AndroidManifest.xml AndroidManifest.xml清单文件通常包括如下内容: 应用程序的包名,该包名作为该应用的唯一标识. 应用包含的组件,如Activity,Servic ...

  4. OpenGL 简介

    OpenGL是一个底层图形库规范.它为程序员提供了一个小的几何图元(点.线.多边形.图片和位图)库和一个支持2D/3D几何对象绘图命令库,通过所提供的图元和命令来控制对象的呈现(绘图). 由于Open ...

  5. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  6. JavaScript实现五子棋的界面设计

    五子棋的界面设计包括绘制棋盘.绘制棋子及黑白棋轮流落子. 涉及的知识点主要有canvas绘制直线.设置画笔颜色:canvas画圆.填充渐变色 1.绘制棋盘 先设定棋盘的宽.高:然后利用for循环,根据 ...

  7. java学习之协调同步的线程

    当一个线程使用的同步方法中用到某个变量,而此变量有需要其他线程修改后才能符合本线程的需要, 那么可以在同步方法中使用wait(),wait方法可以中断线程的执行,使本线程等待,暂时让出CPU的使用权, ...

  8. Unique Paths II [LeetCode]

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  10. struts2视频学习笔记 28(OGNL表达式)

    课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...