views:

48

answers:

2

I have an application that needs a database. The application is running fine in the simulator. But when i try to deploy it onto the iphone then it gives me the error that 'no such table animal'. Where is the problem? I am providing the code for better understanding

(void)viewDidLoad
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"AnimalDatabase.sql"];


sqlite3 *database;


if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) 
{

    const char *sqlStatement = "insert into animal (id, name) VALUES (?, ?)";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
    {
        //NSLog(filePath);
        sqlite3_bind_int(compiledStatement, 1, 12);
        //NSLog(@"A");
        sqlite3_bind_text( compiledStatement, 2, [@"abc" UTF8String], -1, SQLITE_TRANSIENT);        
        //NSLog(@"B");
        if(sqlite3_step(compiledStatement) != SQLITE_DONE )
        {
            //NSLog(@"C");
            NSLog( @"Error: %s", sqlite3_errmsg(database) );
        }
        else
        {
            //NSLog(@"D");
            NSLog( @"Insert into row id = %d", sqlite3_last_insert_rowid(database));
        }
    }
    else
    {
        NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
    }

    sqlite3_finalize(compiledStatement);
}
else
{
    NSLog(@"Error Occured");
}
sqlite3_close(database);
[super viewDidLoad];

}

+1  A: 

The most likely reason is that the AnimalDatabase.sql file doesn't exist so there is no Animal table to insert into.

Mike Weller
But i will disagree with you in that. Because if the animal database is not present then how can the program run on simulator?
Joy
This could well be the issue. From what I can remember there is a difference between how the simulator handles case sensitivity of files to the way the phone handles sensitivity
Liam
+1  A: 

I think you need to look at what is being transfered to your iPhone. I had issues with this, there are some strange issues around how the database is created or not created on the actual iPhone. I think what is happening in your case is that no database is being transfered, and then your call to

sqlite3_open

is actually creating the database so you don't receive an error until you call a select statement.

Check your documentation on where to place the db in your resources to ensure it is copied to your iPhone when building.

Toby Allen