0

I have a controller view who is using thoses 2 functions:


[appDelegate getFichesInfo:[[self.fichesCategory idModuleFiche] intValue] ]; //first function
self.fiche = (Fiche *)[appDelegate.fichesInfo objectAtIndex:0];


[appDelegate getFichesVisuels:[[self.fiche idFiche] intValue] ];  //second function not working
self.fiche = (Fiche *)[appDelegate.fichesInfo objectAtIndex:0];

So in my ressourceManager, here is the 2nd function in details:


- (void)getFichesVisuels:(int)value {

    fichesVisuels = [[NSMutableArray alloc] init];

    sqlite3 *database;

    if(sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK) {
        sqlite3_reset(getFichesVisuelsStatement);


        sqlite3_bind_int(getFichesVisuelsStatement, 1,  value);

        while(sqlite3_step(getFichesVisuelsStatement) == SQLITE_ROW) {


            NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getFichesVisuelsStatement , 7)];
            NSString *aLpath = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getFichesVisuelsStatement , 8)];

            Visuel *visuel = [[Visuel alloc] initWithName:aTitle lpath:aLpath];

            [fichesVisuels addObject:visuel];
        }

    }
    sqlite3_close(database);
}

The problem is that the 2nd function is not working (librairies routine called out of sequence) because I am already calling the 1st function in the same way (I wanna be able to execute many query using arguments in the same times). I heard that using NSInvocation can be the solution to this problem, but I don't know how to turn my code using NSInvocation. Someone can help me?

Best Regards,

ludo
  • 1,479
  • 5
  • 25
  • 50
  • What isn't working and what makes you think that NSInvocation is going to fix it in any way? – Azeem.Butt Dec 22 '09 at 03:13
  • In fact this function is working but only if I use only this one in my View controller but I already use another function first and after that this one don't work. Here is my function: --------- [appDelegate getFichesInfo:[[self.fichesCategory idModuleFiche] intValue] ]; self.fiche = (Fiche *)[appDelegate.fichesInfo objectAtIndex:0]; -------- after that I use the one I put in my question ------ [appDelegate getFichesInfo:[[self.fichesCategory idModuleFiche] intValue] ]; self.fiche = (Fiche *)[appDelegate.fichesInfo objectAtIndex:0];------- You can see that I reuse one argument from the first. – ludo Dec 22 '09 at 03:27

1 Answers1

0

The problem you could be having is that your prepared statement is invalid after sqlite3_close. You need to create a new prepared statement each time you open the database.

  1. Open the database with sqlite3_open.
  2. Prepare the statement with sqlite3_prepare and friends.
  3. Bind with sqlite3_bind.
  4. Step with sqlite3_step.
  5. Afterwards, ensure you call sqlite3_finalize.
  6. Close the database with sqlite3_close.

You can't just reset the statement, because that statement was prepared for a different database handle.

NB

I'm not too familiar with SQLite.

Community
  • 1
  • 1
dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • sorry I forget to say that all my statements are already prepared when I initialize the database, everything is compiled before to faster access. --------- const char *getFichesVisuelsSQL = GET_FICHES_VISUELS_SQL; sqlite3_prepare_v2(database, getFichesVisuelsSQL, -1, &compiledStatement, NULL); getFichesVisuelsStatement = compiledStatement; ------- – ludo Dec 22 '09 at 03:21
  • I didn't use the sqlite3_ finalize I will try it. Do I have to do that everytime? – ludo Dec 22 '09 at 03:22
  • 1
    Each time you open the database, you must prepare your statements again, because that prepared statement is specific to the database you opened. Also, you only called `sqlite3_finalize` to balance the `sqlite3_prepare`, similar to `retain` and `release`. – dreamlax Dec 22 '09 at 03:27
  • 1
    If you close your database with `sqlite3_close`, all prepared statements that were created using that database handle are now invalid. – dreamlax Dec 22 '09 at 03:28
  • But all of my functions are working, the statement already prepared and compiled I just have to reuse functions like that. the problem is that in the same controller view I'm calling 2 differents function and the second one call an argument on the first one. – ludo Dec 22 '09 at 03:32