Accessing the database via Obj-c
I am trying to find source code for accessing and storing variables from database to my program via obj-c (iPhone). I have been watching for many hours and no one has provided the right fire on how to do this. If you have any advice or guidance, please post some source code or a link to it.
Thanks for the help.
+2
a source to share
1 answer
If you are using sqlite3 database, which your program has access to on the phone for local database storage.
#import <sqlite3.h>
And create an openDatabase () method. Also add a variable to save db.
sqlite3 *db = nil;
Just make sure to call the open database method before using the database. Check this page http://ved-dimensions.blogspot.com/2009/03/iphone-development-sqlite3-populating.html Can give you what you can use.
+(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
NSLog(@"Error in opening database :(");
}
return newDBConnection;
}
+1
a source to share