Tuesday, 15 March 2011

objective c - How can I insert my dummy data into my sqlite table - iOS? -



objective c - How can I insert my dummy data into my sqlite table - iOS? -

i trying insert dummy info sqlite table. how insert , doing wrong? thanks, help , advice appreciated

+(void)setallvalues { static sqlite3_stmt *insertstatement; const char *sqlinsert = "insert session_descriptions (sessionid, sessiontitle, sessiondescription) values (1, 'programming title' 'programming description')"; if(sqlite3_prepare_v2([dbhelper getdb], sqlinsert, -1, &insertstatement, null) != sqlite_ok) { nslog(@"insert statement not working"); } sqlite3_finalize(insertstatement); sqlite3_close([dbhelper getdb]); }

a couple of issues:

you're checking see if sqlite3_prepare_v2 failed, not bothering check why failed. accomplished logging sqlite3_errmsg:

if (sqlite3_prepare_v2([dbhelper getdb], sqlinsert, -1, &insertstatement, null) != sqlite_ok) { nslog(@"prepare failed: %s", sqlite3_errmsg([dbhelper getdb])); }

it's of import check sqlite3_prepare_v2 success, , if not, log sqlite3_errmsg, because sqlite errors detected during preparation of statement (and if farther sqlite calls before examining error message, meaningful error message might replaced less-illuminating message).

even if prepare succeeded, you're not performing sql query. after above "prepare" statement, should "step" it, in order perform it:

if (sqlite3_step(insertstatement) != sqlite_done) { nslog(@"step failed: %s", sqlite3_errmsg([dbhelper getdb])); }

do after sqlite3_prepare_v2 before sqlite3_finalize.

you closing database. bit suspect because want leave database in whatever state when called routine. so, i'd might expect database not open, in case, routine open, prepare, step, finalize, , close. or might expect database open already, in case you'd simply prepare, step, , finalize, not close database, leaving database in same state when called routine.

as aside, advise opening database 1 time , keeping open long app alive, either approach works. create sure opens , closes balanced.

your insert statement missing comma.

ios objective-c sqlite

No comments:

Post a Comment