ios - Combine two sqlite3 queries -


can combine these 2 queries how?

i'm getting error of table not found in second query, , think of sqlite calls in first query.

nsstring *dayname = del.dayname; int rowcount = del.tablerownumber;  nsstring *sqlitedb = [[nsbundle mainbundle] pathforresource:@"banklist" oftype:@"sqlite3"];  if(sqlite3_open([sqlitedb utf8string], &_database) == sqlite_ok) {     nsstring *sqlstatement = [nsstring stringwithformat:@"update %@ set recipe_name='%@' cell_id='%i'",dayname, info.name, rowcount];     sqlite3_stmt *compiledstatement;      if(sqlite3_prepare_v2(_database, [sqlstatement utf8string] , -1, &compiledstatement, null) == sqlite_ok)     {         sqlite3_bind_text( compiledstatement, 1, [sqlitedb utf8string], -1, sqlite_transient);     }     if(sqlite3_step(compiledstatement) != sqlite_done )     {         nslog( @"save error: %s", sqlite3_errmsg(_database) );     }     else     {         sqlite3_reset(compiledstatement);     }      sqlite3_finalize(compiledstatement);       nsstring *sqlstatement2 = [nsstring stringwithformat:@"update %@ set recipe_id = (select key recipes name = monday.recipe_name)",dayname];     sqlite3_stmt *compiledstatement2;      if(sqlite3_prepare_v2(_database, [sqlstatement2 utf8string] , -1, &compiledstatement2, null) == sqlite_ok)     {         sqlite3_bind_text( compiledstatement2, 1, [sqlitedb utf8string], -1, sqlite_transient);     }     if(sqlite3_step(compiledstatement2) != sqlite_done )     {         nslog( @"save error: %s", sqlite3_errmsg(_database) );     }     else     {         sqlite3_reset(compiledstatement2);     }      sqlite3_finalize(compiledstatement2); }  sqlite3_close(_database); 

thanks

a couple of observations:

  1. you might not want supply parameters query using stringwithformat. if recipe "pat's infamous cookies"? apostrophe interpreted terminating string , prepare function fail. should use ? placeholders in sql , bind values. see section 3 of sqlite documentation.

  2. while i'm suggesting use sqlite3_bind_text function above, calling sqlite3_bind_text , passing path database file. that

    • doesn't make sense given sql, because don't have ? placeholders bind value to; and

    • i'm not sure why you'd passing path of database @ all.

    that call doesn't seem possibly work. if check return code of existing sqlite_bind_text call, i'd wager not sqlite_ok.

  3. if sqlite3_prepare_v2 calls fail (and common point of failure while you're refining sql), you're not logging sqlite3_errmsg. sqlite3_errmsg after sqlite3_prepare_v2 failure 1 of useful error messages (it tell precisely wrong sql). make sure examine sqlite3_errmsg if sqlite3_prepare_v2 not return sqlite_ok.

thus might yield:

if(sqlite3_open([sqlitedb utf8string], &_database) == sqlite_ok) {     nsstring *sqlstatement = [nsstring stringwithformat:@"update %@ set recipe_name=? cell_id=?",dayname];     sqlite3_stmt *compiledstatement;      if(sqlite3_prepare_v2(_database, [sqlstatement utf8string] , -1, &compiledstatement, null) != sqlite_ok)     {         nslog(@"%s: prepare failed: %s", __function__, sqlite3_errmsg(_database));         sqlite3_close(_database);         return;     }      if (sqlite3_bind_text( compiledstatement, 1, [info.name utf8string], -1, sqlite_transient) != sqlite_ok)     {         nslog(@"%s: bind_text failed: %s", __function__, sqlite3_errmsg(_database));         sqlite3_finalize(compiledstatement);         sqlite3_close(_database);         return;     }      if (sqlite3_bind_int( compiledstatement, 2, rowcount) != sqlite_ok)     {         nslog(@"%s: bind_int failed: %s", __function__, sqlite3_errmsg(_database));         sqlite3_finalize(compiledstatement);         sqlite3_close(_database);         return;     }      if (sqlite3_step(compiledstatement) != sqlite_done )     {         nslog(@"save error: %s", sqlite3_errmsg(_database) );         sqlite3_finalize(compiledstatement);         sqlite3_close(_database);         return;     }      // don't need unless you're going reuse prepared statement, aren't     //     //else     //{     //    sqlite3_reset(compiledstatement);     //}      sqlite3_finalize(compiledstatement);      // did mean hardcode "monday" in sql?      nsstring *sqlstatement2 = [nsstring stringwithformat:@"update %@ set recipe_id = (select key recipes name = monday.recipe_name)",dayname];     sqlite3_stmt *compiledstatement2;      if(sqlite3_prepare_v2(_database, [sqlstatement2 utf8string] , -1, &compiledstatement2, null) != sqlite_ok)     {         nslog(@"%s: prepare 2 failed: %s", __function__, sqlite3_errmsg(_database));         sqlite3_close(_database);         return;     }     if(sqlite3_step(compiledstatement2) != sqlite_done )     {         nslog( @"save 2 error: %s", sqlite3_errmsg(_database) );     }      // again, not needed     //     //else     //{     //    sqlite3_reset(compiledstatement);     //}      sqlite3_finalize(compiledstatement2); }  sqlite3_close(_database); 

i must confess i'm not crazy data model you're building sql, dynamically supplying table names. i'd rather see single table of days, , make dayname column in table. have should work, it's unusual construct.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -