views:

51

answers:

1

Hey,

I am trying to find some source code on how to access and store variables from a database to my program via obj-c(iPhone). I have look for many hours now and no one has provided a sure fire way on how to go about this. If you have any advice or recommendations please post some source code or a link to it.

Thanks for the help.

+1  A: 

If using the 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 for keeping the db around.

sqlite3 *db = nil;

Just make sure you call your open database method before using the database. Check this page out http://ved-dimensions.blogspot.com/2009/03/iphone-development-sqlite3-populating.html Might give you something that 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;
}
Shadow
+1, except there's no good reason to not use a SQLite Objective-C wrapper. It makes things *so* much easier: http://cocoaheads.byu.edu/resources/sqlite
Dave DeLong
Oooh, I didn't look much into a wrapper for my stuff :) That would be a nice thing to take a look at, thanks! Oops sorry about the code, copy paste problems :x
Shadow