I was able to successfully retrieve data on the first table. But I can't seem
to retrieve from the second table. I know it has data inside it because I used
an SQLite Browser to view the data. I also tried performing queries there and
it worked fine. Only when I used the queries in the code that it doesn't work.
Below is the code writing the database in the Users/Documents folder for Iphone
- (void) createDatabaseIfNeeded
{
NSError * error;
//gets access to the file system
NSFileManager * fileManager = [NSFileManager defaultManager];
// gets the complete users document directory path
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// gets the first path in the array
NSString * documentsDirectory = [paths objectAtIndex:0];
// create the complete path for the database file
NSString * databasePath = [documentsDirectory
stringByAppendingString:@"/Database.sql"];
DebugLog("databasePath = %s\n",[databasePath UTF8String]);
// check if file exists or not
BOOL success = [fileManager fileExistsAtPath:databasePath];
if (success) return;
// the database does not exist so copy it in the users documents directory
NSString * dbPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"/Database.sql"];
DebugLog("dbpath is %s\n",[dbPath UTF8String]);
//copy the database file to ther users docuement directory
success = [fileManager copyItemAtPath:dbPath toPath:databasePath
error:&error];
if (!success)
NSAssert(0,@"Failed to copy Database!\n");
}
________________________________
From: Ben Trumbull <[email protected]>
To: [email protected]
Cc: Cocoa dev <[email protected]>
Sent: Sunday, April 11, 2010 9:27:46
Subject: re: Cant read second table from same sqlite database iphone
> Hello All,
>
> I am having trouble reading the second table from my database because it
> returns nothing even if there is data inside it.
How do you know it has data inside it ? A common mistake is to try to write to
databases in the read only part of an application's sandbox on the iphone.
Where in the sandbox is the database ?
> - (NSMutableArray*) getProvinces
> {
> NSMutableArray * data = [[NSMutableArray alloc] init];
> const char * sql = "SELECT * FROM Provinces";
> sqlite3_stmt * statement;
>
> //prepare the select statement
> int returnValue = sqlite3_prepare_v2(mDatabase, sql, -1, &statement, NULL);
>
> DebugLog("return value = %d\n",returnValue);
>
> if (returnValue == SQLITE_OK)
> {
> //sqlite3_bind_int(statement, 1, regionID);
> //loop all the rows returned by the query
> while (sqlite3_step(statement) == SQLITE_ROW)
> {
You don't check the return codes here properly. sqlite3_prepare_v2() can
return SQLITE_BUSY, or SQLITE_SCHEMA, or SQLITE_MISUSE. More importantly, you
don't check if sqlite3_step() returns SQLITE_DONE which would indicate that you
have 0 rows in this table.
- Ben
Get your new Email address!
Grab the Email name you've always wanted before someone else does!
http://mail.promotions.yahoo.com/newdomains/aa/
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]