LKDBHelper SQLite ORM

repository·master·Indexed 22 days ago

https://github.com/li6185377/lkdbhelper-sqlite-orm

An Objective-C SQLite ORM for iOS 12.0+ that simplifies database operations through automatic mapping of class properties to table columns. It provides built-in CRUD methods, support for SQLCipher encryption, and performance optimizations including WAL mode and PRAGMA configurations. It requires FMDB and supports complex queries using string or dictionary-based 'where' clauses.

Tokens
1.2K
Snippets
5
Records
7
Agent score
29%

What's inside LKDBHelper

  1. Implement a data model with LKDBHelper

    master

    To use a class as a database model, follow these steps:

    1. Define Properties: Create an Objective-C class with properties for the data you want to persist (e.g., NSString, NSUInteger, NSArray, NSDictionary, NSDate, etc.).
    2. Set Table Name (Optional): Override getTableName to specify a custom table name.
    3. Perform CRUD Operations: Use class methods like saveToDB, searchWithWhere:, deleteToDB:, updateToDB:where:, isExistsWithModel:, and rowCountWithWhere: 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];
  2. Install LKDBHelper via CocoaPods

    master

    To add LKDBHelper to your project, add the following line to your Podfile:

    pod 'LKDBHelper'

    Requirements

    • iOS 12.0+
    • ARC only
    • FMDB

    Legacy Support (iOS < 12)

    If you need to support iOS versions older than 12, you must pin your versions to:

    pod 'LKDBHelper', '2.6.3'
    pod 'FMDB', '2.7.5'

    Using Encryption (SQLCipher)

    If you require database encryption, use the SQLCipher variants:

    pod 'FMDB/SQLCipher'
    pod 'LKDBHelper/FMDBSQLCipher'
  3. Enable database performance optimizations

    master

    You can improve database performance by enabling the enablePerformanceOptimization switch. This automatically configures several SQLite PRAGMAs:

    PRAGMAValueDescription
    mmap_size6710886464MB memory mapping to reduce read/write syscalls
    cache_size-81928MB page cache to reduce disk I/O
    temp_storememoryStores temporary tables and indices in memory

    Additionally, you can enable Write-Ahead Logging (WAL) mode using enablePragmaWAL.

    To configure these globally, use the onCreateWithLKDBHelper: lifecycle callback in LKDBUtils.

    + (void)onCreateWithLKDBHelper:(LKDBHelper *)dbHelper {
        dbHelper.enablePerformanceOptimization = YES;
        dbHelper.enablePragmaWAL = YES;
    }
  4. Configure database encryption keys

    master

    When using the SQLCipher variant, you must set the encryption key after initializing the LKDBHelper instance within your model's getUsingLKDBHelper function. Use setKey: to set the key and rekey: to reset it.

    - (BOOL)setKey:(NSString *)key;
    - (BOOL)rekey:(NSString *)key;
  5. Customize column attributes

    master

    You can define specific attributes for columns (like default values, constraints, or lengths) by overriding columnAttributeWithProperty:. This is useful for setting defaultValue, checkValue (for constraints), or length.

    ```objective-c
    + (void)columnAttributeWithProperty:(LKDBProperty *)property {
        if ([property.propertyName isEqualToString:@
  6. Use 'where' clauses for queries

    master

    The where parameter in search and update methods accepts either an NSString or an NSDictionary to filter results:

    • Single condition (String): @"rowid = 1" or @ { @"rowid" : @1 }
    • Multiple conditions (String): @"rowid = 1 and sex = 0" or @ { @"rowid" : @1, @"sex" : @0 }
    • OR logic: Must use NSString (e.g., @"rowid = 1 or sex = 0").
    • IN clause (Array): @"rowid in (1,2,3)" or @ { @"rowid" : @[@1, @2, @3] }
    • Complex comparisons: Use NSString for expressions like @"date >= '2013-04-01 00:00:00'".