lundi 2 février 2015

insert sqlite statement successful but no data added to database

I have an application that uses sqlite database. I successfully retrieved all data from database but when I try to insert data to the database the code is correct and no problem with debugging. However, when I check the database, no new row are added. even if I use the (sqlite3_last_insert_rowid) method I always get 0! I have tried many solutions provided here to similar problems as mine but had no luck. Most of the solutions were mentioning that the database directory path should be writable and I used the snippets of code they provided but the problem remains.


here is my code for the insert to the database:



-(void)openConnection
{
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDir = [paths objectAtIndex:0];
dbPath = [documentsDir stringByAppendingPathComponent:@"databaseFile.sqlite"];
NSLog(@"%@",dbPath);
fileManager = [NSFileManager defaultManager];


BOOL success = [fileManager fileExistsAtPath:dbPath];

if (success)
NSLog(@"we have the database");
else
{

NSLog(@"we have no database");
defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TLCWorkshopsApp.sqlite"];
BOOL moved = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:nil];
if (moved)
NSLog(@"database copied");

}

if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"OPENED DB");
void *v = NULL;
char *errmsg;
const char *sq = "PRAGMA foreign_keys = NO";
if (sqlite3_exec(database, sq , 0, v, &errmsg)!= SQLITE_OK) {
NSLog(@"faild to set the foreign_keys pargam");
}

}
else
NSLog(@"error in database");

}

-(void)addWorksopWithTitle:(NSString *)title presenters:(NSMutableArray *)presenters date:(NSString *)date time:(NSString *)time bld:(NSString *)bld floor:(int)floor room:(NSString *)room manadatory:(BOOL)manadatory
{
[self openConnection];
const char *encodeSqlStatement = "insert into Workshop (wid,title,manadatory,date,time,bld,floor,room) values(null,?,?,?,?,?,?,?)";
//end of edited code
sqlite3_stmt *compileStatement1;
int success;
if (sqlite3_prepare_v2(database, encodeSqlStatement, -1, &compileStatement1, NULL) == SQLITE_OK)
{
NSLog(@"insert WS prepair");
sqlite3_bind_text(compileStatement1, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 2, manadatory);
sqlite3_bind_text(compileStatement1, 3, [date UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 4, [time UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(compileStatement1, 5, [bld UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(compileStatement1, 6, floor);
sqlite3_bind_text(compileStatement1, 7, [room UTF8String], -1, SQLITE_TRANSIENT);


success = sqlite3_step(compileStatement1);


}else
{
NSLog(@"insert WS NOT success");
NSAssert1(0, @"Error: failed to prepare statement with message '%s'. ", sqlite3_errmsg(database));

}


sqlite3_finalize(compileStatement1);

if (success == SQLITE_ERROR)
{
NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
}


[self closeConnection];

}

Aucun commentaire:

Enregistrer un commentaire