tags:

views:

102

answers:

1

I'm brand new to using sqlite so I may have missed something basic.

I've created a database and copied it into my app's document's directory. I'm able to open it but when I make a call to sqlite3_prepare_v2 I receive SQLITE_ERROR, "SQL error or missing database".

here's my code:

sqlite3 *database;
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
{
  sqlite3_stmt *compiledStatement;
  NSString *sqlStatement = @"select * from dbname";
  int result = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, nil);
  ...
}

the same sql statement works in the terminal.

I've found this question in a few places either without answers or with solutions that don't fix my problem. I'm hoping someone here will be able to help. Thanks.

Edit:

Got it to work using Firefox's SQLite Manager, was a problem with the database file created through the terminal.

A: 

Since you are using sqlite3_open, a new empty database will automatically be created if it does not already exist in the given location in dbPath. If you instead use sqlite3_open_v2 you are able to control it to just open an existing databse instead of creating it if missing. Just pass the flag SQLITE_OPEN_READWRITE in the third parameter.

If the database have somehow not been copied into the applications documents folder as expected, the latter method should catch this.

Hope this helps,

Claus

Claus Broch
Thanks, I bet that was the case, it was probably opening a new db instead of my sqlite file.
jan