jeudi 1 janvier 2015

sqlite3 prepare statement not working, possibly improper pointers iOS

I have setup a GlobalVars class to hold my sqlite3 database variable.



static sqlite3** database;
const char *dbPath;


@implementation GlobalVars : NSObject

+(GlobalVars*)sharedInstance {
static GlobalVars *myInstance = nil;
if(myInstance == nil) {
myInstance = [[[self class] alloc] init];
}

return myInstance;
}

+(sqlite3*)getGlobalDatabase {
return &database;
}

+(void)setGlobalDatabase:(sqlite3*)_database {
database = &_database;
}


Then in the header file I have



static sqlite3** database;


which is above the interface. This is how I setup my database variable.


I am then trying to access it when i open the database and prepare it. I can open it, because the open call returns true. I can not prepare it properly, because the statement returns false and it doesn't go into the if statement. I am wondering if I have messed up my pointers, because the prepare statement isn't working, and it doesn't prepare it properly.



-(void)setAllValues:(NSMutableArray*)array {
if(sqlite3_open([GlobalVars getGlobalDBPath], [GlobalVars getGlobalDatabase]) == SQLITE_OK) {
sqlite3_stmt *insertStatement;
NSString *sqlInsert = [NSString stringWithFormat:@"insert into my_table ('_id', 'name', 'age', 'weight', 'height', 'description') VALUES (%i, '%@', '%i', '%i', '%@', '%@')", ID, name, age, weight, height, description];

//*********** This is not opening, and SQLITE_OK is equal to false ***********

if(sqlite3_prepare_v2(([GlobalVars getGlobalDatabase]), [sqlInsert UTF8String], -1, &insertStatement, nil) == SQLITE_OK) {
if(sqlite3_step(insertStatement) == SQLITE_DONE) {
NSLog(@"insert stepping done");
}

sqlite3_reset(insertStatement);
}
sqlite3_finalize(insertStatement);
sqlite3_close([GlobalVars getGlobalDatabase]);
}
}


The variables for the database are all filled with their correct data, and it seems to open the database without issues. When it comes to preparing, it does not work properly and returns false. Any ideas why. Thank you for your assistance, any help is appreciated.


Aucun commentaire:

Enregistrer un commentaire