sqlite3_prepare_v2 throws SQLITE_ERROR in iPhone code

Posted by incognitii on Stack Overflow See other posts from Stack Overflow or by incognitii
Published on 2009-12-16T14:49:12Z Indexed on 2010/03/18 3:01 UTC
Read the original article Hit count: 360

Filed under:
|

I have a piece of code that used to work, and other iterations of it that DO work within the same app, and have compared code, and they are identical in structure. This is driving me INSANE!!!!!!

In this instance, sqlite3_prepare_v2 is throwing one of those useless SQLITE_ERROR exceptions. Apparently, it can FIND the database, and OPEN the database, but it can't prepare a statement for it.

Does ANYONE have any ideas? I'm desperate here.

 second_maindatabaseName = @"database.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
second_maindatabasePath = [documentsDir stringByAppendingPathComponent:second_maindatabaseName];

// Setup the database object
sqlite3 *database;
// Init the entry requirements Array
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:second_maindatabasePath];
if(!success) {
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:second_maindatabaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:second_maindatabasePath error:nil];
[fileManager release];
}
if(sqlite3_open([second_maindatabasePath UTF8String], &database) == SQLITE_OK) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

value1 = [defaults stringForKey:kConst1];   
value2 = [defaults stringForKey:kConst2];   
value3 = [defaults stringForKey:kConst3];   
value4 = [defaults stringForKey:kConst4];   
const char *sqlStatement = "SELECT * FROM myTable WHERE Field1 = ? AND Field2 = ? AND Field3 = ? AND Field4 = ?";
sqlite3_stmt *compiledStatement;
int error_code = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(error_code == SQLITE_OK) {
// Loop through the results and add them to the feeds array
sqlite3_bind_text(compiledStatement,1,[value1 UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement,2,[value2 UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement,3,[value3 UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(compiledStatement,4,[value4 UTF8String],-1,SQLITE_TRANSIENT);

while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aNewValue = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                        }
} 
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);

© Stack Overflow or respective owner

Related posts about iphone

Related posts about sqlite3