0

Hopefully this makes sense. I have a sqlite database in my documents folder. I also have a function that checks to see if that file exists in the directory, and if it doesn't, it is moved there from the main bundle. So I know that the database is in the correct place to access. However, when I run my app, I get the error: no such table: databaseName. Could the problem have to do with read write access of the sqlite database? If so, how to I check this and rectify the problem?

#define kFilename   @"foods.sqlite"

- (NSString *)dataFilePath {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* sqliteFile = [documentsPath stringByAppendingPathComponent:kFilename];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:sqliteFile];
if(fileExists)
    return sqliteFile;
else {

    NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFilename];
    NSString *folderPath = [documentsPath stringByAppendingPathComponent:kFilename];

    NSError *error;

    [[NSFileManager defaultManager] copyItemAtPath:sourcePath 
                                            toPath:folderPath
                                             error:&error];

    NSLog(@"Error description-%@ \n", [error localizedDescription]);
    NSLog(@"Error reason-%@", [error localizedFailureReason]);
}
return [documentsPath stringByAppendingPathComponent:kFilename];
}

-(void) viewWillAppear:(BOOL)animated{

sqlite3 *database;
if (sqlite3_open([[self dataFilePath] UTF8String], &database)
    != SQLITE_OK) { 

    sqlite3_close(database);
    NSAssert(0, @"Failed to open database");
}

sqlite3_stmt *statement;
//why is this if statement failing?

if (sqlite3_prepare_v2(database, [sqlStatement UTF8String],
                       -1, &statement, nil) == SQLITE_OK) {
    while (sqlite3_step(statement) == SQLITE_ROW) {
        //int row = sqlite3_column_int(statement, 0);
        char *rowData = (char *)sqlite3_column_text(statement, 1);
        foodName = [[NSString alloc] initWithUTF8String:rowData];
        [foodArray addObject:foodName];
    }
    sqlite3_finalize(statement); 
}
else {

    NSLog(@"%i", sqlite3_prepare_v2(database, [sqlStatement UTF8String],
                              -1, &statement, nil)); 
    NSLog(@"Statement: %s", sqlite3_errmsg(database));

}
sqlite3_close(database);
}
Sean Smyth
  • 1,509
  • 3
  • 25
  • 43

2 Answers2

0

Use this way instead. skip the intermediate directory steps, and talk right to the db.

- (void)checkAndCreateDB {

    NSString *dbPath = [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:@"../Documents/yourDB.sqlite"];

    // Check if the SQL database has already been saved to the users phone, if not then copy it over
    BOOL success;

    // Check if the database has already been created in the users filesystem
    success = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];

    if(!success) {

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourDB.sqlite"];

    [fileManager copyItemAtPath:databasePathFromApp toPath:dbPath error:nil];

    NSLog(@"readandwrite is available");
    }

if(!(sqlite3_open([dbPath UTF8String], &dB) == SQLITE_OK))
    {
        NSLog(@"An error has occurred.");
    }

}
Nick Esposito
  • 395
  • 2
  • 6
0

The error that you are getting says that the table 'databaseName' does not exist in your database file.

You are not getting an error that the database can not be opened, so that isn't the problem.

It looks like your sqlStatement is wrong.

lnafziger
  • 25,760
  • 8
  • 60
  • 101
  • the SQL statement is in the form SELECT * FROM database WHERE foodType = 'food type' isn't that the right way to write it? – Sean Smyth Jun 21 '12 at 20:04
  • according to your error message, the statement include the table named "databaseName" and not "database". Try `NSLog(@"%@", sqlStatement);` and see what it actually is right before you try to use it. – lnafziger Jun 21 '12 at 20:33