Sqlite3_prepare_v2 gets SQLITE_ERROR

I was at this hour and MUST get this job! It supports the release of iPhone applications ... My first time using SQLite. I followed all the tips, and yet my sqlite3_prepare_v2 call gets SQLITE_ERROR (1) every time!

Here is my code from my controller:

NSString *query = @"SELECT * FROM QandA ORDER BY random() LIMIT 1"; // const char *sqlStatement = "SELECT * FROM QandA ORDER BY random() LIMIT 1"; sqlite3_stmt *compiledStatement; // sqlite3_stmt *statement; int prepareStatus = sqlite3_prepare_v2(database, [query UTF8String], -1, &compiledStatement, NULL); if (prepareStatus == SQLITE_OK) {... 

You will notice that I tried using "char *" to no avail (among other attempts). My database opens with:

  databaseName = @"Facts.sqlite"; // Get the path to the documents directory and append the databaseName NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDir = [documentPaths objectAtIndex:0]; databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; NSLog(@"databasePath = %@", databasePath); int dbOpenStatus = sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READWRITE, NULL); 

In my controller interface:

  NSString *databaseName; NSString *databasePath; 

I checked in the debugger, and everything looks fine, but the preparation statement fails. I don’t know how to write down the expression that he is trying to compile ... I assume / hope that this is exactly what my SELECT says.

Can anyone help? I'm desperate. Mark

+2
source share
3 answers

Found the answer here . I should have used this instead of the path to the DB file:

 [[NSBundle mainBundle]pathForResource:@"Facts"extension:@"sqlite"]; 

This gave a slightly different path (one additional directory) - as soon as I used it to work! Hope this helps someone else ... I spent many hours on this.

+6
source

Please note that the above is a bit inaccurate, theType should not be used for the extension, and for the Type type it is clearly necessary to match the extension of your file.

If your database is called "mysqldatabase.sql", change your path URL:

 databasePath = [[NSBundle mainBundle]pathForResource:@"mysqldatabase" ofType:@"sql"]; 
+1
source

The code seems to be fine, maybe a dumb question, but have you created a QandA table inside the database?

0
source

Source: https://habr.com/ru/post/1436935/


All Articles