iOS中 学会如何对sqlite3 进行封装
#waring ---(看官注意) ---使用说明:
①在创建自定义model类之前让该类继承自文件中的Model类,
②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父类Model类
ID属性等于你所选的NSString属性值即可 (具体可参见.下文 二.3 中)
一.文件夹结构:
二.文件说明:
1.NSObject+NSCoding
该文件是对对象类型进行encoding 以及 decoding操作的方法
2.DataHandle
1. 该文件是 sqlite3 单例数据库对Model 对象进行增.删.查.改
3.Model文件夹下面的Model 是遵守NSCoding 协议的底层Model
1. 而Model文件夹下面的TestModel 则是继承自Model 的一种自定义model (也就是我们项目中的自定义的Model),
2.如此,我们自己项目中的自定义model 只要继承字Model 类就可以了,
3.但还要在自定义的TestModel中添加以下改动: self.ID属性是从父类Model中继承来的 而Model 的ID属性被设置为数据库的主键, 因此不可以为空,需要将TestModel的一个属性替代ID属性,既作为主键;
三.各个文件的代码示例
1.NSObject+NSCoding
.h文件
.m文件
#import "NSObject+NSCoding.h"
#import <objc/runtime.h>
#import <UIKit/UIKit.h>
@implementation NSObject (NSCoding)
- (NSMutableDictionary *)propertiesForClass:(Class)klass {
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
unsigned int outCount, i;
//通过运行时获取 该类所有属性列表
objc_property_t *properties = class_copyPropertyList(klass, &outCount);
for(i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *pname = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
NSString *pattrs = [NSString stringWithCString:property_getAttributes(property) encoding:NSUTF8StringEncoding];
pattrs = [[pattrs componentsSeparatedByString:@","] objectAtIndex:0];
pattrs = [pattrs substringFromIndex:1];
[results setObject:pattrs forKey:pname];
}
free(properties);
if ([klass superclass] != [NSObject class]) {
[results addEntriesFromDictionary:[self propertiesForClass:[klass superclass]]];
}
return results;
}
- (NSDictionary *)properties {
return [self propertiesForClass:[self class]];
}
- (void)autoEncodeWithCoder:(NSCoder *)coder {
NSDictionary *properties = [self properties];
for (NSString *key in properties) {
NSString *type = [properties objectForKey:key];
id value;
unsigned long long ullValue;
BOOL boolValue;
float floatValue;
double doubleValue;
NSInteger intValue;
unsigned long ulValue;
long longValue;
unsigned unsignedValue;
short shortValue;
NSString *className;
NSMethodSignature *signature = [self methodSignatureForSelector:NSSelectorFromString(key)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:NSSelectorFromString(key)];
[invocation setTarget:self];
switch ([type characterAtIndex:0]){
case '@': // object
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
#warning UIImage类型的属性不归档 add by yhy›
if ([className isEqualToString:@"UIImage"]) {
//如果属性是UIImage类型的,不进行归档
break;
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
value = [self performSelector:NSSelectorFromString(key)];
#pragma clang diagnostic pop
// only decode if the property conforms to NSCoding
if([class conformsToProtocol:@protocol(NSCoding)]){
[coder encodeObject:value forKey:key];
}
}
break;
case 'c': // bool
[invocation invoke];
[invocation getReturnValue:&boolValue];
[coder encodeObject:[NSNumber numberWithBool:boolValue] forKey:key];
break;
case 'f': // float
[invocation invoke];
[invocation getReturnValue:&floatValue];
[coder encodeObject:[NSNumber numberWithFloat:floatValue] forKey:key];
break;
case 'd': // double
[invocation invoke];
[invocation getReturnValue:&doubleValue];
[coder encodeObject:[NSNumber numberWithDouble:doubleValue] forKey:key];
break;
case 'i': // int
[invocation invoke];
[invocation getReturnValue:&intValue];
[coder encodeObject:[NSNumber numberWithInt:(int)intValue] forKey:key];
break;
case 'L': // unsigned long
[invocation invoke];
[invocation getReturnValue:&ulValue];
[coder encodeObject:[NSNumber numberWithUnsignedLong:ulValue] forKey:key];
break;
case 'Q': // unsigned long long
[invocation invoke];
[invocation getReturnValue:&ullValue];
[coder encodeObject:[NSNumber numberWithUnsignedLongLong:ullValue] forKey:key];
break;
case 'l': // long
[invocation invoke];
[invocation getReturnValue:&longValue];
[coder encodeObject:[NSNumber numberWithLong:longValue] forKey:key];
break;
case 's': // short
[invocation invoke];
[invocation getReturnValue:&shortValue];
[coder encodeObject:[NSNumber numberWithShort:shortValue] forKey:key];
break;
case 'I': // unsigned
[invocation invoke];
[invocation getReturnValue:&unsignedValue];
[coder encodeObject:[NSNumber numberWithUnsignedInt:unsignedValue] forKey:key];
break;
default:
break;
}
}
}
- (void)autoDecode:(NSCoder *)coder {
NSDictionary *properties = [self properties];
for (NSString *key in properties) {
NSString *type = [properties objectForKey:key];
id value;
NSNumber *number;
NSInteger i;
CGFloat f;
BOOL b;
double d;
unsigned long ul;
unsigned long long ull;
long longValue;
unsigned unsignedValue;
short shortValue;
NSString *className;
switch ([type characterAtIndex:0]) {
case '@': // object
if ([[type componentsSeparatedByString:@"\""] count] > 1) {
className = [[type componentsSeparatedByString:@"\""] objectAtIndex:1];
Class class = NSClassFromString(className);
#warning UIImage类型的属性不归档 add by yhy
if ([className isEqualToString:@"UIImage"]) {
//如果属性是UIImage类型的,不进行反归档
break;
}
// only decode if the property conforms to NSCoding
if ([class conformsToProtocol:@protocol(NSCoding )]){
value = [coder decodeObjectForKey:key];
[self setValue:value forKey:key];
}
}
break;
case 'c': // bool
number = [coder decodeObjectForKey:key];
b = [number boolValue];
[self setValue:@(b) forKey:key];
break;
case 'f': // float
number = [coder decodeObjectForKey:key];
f = [number floatValue];
[self setValue:@(f) forKey:key];
break;
case 'd': // double
number = [coder decodeObjectForKey:key];
d = [number doubleValue];
[self setValue:@(d) forKey:key];
break;
case 'i': // int
number = [coder decodeObjectForKey:key];
i = [number intValue];
[self setValue:@(i) forKey:key];
break;
case 'L': // unsigned long
number = [coder decodeObjectForKey:key];
ul = [number unsignedLongValue];
[self setValue:@(ul) forKey:key];
break;
case 'Q': // unsigned long long
number = [coder decodeObjectForKey:key];
ull = [number unsignedLongLongValue];
[self setValue:@(ull) forKey:key];
break;
case 'l': // long
number = [coder decodeObjectForKey:key];
longValue = [number longValue];
[self setValue:@(longValue) forKey:key];
break;
case 'I': // unsigned
number = [coder decodeObjectForKey:key];
unsignedValue = [number unsignedIntValue];
[self setValue:@(unsignedValue) forKey:key];
break;
case 's': // short
number = [coder decodeObjectForKey:key];
shortValue = [number shortValue];
[self setValue:@(shortValue) forKey:key];
break;
default:
break;
}
}
}
@end
2.DataHandle文件
.h文件
.m文件
#import <sqlite3.h>
#import "DatabaseHandle.h"
#import "Model.h"
#define ARCHIVERKEY @"archiver"
static DatabaseHandle *dataHandle =nil;
@implementation DatabaseHandle
+ (DatabaseHandle*)shardInstnce{
@synchronized(self){
if (dataHandle == nil) {
dataHandle = [[DatabaseHandle alloc]init];
}
return dataHandle;
}
}
//打开数据库
static sqlite3 *db = nil;
- (void)openDB{
if (db !=nil) {
return;
}
NSString *filePath = [self useCachesPathSqlite:@"WatchBall.sqlite"];
NSLog(@"db filePath = %@",filePath);
int result = sqlite3_open(filePath.UTF8String, &db);
if (result ==SQLITE_OK) {
NSString *createModelSql = @"CREATE TABLE ModelList(ID TEXT PRIMARY KEY,title TEXT,imageUrl TEXT,data BLOB)";
//执行参数?
sqlite3_exec(db, [createModelSql UTF8String], NULL, NULL, NULL);
}
}
- (void)closeDB{
int result = sqlite3_close(db);
if (result ==SQLITE_OK) {
db = nil;
NSLog(@"关闭成功");
}
}
//插入收藏活动
- (void)insertNewactivity:(Model *)model{
[self openDB];
static sqlite3_stmt *stmt = nil;
NSString *sql = @"insert into ModelList(ID,title,imageUrl,data) values (?,?,?,?)";
int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
if (result ==SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [model.ID UTF8String], -1, nil);
sqlite3_bind_text(stmt, 2, [model.title UTF8String], -1, nil);
sqlite3_bind_text(stmt, 3, [model.imageUrl UTF8String], -1, nil);
NSString *archiverKey = [NSString stringWithFormat:@"%@%@",ARCHIVERKEY,model.ID];
NSData *data = [self dataOfArchiverObject:model forKey:archiverKey];
sqlite3_bind_blob(stmt, 4, [data bytes], (int)[data length], nil);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
}
- (void)deleteModelByID:(NSString *)ID{
[self openDB];
NSString *deleteSQL = [NSString stringWithFormat:@"delete from ModelList where ID ='%@'",ID];
char * error =nil;
int result = sqlite3_exec(db, deleteSQL.UTF8String, NULL, NULL, &error);
if (result == SQLITE_OK) {
NSLog(@"删除成功");
}else{
NSLog(@"删除失败 error = %s",error);
}
}
- (Model*)selectModelWithID:(NSString *)ID{
[self openDB];
sqlite3_stmt *stmt = nil;
NSString *sql = @"select data from ModelList where ID = ?";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
Model *model = nil;
if (result ==SQLITE_OK) {
sqlite3_bind_text(stmt, 1, [ID UTF8String], -1, NULL);
if (sqlite3_step(stmt) ==SQLITE_ROW) {
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(stmt, 0) length:sqlite3_column_bytes(stmt, 0)];
NSString *archiver = [NSString stringWithFormat:@"%@%@",ARCHIVERKEY,ID];
model = [self unarchiverObject:data forKey:archiver];
}
}
sqlite3_finalize(stmt);
return model;
}
- (NSArray*)selectAllModel{
[self openDB];
sqlite3_stmt *stmt = nil;
NSString *sql = @"select ID,data from ModelList";
int result = sqlite3_prepare_v2(db, [sql UTF8String], -1, &stmt, NULL);
NSMutableArray *activityArray = [NSMutableArray array];
if (result == SQLITE_OK) {
while (sqlite3_step(stmt) ==SQLITE_ROW) {
NSString *ID = [NSString stringWithUTF8String:(const char*)sqlite3_column_text(stmt, 0)];
NSData *data = [NSData dataWithBytes:sqlite3_column_blob(stmt, 1) length:sqlite3_column_bytes(stmt, 1)];
NSString *modelKey = [NSString stringWithFormat:@"%@%@",ARCHIVERKEY,ID];
Model * model = [self unarchiverObject:data forKey:modelKey];
[activityArray addObject:model];
NSLog(@"%@",model);
}
}
sqlite3_finalize(stmt);
if (activityArray.count ==0) {
return nil;
}
return activityArray;
}
- (BOOL)isFavoriteModelWithID:(NSString *)ID{
Model *model = [self selectModelWithID:ID];
if (model ==nil) {
NSLog(@"没有收藏该条目");
return NO;
}
NSLog(@"已经收藏");
return YES;
}
#pragma ---文件路径处理----
////文件夹路径处理
- (NSString*)useCachesPathSqlite:(NSString*)databaseName{
NSString *caches = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
NSString *filePath = [caches stringByAppendingPathComponent:databaseName];
return filePath;
}
#pragma ---归档处理---
- (NSData *)dataOfArchiverObject:(id)object forKey:(NSString*)key{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:object forKey:key];
[archiver finishEncoding];
return data;
}
- (id)unarchiverObject:(NSData*)data forKey:(NSString *)key{
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
id object = [unarchiver decodeObjectForKey:key];
[unarchiver finishDecoding];
return object;
}
@end
3.基类Model
.h文件
.m文件
iOS中 学会如何对sqlite3 进行封装的更多相关文章
- iOS中 学会如何对sqlite3 进行封装 (纯手工)
#waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...
- ios中 radioButton和DataPIcker,九宫格封装好使用
下载地址 http://pan.baidu.com/share/link?shareid=2894506499&uk=923776187 引用这几个文件 radiobutton.封装好单选按钮 ...
- ios中xml和html解析(封装)
下载地址 http://pan.baidu.com/share/link?shareid=2902188921&uk=923776187 GDataXML和TFHpple配置是一样的(配置方 ...
- QF——iOS中的数据库操作:SQLite数据库,第三方封装库FMDB,CoreData
SQLite数据库: SQLite是轻量级的数据库,适合应用在移动设备和小型设备上,它的优点是轻量,可移植性强.但它的缺点是它的API是用C写的,不是面向对象的.整体来说,操作起来比较麻烦.所以,一般 ...
- 转载 -- iOS中SDK的简单封装与使用
一.功能总述 在博客开始的第一部分,我们先来看一下我们最终要实现的效果.下图中所表述的就是我们今天博客中要做的事情,下方的App One和App Two都植入了我们将要封装的LoginSDK, 两个A ...
- 数据库sqlite3的使用-ios中引用方法
一.简单说明 在iOS中使用SQLite3,首先要添加库文件libsqlite3.dylib和导入主头文件. 导入头文件,可以使用库中的函数(是纯C语言的) 二.具体说明 新建一个项目,在项目的主界面 ...
- iOS中数据库应用基础
iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...
- iOS中的数据持久化方式
iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data. 1.属性列表 涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults ...
- iOS中常用的四种数据持久化技术
iOS中的数据持久化方式,基本上有以下四种:属性列表 对象归档 SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 [NSUserDefaults st ...
随机推荐
- sql阻塞进程查询
select A.SPID as 被阻塞进程,a.CMD AS 正在执行的操作,b.spid AS 阻塞进程号,b.cmd AS 阻塞进程正在执行的操作 from master..sysprocess ...
- 初学PHP&MySQL 2014-05-31 12:40 92人阅读 评论(0) 收藏
PHP echo print 都能输出文本 date(format,timestamp)可以格式化时间戳 mktime()可以返回指定日期的时间戳 include 'filename'或者 requ ...
- 在C# 获取当前应用网址
/// <summary> /// 获取当前应用网址 /// </summary> /// <returns></r ...
- 破解root用户密码 -rwx权限
破解root用户密码(本地登录) 1.光驱要放入系统光盘 2.重启os 3.修改启动菜单进入1运行级别 4.设置新密码 5.重启os linux的运行级别(默认3或5): 查看默认的运行级别 cat ...
- HDU 3911 Black And White
Black And White Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Origina ...
- C# INotifyPropertyChanged 实现
INotifyPropertyChanged: 该接口包含一个事件, 针对属性发生变更时, 执行该事件发生. // // 摘要: // 通知客户端属性值已更改. public interface IN ...
- Springboot集成mybatis通用Mapper与分页插件PageHelper
插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 Example 相关的单表操作.通用 Mapper 是为了解决 MyBatis 使用 ...
- 洛谷 P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L…
P1360 [USACO07MAR]黄金阵容均衡Gold Balanced L… 题目描述 Farmer John's N cows (1 ≤ N ≤ 100,000) share many simi ...
- android 动态设置TextView值,例:金额添加
一说到动态递增设置TextView值,非常多人应该立即就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值可是效率不 ...
- J2EE之13个规范标准概念
主要是关于j2EE十三个规范的总结. java基础知识 首先java分为三类:J2ME.J2SE.J2EE. 依据开发软件的大小和量级他们的作用分别不同,J2ME是开发为机顶盒.移动电话和PDA之类嵌 ...