开发者

sqlite on iphone can open db but not prepare statements

开发者 https://www.devze.com 2022-12-25 19:47 出处:网络
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

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号