iphone - sqlite3_prepare_v2() == SQLITE_OK returns NO and existing table is not found -
i developing iphone application using sqlite3 database file. , using following code select sqlite3 database ;
nsstring* dbyolu = [nsstring stringwithformat:@"%@/emhdb3.sqlite3",nshomedirectory()]; sqlite3* db; nslog(@"%@ dbyolu : ", dbyolu); if (sqlite3_open([dbyolu utf8string], &db) == sqlite_ok) { nsstring *query = [nsstring stringwithformat: @"select username, mobile peopleto namesurname=\"%@\"", name.text]; nslog(@"%@ : query", query); sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, [query utf8string], -1, &stmt, nil) == sqlite_ok) // statement stmt returns null , returns sqlite_ok = false. { nslog(@"stepped in sqlite_ok true"); while (sqlite3_step(stmt) == sqlite_row) { nsstring* ders_kodu = [nsstring stringwithutf8string:(char*)sqlite3_column_text(stmt, 0)]; double not = sqlite3_column_double(stmt, 1); nslog(@"%@ : %f", ders_kodu, not); } } else { nslog(@"%@ - failed", stmt); } sqlite3_finalize(stmt); } else nslog(@"db not opened");
it fails @ "sqlite3_prepare_v2(db, [query utf8string], -1, &stmt, nil) == sqlite_ok" point above. , error : "error:no such table : peopleto. returns content of table when run query in sql browser. mean query correct , working.
btw, file pathname following code on viewload
- (void)viewdidload { nsstring *docsdir; nsarray *dirpaths; // documents directory dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); docsdir = [dirpaths objectatindex:0]; // build path database file databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent: @"emhdb3.sqlite3"]]; nslog(@"dbpath : %@", databasepath); nsfilemanager *filemgr = [nsfilemanager defaultmanager]; if ([filemgr fileexistsatpath: databasepath ] == no) { const char *dbpath = [databasepath utf8string]; nslog(@"db not found"); if (sqlite3_open(dbpath, &contactdb) == sqlite_ok) { nslog(@"sqlite_ok passed"); //.... codes here, doesn't metter me because sqlite_ok = true } else { status.text = @"failed open/create database"; } } else if ([filemgr fileexistsatpath:databasepath] == yes) { nslog(@"i found file here : %s",[databasepath utf8string]); } nslog(@"completed"); [super viewdidload]; }
i have database file in project folder , added project. missing?
thanks
///////
i sorry updating question stackoverflow not allow me add new ,
please find update below ;
here updated version of code :
viewload part;
- (void)viewdidload { [self createeditablecopyofdatabaseifneeded]; nsstring *docsdir; nsarray *dirpaths; dirpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); docsdir = [dirpaths objectatindex:0]; databasepath = [[nsstring alloc] initwithstring:[docsdir stringbyappendingpathcomponent:@"emhdb3.sqlite3"]]; nslog(@"dbpath : %@", databasepath); 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 peopleto (pid integer primary key autoincrement, namesurname text, mobile text)"; if (sqlite3_exec(contactdb, sql_stmt, null, null, &errmsg) != sqlite_ok) { status.text = @"failed create table"; } sqlite3_close(contactdb); } else { status.text = @"failed open/create database"; } } else if ([filemgr fileexistsatpath:databasepath] == yes) { nslog(@"%s filepath",[databasepath utf8string]); } nslog(@"checkpoint 4"); [super viewdidload]; }
buraya başka birşey yaz
- (void)createeditablecopyofdatabaseifneeded { nslog(@"entered createeditablecopyofdatabaseifneeded"); bool success; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writabledbpath = [documentsdirectory stringbyappendingpathcomponent:@"emhdb3.sqlite3"]; success = [filemanager fileexistsatpath:writabledbpath]; if (success) { nslog(@"success == yes returned"); return; } nsstring *defaultdbpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"emhdb3.sqlite3"]; success = [filemanager copyitematpath:defaultdbpath topath:writabledbpath error:&error]; if (!success) { nslog( @"failed create writable database file message '%@'.", [error localizeddescription]); } else{ nslog(@"checkpoint 2"); } }
and part select query performed ;
-(ibaction)findcontact2:(id)sender { nsstring *dbyolu = databasepath; sqlite3* db; nslog(@"%@ dbyolu : ", dbyolu); if (sqlite3_open([dbyolu utf8string], &db) == sqlite_ok) { nsstring *query = [nsstring stringwithformat: @"select namesurname, mobile peopleto"]; //any query nslog(@"%@ : query", query); sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, [query utf8string], -1, &stmt, nil) == sqlite_ok) { nslog(@"sqlite ok"); while (sqlite3_step(stmt) == sqlite_row) { nsstring* ders_kodu = [nsstring stringwithutf8string:(char*)sqlite3_column_text(stmt, 0)]; double not = sqlite3_column_double(stmt, 1); nslog(@"%@ : %f", namesurname, mobile); } } else { nslog(@"error=%s",sqlite3_errmsg(db)); // error message : error=no such table: peopleto nslog(@"%@ - stmt", stmt); //stmt null } sqlite3_finalize(stmt); } else nslog(@"could not open db"); }
as pointed above, error error=no such table: peopleto , stmt returned null
please check db in main bundle contains table try copy resource folder , apply above
Comments
Post a Comment