Created
July 1, 2013 15:33
-
-
Save scottsappen/5901896 to your computer and use it in GitHub Desktop.
SQLLite integration with iOS app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Here’s your data controller class | |
#import <Foundation/Foundation.h> | |
#import <sqlite3.h> | |
#import "UserObject.h" | |
@interface DataController : NSObject | |
{ | |
sqlite3 *databaseHandle; | |
} | |
-(void)initDatabase; | |
-(void)insertUserObject:(UserObject*)userObject; | |
-(UserObject *)getUserObject; | |
@end | |
2. Here’s your implementation of the data controller | |
#import "DataController.h" | |
#import "UserObject.h" | |
@implementation DataController | |
// Method to open a database, the database will be created if it doesn't exist | |
-(void)initDatabase | |
{ | |
// Create a string containing the full path to the sqlite.db inside the documents folder | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"sqlite.db"]; | |
// Check to see if the database file already exists | |
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath]; | |
// Open the database and store the handle as a data member | |
if (sqlite3_open([databasePath UTF8String], &databaseHandle) == SQLITE_OK) | |
{ | |
// Create the database if it doesn't yet exists in the file system | |
if (!databaseAlreadyExists) | |
{ | |
//USER OBJECT TABLE | |
const char *sqlStatement = "CREATE TABLE IF NOT EXISTS USEROBJECT (ID INTEGER PRIMARY KEY AUTOINCREMENT, EMAILADDRESS TEXT, FIRSTNAME TEXT)"; | |
char *error; | |
if (sqlite3_exec(databaseHandle, sqlStatement, NULL, NULL, &error) == SQLITE_OK) | |
{ | |
NSLog(@"Created user object table successfully"); | |
} | |
else | |
{ | |
NSLog(@"Error creating sqllite.db: %s", error); | |
} | |
} | |
} | |
} | |
//Get the user object | |
-(UserObject *)getUserObject | |
{ | |
UserObject *myUserObject = nil; | |
// Create the query statement to get all persons | |
NSString *queryStatement = [NSString stringWithFormat:@"SELECT EMAILADDRESS, FIRSTNAME FROM USEROBJECT"]; | |
// Prepare the query for execution | |
sqlite3_stmt *statement; | |
if (sqlite3_prepare_v2(databaseHandle, [queryStatement UTF8String], -1, &statement, NULL) == SQLITE_OK) | |
{ | |
// Iterate over all returned rows | |
while (sqlite3_step(statement) == SQLITE_ROW) { | |
if (sqlite3_column_text(statement, 0)) | |
{ | |
if (sqlite3_column_text(statement, 1)) | |
{ | |
NSString *emailAddress = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)]; | |
NSString *firstName = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)]; | |
//Create user object | |
myUserObject = [[UserObject alloc] initWithEmailAddress:emailAddress andFirstName:firstName]; | |
} | |
} | |
} | |
sqlite3_finalize(statement); | |
} | |
return myUserObject; | |
} | |
// Method to store a user object | |
-(void)insertUserObject:(UserObject*)userObject | |
{ | |
// Create insert statement for the person | |
NSString *insertStatement = [NSString stringWithFormat:@"INSERT INTO USEROBJECT (EMAILADDRESS, FIRSTNAME) VALUES (\"%@\", \"%@\")", userObject.userObjectEmailAddress, userObject.userObjectFirstName]; | |
char *error; | |
if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK) | |
{ | |
NSLog(@"User Object inserted"); | |
//in case you wanted the last inserted auto incremented id | |
int personID = sqlite3_last_insert_rowid(databaseHandle); | |
} | |
else | |
{ | |
NSLog(@"Error inserting User Object: %s", error); | |
} | |
} | |
// Close the database connection when the DataController is disposed | |
- (void)dealloc { | |
sqlite3_close(databaseHandle); | |
} | |
@end | |
3. And finally, here’s how you can use it! | |
//initialize the datacontroller | |
DataController *dataController = [[DataController alloc]init]; | |
[dataController initDatabase]; | |
//get a UserObject | |
UserObject *myUserObject=[dataController getUserObject]; | |
//insert a UserObject | |
[dataController insertUserObject:userObject]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment