// // SaveOrReadArr.m // JiSuanQi // // Created by 杨杰 on 2017/7/11. // Copyright © 2017年 向礼. All rights reserved. // #import "SaveOrReadArr.h" #import "FMDatabase.h" #define HMHistoryDealsFile [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"history_deals.data"] #define listcontatcsCatch [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"listcontatcsCatchx.data"] #define groundTypeCatch [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"groundTypeXX.data"] #define saveFriendnumCatch [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"saveFriendnum.data"] #define saveFrienTempCatch [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"saveTempFriendnum.data"] #define saveNewFriendXXCatch [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"saveNewFriendXXCatch.data"] @implementation SaveOrReadArr +inserinto:(NSString *)userId msgModel:(JXMsgAndUserObject *)msg{ NSString * docPath=[[NSString alloc]init]; docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];//获取沙河地址 NSLog(@"docPath==%@",docPath); NSString * fileName=[docPath stringByAppendingPathComponent:@"student333.sqlite"];//设置数据库名称 FMDatabase * fmdb=[FMDatabase databaseWithPath:fileName];//创建并获取数据库信息 if ([fmdb open]) { NSLog(@"数据库打开成功!"); }else{ NSLog(@"数据库打开失败!"); } // PRIMARY KEY AUTOINCREMENT 基键 并且自增 BOOL executeUpdate = [fmdb executeUpdate:@"CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, userId text NOT NULL,messageId text NOT NULL,typeName text NOT NULL);"]; if (executeUpdate) { NSLog(@"创建表成功"); } else { NSLog(@"创建表失败"); } NSString *messageId=msg.message.messageId; NSString *typeName =@""; BOOL result = [fmdb executeUpdate:@"INSERT INTO t_student(userId,json) VALUES (?,?);" withArgumentsInArray:@[userId, messageId, typeName]]; if (result) { NSLog(@"插入成功"); } else { NSLog(@"插入失败"); } //查询整个表 //FMResultSet * resultSet = [fmdb executeQuery:@"select * from t_student"]; //根据条件查询 FMResultSet * resultSet = [fmdb executeQuery:@"select * from t_student where id < ?", @(4)]; // 遍历结果集合 while ([resultSet next]) { // NSString *name = [resultSet objectForColumn:@"name"]; int age = [resultSet intForColumn:@"age"]; // NSString *sex = [resultSet objectForColumn:@"sex"]; } } //----------- + (void)initSerializationTempFriend:(NSMutableArray *)array{ // 1,创建一个空的data NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0]; // 2,创建一个序列化器(编码器) 给它一个空的data,用来存放编码之后的数据 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // 3,用序列化器来编码数组 [archiver encodeObject:array]; // 4,结束编码 [archiver finishEncoding]; // 此时data已经不空了 // 5,把data写入文件 // NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/saveTempFriendnum.plist"]; [data writeToFile:saveFrienTempCatch atomically:YES]; } + (NSMutableArray *)initNSKeyedTempFriendUnarchiver{ NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:saveFrienTempCatch]; // 2,创建一个反序列化器(解码器) NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 3,从反序列化器中解码出对象 NSMutableArray *array = [unarchiver decodeObject]; // 4,结束解码 [unarchiver finishDecoding]; // 5,使用数组 // NSLog(@"array--%@",array); return array; } + (void *)removeOjbTemp:(NSString *)fromUseId{ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/saveTempFriendnum.plist"]; // 创建文件管理对象 NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExist = [manager isExecutableFileAtPath:path]; if (isExist) { BOOL isDelete = [manager removeItemAtPath:path error:nil]; }else{} } + (void)initSerializationFriendArray:(NSMutableArray *)array{ // 1,创建一个空的data NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0]; // 2,创建一个序列化器(编码器) 给它一个空的data,用来存放编码之后的数据 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // 3,用序列化器来编码数组 [archiver encodeObject:array]; // 4,结束编码 [archiver finishEncoding]; // 此时data已经不空了 // 5,把data写入文件 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:saveNewFriendXXCatch]; [data writeToFile:path atomically:YES]; } +(void *)removeOjb:(NSString *)fromUseId{ NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/saveFriendnum.plist"]; // 创建文件管理对象 NSFileManager *manager = [NSFileManager defaultManager]; BOOL isExist = [manager isExecutableFileAtPath:path]; if (isExist) { BOOL isDelete = [manager removeItemAtPath:path error:nil]; }else{} } + (NSMutableArray *)initNSKeyedFriendUnarchiver{ NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:saveNewFriendXXCatch]; // 2,创建一个反序列化器(解码器) NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 3,从反序列化器中解码出对象 NSMutableArray *array = [unarchiver decodeObject]; // 4,结束解码 [unarchiver finishDecoding]; // 5,使用数组 // NSLog(@"array--%@",array); return array; } + (void)initSerializationArray:(NSMutableArray *)array{ // 1,创建一个空的data NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0]; // 2,创建一个序列化器(编码器) 给它一个空的data,用来存放编码之后的数据 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // 3,用序列化器来编码数组 [archiver encodeObject:array]; // 4,结束编码 [archiver finishEncoding]; // 此时data已经不空了 // 5,把data写入文件 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/LoginData.plist"]; [data writeToFile:path atomically:YES]; } + (NSMutableArray *)initNSKeyedUnarchiver{ // 1,把文件 读到data中 NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/LoginData.plist"]; NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:path]; // 2,创建一个反序列化器(解码器) NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 3,从反序列化器中解码出对象 NSMutableArray *array = [unarchiver decodeObject]; // 4,结束解码 [unarchiver finishDecoding]; // 5,使用数组 // NSLog(@"array--%@",array); return array; } //=========================== + (void)initSerializationArrayHuiLv2222:(NSMutableArray *)array{ // 1,创建一个空的data NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0]; // 2,创建一个序列化器(编码器) 给它一个空的data,用来存放编码之后的数据 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // 3,用序列化器来编码数组 [archiver encodeObject:array]; // 4,结束编码 [archiver finishEncoding]; // 此时data已经不空了 // 5,把data写入文件 [data writeToFile:groundTypeCatch atomically:YES]; } + (NSMutableArray *)initNSKeyedUnarchiverHuiLv222{ NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:listcontatcsCatch]; // 2,创建一个反序列化器(解码器) NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 3,从反序列化器中解码出对象 NSMutableArray *array = [unarchiver decodeObject]; // 4,结束解码 [unarchiver finishDecoding]; // 5,使用数组 // NSLog(@"array--%@",array); return array; } ///======================================================================== + (NSMutableArray *)initNSKeyedUnarchiverHuiLv{ // 1,把文件 读到data中 NSMutableData *data = [[NSMutableData alloc] initWithContentsOfFile:listcontatcsCatch]; // 2,创建一个反序列化器(解码器) NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; // 3,从反序列化器中解码出对象 NSMutableArray *array = [unarchiver decodeObject]; // 4,结束解码 [unarchiver finishDecoding]; // 5,使用数组 // NSLog(@"array--%@",array); return array; } + (void)initSerializationArrayHuiLv:(NSMutableArray *)array{ // 1,创建一个空的data NSMutableData *data = [[NSMutableData alloc] initWithCapacity:0]; // 2,创建一个序列化器(编码器) 给它一个空的data,用来存放编码之后的数据 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; // 3,用序列化器来编码数组 [archiver encodeObject:array]; // 4,结束编码 [archiver finishEncoding]; // 此时data已经不空了 // 5,把data写入文件 [data writeToFile:listcontatcsCatch atomically:YES]; } @end