iphone - Can't save string into SQLite database (SQLITE_DONE doesn't return true) -
i'm trying store strings in sqlite database ios 6 iphone application. it's simple: joke displayed in textview when clicking button. when clicking second button, want save textview sqlite database (savejoke). however, sqlite_done never returned, indicating not working. so, looking @ alerts, "fail" when savejoke executed below.
any idea why not working? have feeling might missing basic in creation of , inserting sqlite database. thankful help!
my code:
jokefirstviewcontroller.m:
#import "jokefirstviewcontroller.h" @interface jokefirstviewcontroller () @end @implementation jokefirstviewcontroller @synthesize joke = _joke; - (void)viewdidload { [super viewdidload]; nsstring *docsdir; nsarray *dirpaths; dirpaths = nssearchpathfordirectoriesindomains( nsdocumentdirectory, nsuserdomainmask, yes); docsdir = dirpaths[0]; // build path database file _databasepath = [[nsstring alloc] initwithstring: [docsdir stringbyappendingpathcomponent: @"contacts.db"]]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: _databasepath ] == no) { const char *dbpath = [_databasepath utf8string]; if (sqlite3_open(dbpath, &_contactdb) == sqlite_ok) { char *errmsg; const char *sql_stmt = "create table if not exists contacts (id integer primary key autoincrement, jokesaved text)"; //look sqlite3_close(_contactdb); } } } - (ibaction)savejoke:(id)sender { /* current joke displayed */ self.joke = self.text.text; nsstring *currentjoke = self.joke; nsstring *jokesaved = [[nsstring alloc] initwithformat:currentjoke]; sqlite3_stmt *statement; const char *dbpath = [_databasepath utf8string]; if (sqlite3_open(dbpath, &_contactdb) == sqlite_ok) { nsstring *insertsql = [nsstring stringwithformat: @"insert contacts (jokesaved) values (?)", jokesaved]; const char *insert_stmt = [insertsql utf8string]; sqlite3_prepare_v2(_contactdb, insert_stmt, -1, &statement, null); if (sqlite3_step(statement) == sqlite_done) { void alertwithmessage(nsstring *message); { uialertview *alert = [[uialertview alloc] initwithtitle:@"database" message:@"success" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; } } else { void alertwithmessage(nsstring *message); { uialertview *alert = [[uialertview alloc] initwithtitle:@"database" message:@"fail" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; } } sqlite3_finalize(statement); sqlite3_close(_contactdb); } } }
jokefirstviewcontroller.h:
#import <uikit/uikit.h> #import <messageui/mfmessagecomposeviewcontroller.h> #import <sqlite3.h> @interface jokefirstviewcontroller : uiviewcontroller <mfmessagecomposeviewcontrollerdelegate> - (ibaction)savejoke:(id)sender; - (ibaction)sharejoke:(id)sender; - (ibaction)generatejoke:(id)sender; @property (strong, nonatomic) iboutlet uitextview *text; @property (copy, nonatomic) nsstring *joke; @property (strong, nonatomic) nsstring *databasepath; @property (nonatomic) sqlite3 *contactdb; @end
you have check insert query, stringformat wrong:
change:
nsstring *insertsql = [nsstring stringwithformat: @"insert contacts (jokesaved) values (?)", jokesaved];
to:
nsstring *insertsql = [nsstring stringwithformat: @"insert contacts (jokesaved) values ('%@')", jokesaved];
Comments
Post a Comment