Don't like ads? PRO users don't see any ads ;-)
Guest

try-catch-finally (sqlite3)

By: a guest on Apr 28th, 2012  |  syntax: Objective C  |  size: 1.23 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. sqlite3_stmt *statement = nil;
  2.  
  3. @try {
  4.     sqlite3_open([databasePath UTF8String], &database);
  5.    
  6.     @try {
  7.         NSString *queryStatement = @"INSERT INTO People (ID_Organization, Name, EMail) VALUES (?, ?, ?);";
  8.        
  9.         sqlite3_prepare_v2(database, [queryStatement UTF8String], -1, &statement, NULL);
  10.        
  11.         @try {
  12.             sqlite3_bind_int(statement, aPerson.organizationID, 1);
  13.             sqlite3_bind_text(statement, 2, [aPerson.name UTF8String], -1, NULL);
  14.             sqlite3_bind_text(statement, 3, [aPerson.eMail UTF8String], -1, NULL);
  15.            
  16.             sqlite3_step(statement);
  17.            
  18.             NSLog(@"Person inserted.");
  19.             aPerson.ID = sqlite3_last_insert_rowid(database);
  20.             return TRUE;
  21.         }
  22.         @catch (NSException *exception) {
  23.             NSLog(@"%s", sqlite3_errmsg(database));
  24.             return FALSE;
  25.         }
  26.     }
  27.     @catch (NSException *exception) {
  28.         NSLog(@"%s", sqlite3_errmsg(database));
  29.         return FALSE;
  30.     }
  31.     @finally {
  32.         sqlite3_reset(statement);
  33.     }
  34. }
  35. @catch (NSException *exception) {
  36.     NSLog(@"%s", sqlite3_errmsg(database));
  37.     return FALSE;
  38. }
  39. @finally {
  40.     sqlite3_close(database);
  41. }