通过model 得到 属性名和属性类型 放进数据库

该博客介绍了一个Objective-C方法,用于从模型中获取属性名称和类型,并将它们存储到SQLite数据库。首先,使用`objc/runtime.h`获取模型的属性列表,然后根据属性类型创建不同的查询操作(如插入、更新和删除)。在`selectALWithModel:`方法中,从数据库中检索数据并根据属性设置模型对象。


#import <objc/runtime.h>


typedef NS_ENUM(NSUInteger, PropertyArrayType) {

    PropertyArrayTypeName,

    PropertyArrayTypeAttribute

};



+ (NSMutableArray *)arrayWithModel:(id)model

                              type:(PropertyArrayType)type{

    

    Class clazz = [model class];

    u_int count;

    

    objc_property_t* properties = class_copyPropertyList(clazz, &count);

    NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];

    NSMutableArray* attributeArray = [NSMutableArray arrayWithCapacity:count];

    for (int i = 0; i < count ; i++)

    {

        objc_property_t prop= properties[i];

        const char* propertyName = property_getName(prop);

        const char* attributeName = property_getAttributes(prop);

        

        [attributeArray addObject:[NSString stringWithCString:attributeName encoding:NSUTF8StringEncoding]];

        [propertyArray addObject:[NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding]];

        

//                const char* attributeName = property_getAttributes(prop);

//                NSLog(@"%@",[NSString stringWithUTF8String:propertyName]);

//                NSLog(@"%@",[NSString stringWithUTF8String:attributeName]);

        

    }

    free(properties);

    switch (type) {

        case PropertyArrayTypeName:

            return propertyArray;

            break;

        case PropertyArrayTypeAttribute:

            return attributeArray;

            break;

    }

    return nil;

}



- (NSMutableArray *)selectALWithModel:(id)model{

    

    

//    NSString *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];

//    NSString *path = [sandBoxPath stringByAppendingPathComponent:@"Student.sqlite"];

//    int result = sqlite3_open([path UTF8String], &dbPoint);

    

    NSMutableArray *models = [[self class]arrayWithModel:model type:PropertyArrayTypeName];

    NSMutableArray *modelAttributes = [[self class]arrayWithModel:model type:PropertyArrayTypeAttribute];

    NSLog(@"%@", modelAttributes);

    sqlite3_stmt *stmt = nil;

    NSString *sqlStr = @"select * from stu";

    

    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);

    NSMutableArray *arr = [NSMutableArray array];

    

    if (result == SQLITE_OK) {

        NSLog(@"成功");

        while (sqlite3_step(stmt) == SQLITE_ROW){


            id temp = [model copy];

            for (int i = 1; i <= models.count; i++) {

                NSString *propName = models[i - 1];

                NSString *attributeName = modelAttributes[i - 1];

                SEL setterMothod = NSSelectorFromString([NSString stringWithFormat:@"set%@:", propName.capitalizedString]);

                if ([attributeName hasPrefix:@"Tq"]) {

                    int age = sqlite3_column_int(stmt, i);

                    [temp performSelector:setterMothod withObject:(NSInteger)age];

                } else if ([attributeName hasPrefix:@"T@\"NSString\""]){

                    const unsigned char *pro = sqlite3_column_text(stmt, i);

                    NSString *modelPro = [NSString stringWithUTF8String:(const char *)pro];

                    [temp performSelector:setterMothod withObject:modelPro];}

            }

            [arr addObject:temp];

        }

    }else {

        NSLog(@"失败");

    }


    return arr;

}


@end

// 创建表格

    NSString *sqlStr = @"create table if not exists stu(number integer primary key autoincrement, name text ,sex text, age integer, hobby text)";

// 增

    NSString *sqlsStr = [NSString stringWithFormat:@"insert into stu (name, age , sex, hobby)\n values ('%@', '%ld', '%@', '%@')", stu.name, stu.age, stu.sex, stu.hobby];

// 改

    NSString *sqlsStr = [NSString stringWithFormat:@"update stu set name = '%@', sex = '%@', hobby = '%@', age = '%ld'  where name = '刘山山'", stu.name, stu.sex, stu.hobby, stu.age];

// 删

    NSString *sqlStr = [NSString stringWithFormat:@"delete from stu where name = '%@'", stu.name];

// 调用

    int result = sqlite3_exec(dbPoint, [sqlsStr UTF8String], nil, nil, nil);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值