用简单的方式操作sqlite
发布时间:2020-12-12 19:19:31 所属栏目:百科 来源:网络整理
导读:很久前就想写一个类似于ORM的东西,把数据层(Model)和数据库的表(Table)对应起来,写了一半后来就忘记了。github连接点我 原理: 每一个项目有N个数据库 每一个Model对应一个数据表 Model的每个字段对应表中的列 Model的每个字段的类型对应表每列的类型
很久前就想写一个类似于ORM的东西,把数据层(Model)和数据库的表(Table)对应起来,写了一半后来就忘记了。github连接点我原理: 每一个项目有N个数据库 每一个Model对应一个数据表 Model的每个字段对应表中的列 Model的每个字段的类型对应表每列的类型把Model保存数据库的过程: 1、检测用户配置项,确定那些字段是要映射 2、把Model的属性和属性类型装换成表对应的列和列类型 3、转换成SQL语句 4、插入到数据库 从数据库中读取数据转成Model的过程和这个相反。 DBAssistantDBAssistant base on FMDB and it provide a simple way to access sqlite database.
Getting started
Examplecreate a file user.h @interface User : NSObject @property (strong,nonatomic) NSString *name; @property (assign,nonatomic) CGFloat height; @property (assign,nonatomic) NSInteger num; @end use the User model where you need //create a new model User *user = [[User alloc]init]; user.name = @"mason"; user.height = 180.23; user.num = 123456; [user saveModel]; ? NSLog(@"users count: %ld",User.allModels.count); ? //find model user = [User firstModelWhere:@{DBRowId: @(1)}]; NSLog(@"name %@,num: %ld,height: %lf",user.name,user.num,user.height); ? ? //update model [user updateModel:@{@"name": @"Dear"}]; ? ? user = [User firstModelWhere:@{DBRowId: @(1)}]; NSLog(@"name %@,user.height); ? //delete model [user deleteModel]; NSLog(@"users count: %ld",User.allModels.count); More InterfacesConfigs//dbPath +(NSString*)dbPath; ? //tableName,default is the current class +(NSString*)tableName; ? //primarykeys,default is DBRowID +(NSArray*)primaryKeys; ? //only properties to map table columns +(NSArray *)onlyPropertiesToMapColumns; ? //except properties to map table columns +(NSArray *)exceptPropertiesToMapColumns; ? //property to column Mappings +(NSDictionary *)propertyToColumnMappings; ? //properties default values +(NSDictionary *)defaultValues; ? //properties value check values +(NSDictionary *)checkValues; ? //properties value length +(NSDictionary *)lengthValues; ? //properties value those should be unique +(NSArray *)uniqueValues; ? //properties value those should be not null +(NSArray *)notNullValues; ? +(NSString *)dateFormatterString; ? +(NSString *)imagePathForImage:(NSString *)imgName ; ? +(NSString *)dataPathForData:(NSString *)dataName; ? //default is YES +(BOOL)shouldMapAllParentPropertiesToTable; ? //default is YES +(BOOL)shouldMapAllSelfPropertiesToTable; Create/Drop table+(BOOL)createTable; ? +(BOOL)dropTable; Insert+(BOOL)insertModel:(NSObject *)model; ? +(BOOL)insertModelIfNotExists:(NSObject *)model; ? -(BOOL)saveModel; Update+(BOOL)updateModelsWithModel:(NSObject *)model where:(NSObject *)where; ? +(BOOL)updateModelsWithDictionary:(NSDictionary *)dic where:(NSObject *)where; ? -(BOOL)updateModel:(id)value; Find+(BOOL)modelExists:(NSObject *)model; ? +(NSArray *)allModels; ? +(NSArray *)findModelsBySQL:(NSString *)sql; ? +(NSArray *)findModelsWhere:(NSObject *)where; ? +(NSArray *)findModelsWhere:(NSObject *)where orderBy:(NSString *)orderBy; ? +(NSArray )findModelsWhere:(NSObject *)where groupBy:(NSString *)groupBy orderBy:(NSString)orderBy limit:(int)limit offset:(int)offset; ? +(id)firstModelWhere:(NSObject *)where; ? +(id)firstModelWhere:(NSObject )where orderBy:(NSString)orderBy ; ? +(id)lastModel; ? +(NSInteger)rowCountWhere:(NSObject *)where; Delete+(BOOL)deleteModel:(NSObject *)model; ? +(BOOL)deleteModelsWhere:(NSObject *)where; ? -(BOOL)deleteModel; Transaction+(void)beginTransaction; ? +(void)commit; ? +(void)rollback; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |