当前位置: 首页 > 工具软件 > LKDBHelper > 使用案例 >

LKDBHelper Sqlite操作数据库

卢权
2023-12-01

首先这里要说明一下,为什么用FMDB而不用Core Data呢,因为我们不知道Core Data是如何映射对象里面的属性关系的,如果我们更改了属性的话,就会报错

 

首先是创建LKDBHelper对象

    LKDBHelper * dbHelper = [LKDBHelper getUsingLKDBHelper];

然后根据model创建表

    [dbHelper createTableWithModelClass:[MyWorkModelclass]];

好了,这就创建完了,接下来看看这个model里面的一些操作

.h文件

#import "ITTBaseModelObject.h"

#import "MyWorkListModel.h"
#import "MyWorkSettingModel.h"
@interface MyWorkModel : ITTBaseModelObject

@property(retain,nonatomic) UIImage  * thumbnail;
@property(retain,nonatomic) NSString * workName;
@property(retain,nonatomic) NSString * workNumber;
@property(retain,nonatomic) NSString * createDate;
@property(retain,nonatomic) NSString * userId;
@property(retain,nonatomic) NSString * templateId; // 预览的时候用
@property(retain,nonatomic) NSString * workState;// 作品状态 1,自由状态  2,购物车状态
@property(retain,nonatomic) NSString * workType; // 作品类型
// db attri  在购物车结算的时候用
@property (retain,nonatomic) NSString * price;
@property (retain,nonatomic) NSString * productId;
@property (retain,nonatomic) NSString * subProId;
@property (retain,nonatomic) NSString * pId;// 上传服务器返回的作品id
@property (retain,nonatomic) NSString * thumnailId;//缩略图路径


@property (nonatomic, retain)MyWorkSettingModel *myWorkSettingModel;
@property (nonatomic, retain)MyWorkListModel *myWorkListModel;


+(MyWorkModel *) getMyWorkWithWorkId:(NSInteger ) rowId;
+(NSMutableArray * ) searchAllMyWorkWithUid:(NSString*) uid;

+(BOOL)updateMyWorkState:(MyWorkModel *) myWork workState:(NSString *)workState;
+(void)upMyWorkUserID:(MyWorkModel *)myWork userId:(NSString*) uid;
+(void)updataMyWork:(MyWorkModel * )myWork updataPar:(NSDictionary * ) paraDic;

+(NSMutableArray * ) searchInShopCarWorkWithUid:(NSString*) uid;
+(void) deleteMyWork:(MyWorkModel *)myWork;
+(BOOL)setMyworWithPid:(NSString * ) pid myWork:(MyWorkModel *)myWork;
+(MyWorkModel *) getMyworkWithPid:(NSString*)pid;

@end

.m文件

//
//  MyWorkModel.m
//  wanhuPrint
//
//  Created by Mao on 13-7-30.
//
//

#import "MyWorkModel.h"
@implementation MyWorkModel

- (id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

/**
 这里映射的时候要注意,返回的诗歌字典,左边是对应的属性名,右边是LKSQLInherit
 **/
+(NSDictionary *)getTableMapping
{
    return @{
             @"workName":LKSQLInherit,
             @"workNumber":LKSQLInherit,
             @"createDate":LKSQLInherit,
             @"userId":LKSQLInherit,
             @"workType":LKSQLInherit,
             @"userId": LKSQLInherit,
             @"templateId":LKSQLInherit,
             @"workState":LKSQLInherit,
             @"price":LKSQLInherit,
             @"productId":LKSQLInherit,
             @"subProId":LKSQLInherit,
             @"pId":LKSQLInherit,
             @"thumnailId":LKSQLInherit
             };
}

/*
 这个方法返回表名
 */
+(NSString *)getTableName
{
    return @"MyWorksTable";
}

/*
 这个方法返回版本号,用于更新数据库
 */
+(int)getTableVersion
{
    return 1;
}
 
-(void)dealloc
{
    [_createDate release];
    [_workName release];
    [_thumbnail release];
    [_workNumber release];
    [_userId release];
    [_workState release];
    [_templateId release];
    [_subProId release];
    [_productId release];
    [_price release];
    [_pId release];
    [_thumnailId release];
    [super dealloc];
}

#pragma mark - DBUtil
/*获取单一作品,从一个model根据rowid查询一个model,注意where后面跟NSString or NSDictionary or nil,orderBy是排序方法,for example:Ascending "name asc",Descending "name desc",此处不用排序。查询方法返回的是一个model或者一个NSArray
 */
+(MyWorkModel *) getMyWorkWithWorkId:(NSInteger ) rowId
{
    LKDBHelper * help = [LKDBHelper getUsingLKDBHelper];
    MyWorkModel * myWork=[help searchSingle:[MyWorkModel class] where:[NSString stringWithFormat:@"rowId=%d",rowId]  orderBy:nil];
    return myWork;
}

// 显示我所有的作品,根据uid查询一组model,返回NSArray
+(NSMutableArray * ) searchAllMyWorkWithUid:(NSString*) uid
{
  NSMutableArray * arr= [[LKDBHelper getUsingLKDBHelper] search:[MyWorkModel class] where:[NSString stringWithFormat:@"userId=%@",uid] orderBy:@"rowId desc" offset:0 count:100000];
    return  arr;
}

/*
 根据uid更新一个model,where后面跟主键或者rowid
 */
+(void)upMyWorkUserID:(MyWorkModel *)myWork userId:(NSString*) uid
{
    LKDBHelper * helper =[LKDBHelper getUsingLKDBHelper];
    [helper updateToDB:[MyWorkModel class] set:[NSString stringWithFormat:@"userId=%@",uid] where: @{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}];
}

//修改我的作品状态 1,自由状态  2.在购物车中
+(BOOL)updateMyWorkState:(MyWorkModel *) myWork workState:(NSString *)workState
{
    LKDBHelper *help =[LKDBHelper getUsingLKDBHelper];
    if([help isExistsClass:[MyWorkModel class] where:@{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}]){
        BOOL isUpdate= [ help updateToDB:[MyWorkModel class] set:[NSString stringWithFormat:@"workState = %@",workState] where:@{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}];
        return isUpdate;
    }
    return NO;
}

+(void)updataMyWork:(MyWorkModel * )myWork updataPar:(NSDictionary * ) paraDic
{
    
    NSString * keyStr = [paraDic objectForKey:@"KEY"];
    NSString * value =[paraDic objectForKey:@"VALUE"];
    LKDBHelper * help = [LKDBHelper getUsingLKDBHelper];
    [help updateToDB:[MyWorkModel class] set:[NSString stringWithFormat:@"%@ = %@",keyStr,value] where:@{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}];
}

//设置pid
+(BOOL)setMyworWithPid:(NSString * ) pid myWork:(MyWorkModel *)myWork
{
    LKDBHelper  * help = [LKDBHelper getUsingLKDBHelper];
    if([help isExistsClass:[MyWorkModel class] where:@{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}]){
        BOOL isSetPid =[help updateToDB:[MyWorkModel class] set: [NSString stringWithFormat:@"pId= %@",pid] where:@{@"rowId":[NSString stringWithFormat:@"%d",myWork.rowid]}];
        return isSetPid;
    }
    return NO;
}

+(MyWorkModel *) getMyworkWithPid:(NSString*)pid
{
    LKDBHelper * help = [LKDBHelper getUsingLKDBHelper];
   MyWorkModel*myWork =[help searchSingle:[MyWorkModel class] where:[NSString stringWithFormat:@"pId=%@",pid] orderBy:nil];
    return  myWork;
}
//获取所有购物车中的作品
+(NSMutableArray * ) searchInShopCarWorkWithUid:(NSString*) uid
{
    NSMutableArray * arr= [[LKDBHelper getUsingLKDBHelper] search:[MyWorkModel class] where:[NSString stringWithFormat:@"userId =%@ and workState = 2",uid] orderBy:@"createDate" offset:0 count:1000];
    return  arr;
}
+(void) deleteMyWork:(MyWorkModel *)myWork
{
    LKDBHelper * help = [LKDBHelper getUsingLKDBHelper];
    [help deleteToDB:myWork callback:^(BOOL result) {
        if (!result) {
            [help deleteToDB:myWork];
        }
    }];
}
@end

 

 

转载于:https://www.cnblogs.com/xiaobaizhu/p/3373111.html

 类似资料: