views:

281

answers:

3

I am developing a app for a movie in which I need to fetch data from the database considering some constraints. It works perfectly on the first occasion, but when I try to fetch data 2nd time it throws a runtime exception( the app crashes).I have to bind 3 placeholders. 2 are text and 1 is integer type. Here's the code which I am using to fetch data from the database.

-(void) Data2
{
 databaseName = @"Cinema1.sqlite";

 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
 NSString *documentsDir = [documentPaths objectAtIndex:0];
 databasePath =[documentsDir stringByAppendingPathComponent:databaseName];

 [self checkAndCreateDatabase];


 sqlite3 *database;
 if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
 {
  if(detailStmt == nil)
  {
   const char *sql = "Select PVR,Fame,Cinemax,Big from listing where UPPER(State) = UPPER(?) and UPPER(City) = UPPER(?) and ZIP = ?";   
   if(sqlite3_prepare_v2(database, sql, -1, &detailStmt, NULL) == SQLITE_OK) 
   {  
    NSLog(@"Hiiiiiii");
    sqlite3_bind_text(detailStmt, 1, [t1 UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(detailStmt, 2, [t2 UTF8String], -2, SQLITE_TRANSIENT);
    sqlite3_bind_int(detailStmt, 3, t3);
    if(SQLITE_DONE != sqlite3_step(detailStmt))
    {
     NSLog(@"Helllloooooo");
     NSString *pvr= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 0)];
     NSString *fame= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 1)];;
     NSString *cinemax = [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 2)];
     NSString *big= [NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt, 3)];
     pvr1 = pvr;
     fame1 = fame;
     cinemax1 = cinemax;
     big1 = big;
     NSLog(@"PVR %@ Fame %@ Cinemax %@ Big %@",pvr1,fame1,cinemax1,big1);
    }
   }
   sqlite3_finalize(detailStmt);
  }
 }
 sqlite3_close(database);}

Can anyone help me with this.

A: 

In my limited Iphone experience when something runs once and then crashes on the next iteration, generally you are releasing memory you shouldnt be or not releasing memory you should be. Try looking at your memory allocations for problems.

Toby Allen
I think its crashing because of the binding part as when I call the function 2nd time.
Atulkumar V. Jain
+1  A: 

You are using the sqlite3_prepare_v2() and sqlite3_finalize() wrong. You only prepare a statement once and then you can bind values to it as much as you'd like. When you're done you call sqlite3_reset(). When you're completely done with this statement and won't ever use it again (i.e. you quit the app), then you use finalize.

Your app crashes because you finalize the statement, but the pointer detailStmt still points to the position where your statement once was, and tries to access that region of the memory.

See also here: http://www.sqlite.org/c3ref/prepare.html

Pascal
Now the app is not crashing but it prints the same value second time round(that is it fetches the same value from the database)
Atulkumar V. Jain
If you have multiple entries matching your criteria, then you should fetch the entries with a while-loop: `while (SQLITE_ROW == sqlite3_step(detailStmt))`. You're currently only always fetching the first value.
Pascal
Its having only one entry which matches my criteria. Second time means when I click on the submit button on the second occasion the value is not fetched from the database rather it prints the same value which it fetched from the database on the 1st occasion. So can u help me with this problem.
Atulkumar V. Jain
+1  A: 

As Toby points out, the variables pvr, fame, cinemax, and big (and the reassigned pvr1, fame1, cinemax1, and big1) are autoreleased when returned from -stringWithUTF8String:, but you never retain them. Any access to these variables after this point will cause a crash.

You say that you are seeing a thrown exception. It might be helpful to know what that exception is, by examining the console output. Also, you can enable a breakpoint on objc_exception_throw in libobjc.A.dylib, then debug with breakpoints on, to find the exact line at which this exception occurs.

Brad Larson