SQLiteManager is a manager of SQL statements for iOS projects in objective-c. SQLiteManager follows the Singleton design pattern to simplify its use throughout the project. In an MVC design pattern, SQLiteManager can be regarded as the parent class of the Model layer.
Drag and drop files SQLiteManager.h and SQLiteManager.m in your project in Xcode.
Add to your frameworks list libsqlite3.dylib to your project.
Add your database to your project eg datables.sqlite . To create a sqlite database, you can use the sqlite manager firefox addon.
Edit SQLiteManager.h and enter the name of your database to the constant databaseName with its extension.
When you want to use SQLiteManager, #import "SQLiteManager.h"
in your code file:
You can now access method SQLiteManager this way [SQLiteManager singleton]
:
Note : All optional parameters must be set to nil if they are not used.
-(BOOL)save:(NSMutableDictionary *)data into:(NSString *)table;
Insert a new row in a table.
YES
if successful, otherwise NO
NSMutableDictionary *dataSave = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"value1",@"field1",
@"value2",@"field2",
nil];
[[SQLiteManager singleton] save:dataSave into:@"tableName"];
-(NSArray *)findAllFrom:(NSString *)table;
Reads all rows from a table.
NSArray *result = [[SQLiteManager singleton] findAllFrom:@"TableName"];
-(NSArray *)find:(NSString *)field from:(NSString *)table where:(NSString *)condition;
Read some field of a table with condition.
NSArray *result = [[SQLiteManager singleton] find:@"field1,field2" from:@"tableName" where:@"field2=value"];
-(NSArray *)find:(NSString *)field from:(NSString *)table where:(NSString *)condition order:(NSString *)order limit:(NSString *)limit;
Read some field of a table with criteria, sorting and limit.
NSArray *result = [[SQLiteManager singleton] find:@"field1,field2" from:@"tableName" where:@"field2=value" order:@"field3 ASC" limit:@"5"];
Update is used the same method of Create. Except that in the data table must be specified Backup Id.
NSMutableDictionary *dataSave = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"4",@"id",
@"value1",@"field1",
@"value2",@"field2",
nil];
-(BOOL)deleteRowWithId:(int)idRow from:(NSString *)table;
Deletes a row from a table.
YES
if successful, otherwise NO
-(id)executeSql:(NSString *)sql;
Executes a sql query.
NSArray
if it's an read query, else YES
if successful, otherwise NO
.NSString *sql = @"SELECT * FROM tableName WHERE id=3 OR id=7";
NSArray *result =[[SQLiteManager singleton] executeSql:sql];
NSString *sql = @"UPDATE tableName SET field1 = 'value1' WHERE field2 != 'value2' OR field3 = 'value3'";
[[SQLiteManager singleton] executeSql:sql];