Implement a data model with LKDBHelper
masterTo use a class as a database model, follow these steps:
- Define Properties: Create an Objective-C class with properties for the data you want to persist (e.g.,
NSString,NSUInteger,NSArray,NSDictionary,NSDate, etc.). - Set Table Name (Optional): Override
getTableNameto specify a custom table name. - Perform CRUD Operations: Use class methods like
saveToDB,searchWithWhere:,deleteToDB:,updateToDB:where:,isExistsWithModel:, androwCountWithWhere:to interact with the database.
// 1. Define Model
@interface LKTest : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSUInteger age;
@property (nonatomic, strong) NSArray *blah;
@end
// 2. Set Table Name
+ (NSString *)getTableName {
return @"LKTestTable";
}
// 4. Insert Data
LKTest *test = [[LKTest alloc] init];
test.name = @"zhan san";
test.age = 16;
[test saveToDB];
// 5. Query Data
NSMutableArray *array = [LKTest searchWithWhere:nil orderBy:nil offset:0 count:100];
// 5. Update Data
test.name = @"rename";
[LKTest updateToDB:test where:nil];