ios - how to save the data in sqlite3 in iphone -


i trying create database using sqlite manager.but values not stored in database.if click save button alert message displayed on "data insertion failed".i trying rectify these problem.in case visit many tutorials.but cant rectify problem.yesterday onwards totally blocked issue.please give me idea or suggestion how save data.thanks visit question.t.c.

database.m

// creation of database

-(bool)createdb  {  nsstring *docsdir;  nsarray *dirpaths;   // document directory   dirpaths=nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);   docsdir=dirpaths[0];   // build path database file   databasepath=[[nsstring alloc]initwithstring:[docsdir stringbyappendingpathcomponent:@"feedback.db"]];   bool issuccess=yes;   nsfilemanager *filemanager=[nsfilemanager defaultmanager];   if([filemanager fileexistsatpath:databasepath]==0)   {   const char *dbpath=[databasepath utf8string];   if(sqlite3_open(dbpath, &database)==sqlite_ok)   {   char *errmsg;   const char *sql_stmt= "create table if not exists feeback details (traineeid integer, trainername text,traineename text,rating float)";   if(sqlite3_exec(database, sql_stmt, null, null, &errmsg)!=sqlite_ok)   {   issuccess=no;    nslog(@"failed create table");   }   sqlite3_close(database);   return issuccess;   }   else   {   issuccess=no;   nslog(@"failed open/create database");   }   }  return issuccess;  }  // save data in database  -(bool) savedata:(nsstring *)traineeid trainername:(nsstring *)trainername traineename:(nsstring *)traineename rating:(nsstring *)rating;   {  const char *dbpath=[databasepath utf8string];   if(sqlite3_open(dbpath, &database)==sqlite_ok)   {   nsstring *insertsql=[nsstring stringwithformat:@"insert feedbackdetails(traineeid,trainername,traineename,rating) values(\"%d\",\"%@\", \"%@\", \"%@\")",[traineeid integervalue],trainername,traineename,rating];   const char *insert_stmt=[insertsql utf8string];   sqlite3_prepare_v2(database, insert_stmt, -1, &statement, null);   if(sqlite3_step(statement)==sqlite_done)   {  return yes;  }  else  {  return no;  }  sqlite3_reset(statement);   }  return no;  }   feebbackform.m  -(ibaction)savedata:(id)sender  {  bool success=no;  nsstring *alertstring = @"data insertion failed";  if (traineeid.text.length>0 &&trainername.text.length>0 &&traineename.text.length>0 &&rating.text.length>0)  {  success=[[dbmanager getsharedinstance]savedata:traineeid.text trainername:trainername.text traineename:traineename.text rating:rating.text];  }  else  {  alertstring = @"enter fields";  }  if (success == no)   {  uialertview *alert = [[uialertview alloc]initwithtitle:alertstring message:nil delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil];   [alert show];   }  } 

your putting string quotes around int params in sql statement.

this may not issue should bind params. looks you're putting quotes around integer values in sql insert statement defined integers in table.

"create table if not exists feeback details (traineeid integer, trainername text,traineename text,rating float)"  "insert feedbackdetails(traineeid,trainername,traineename,rating) values(\"%d\",\"%@\", \"%@\", \"%@\")" 

notice double quotes around ints.

also, log out path database (even if running in simulator). go sqlite cmd line , ensure db exists , empty table there. helps in troubleshooting.

finally, take @ fmdb sqlite wrapper - helps using sqlite it's code shows patterns using sqlite raw if that's preference.

here's similar function 1 of samples shows how bind params. should finalize prepare:

- (void)updatecontact: (contact*)contact error:(nserror**)error {     if (![self ensuredatabaseopen:error])     {         return;     }      nslog(@">> contactmanager::updatecontact");      // prep statement     sqlite3_stmt    *statement;     nsstring *querysql = @"update contacts set name=?,address=?,phone=? id=?";     nslog(@"query: %@", querysql);     const char *query_stmt = [querysql utf8string];      // preparing query compiles query can re-used.     sqlite3_prepare_v2(_contactdb, query_stmt, -1, &statement, null);          sqlite3_bind_text(statement, 1, [[contact name] utf8string], -1, sqlite_static);     sqlite3_bind_text(statement, 2, [[contact address] utf8string], -1, sqlite_static);     sqlite3_bind_text(statement, 3, [[contact phone] utf8string], -1, sqlite_static);     sqlite3_bind_int64(statement, 4, [[contact id] longlongvalue]);      nslog(@"bind name: %@", [contact name]);     nslog(@"bind address: %@", [contact address]);     nslog(@"bind phone: %@", [contact phone]);     nslog(@"bind int64: %qi", [[contact id] longlongvalue]);      // process result     if (sqlite3_step(statement) != sqlite_done)     {         nslog(@"error: %@", sqlite3_errmsg(_contactdb));     }      sqlite3_finalize(statement); } 

Comments