swaroop
swaroop

Reputation:

Sqlite3 gives "no such table" error on iPhone

I created a sqlite3 database, created tables and insert some data. I can retrieve using select query by using terminal application.

But when i add this database to my iPhone application resources and try to access data programatically I get error as "no such table: table name"

Why does this happen?

Upvotes: 3

Views: 10961

Answers (3)

Andrew
Andrew

Reputation:

I had the same error with sqlite3X C++ wrapper. I use this library in my IE-plugin. The problem was in unavailability .sqlite base. Current directory for IE(not plugin) is "C:\Documents and Settings\UserName\desktop\". When I put my base in this location - problem was solved.

Upvotes: 0

Peter Lee
Peter Lee

Reputation:

sqlite3_open() creates an empty database for you if the database path does not exist. So, it is possible that the path you gave it does not lead you to the intended file. With an empty database, you get "no such table" a lot.

Upvotes: 8

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181460

The DB file is probably not reachable by the iPhone. Try creating the DB from the iPhone app itself.

NSArray *paths = NSSearchPathForDirectoriesInDomains(
                    NSDocumentDirectory, NSUserDomainMask, YES
                 );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [
      documentsDirectory stringByAppendingPathComponent:@"yourdbname.sql"
];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &db_handle) != SQLITE_OK) {
    // Even though the open failed, 
    // call close to properly clean up resources.
    sqlite3_close(db_handle);
    NSAssert1(0, 
         @"Failed to open database with message '%s'.", 
         sqlite3_errmsg(db_handle)
    );
}

And the perform your query again. Also, it helps if you check for potential errors on every step of your DB access code.

Upvotes: 0

Related Questions