CRDatabase is an experimental fork of EGODatabase, a thread-safe Objective-C SQLite wrapper (which uses some code from FMDB), where thread-safety is implemented via custom GCD queues. It also has some asynchronous methods with C blocks.
Get it
This is a Fossil repository (see Fossil Quick Start).
Clone command:
fossil clone http://codingrobots.org/p/crdatabase CRDatabase.fossil
How to use
Remember to link libsqlite3.dylib to your project!
Methods
CRDatabase
- (NSInteger)executeInsert:(NSString *)sql withArguments:(NSArray *)args; - (NSInteger)executeInsert:(NSString *)sql withArgument:(id)arg;Execute insert and return last row id (or NSNotFound if update failed)
- (BOOL)executeUpdate:(NSString *)sql withArguments:(NSArray *)args; - (BOOL)executeUpdate:(NSString *)sql withArgument:(id)arg;Execute update and return YES (success) or NO (failure) You do deletes also with this method.
- (CRDatabaseResult *)executeQuery:(NSString*)sql withArguments:(NSArray *)args; - (CRDatabaseResult *)executeQuery:(NSString *)sql withArgument:(id)arg;
Execute query and return CRDatabaseResult.
Do not use executeQuery for inserts or updates -- use appropriate methods for this, because they disable sudden termination
- (void)asyncExecuteQuery:(NSString *)sql
withArguments:(NSArray *)args
doWithResult:(void (^)(CRDatabaseResult *))block;
Asyncroniously execute query and call block with results.
Block is running under the global queue, so to make updates to GUI enclose relevant code in block with main queue dispatch.
Examples
See CRDatabase.h for methods, and CRDB.m for some examples.
Basically, you open a database:
CRDatabase *database = [CRDatabase databaseWithURL:[NSURL fileURLWithPath:@"posts.db"]];
You can then access it. Synchronously:
CRDatabaseResult *result = [database
executeQuery:@"SELECT * FROM posts WHERE title = ?"
withArgument:@"Test title"];
for(CRDatabaseRow *row in result) {
NSLog(@"Id: %d, Body: %@",
[row integerForColumn:@"id"],
[row stringForColumn:@"body"]);
}
Asynchronously, with blocks:
[database asyncExecuteQuery:@"SELECT * FROM posts"
withArguments:nil
doWithResult:^(CRDatabaseResult *result) {
for(CRDatabaseRow *row in result) {
NSLog(@"Id: %d, Body: %@",
[row integerForColumn:@"id"],
[row stringForColumn:@"body"]);
}
});
License
MIT License. See LICENSE.
Contacts
Feel free to contact me if you have questions, bug fixes or anything else.
You can report bugs here -- see Tickets.
Icon © FatCow. Creative Commons Attribution 3.0 License