objective c - how to insert data in database and get data from database -
i new on iphone development. making app, using sqlite3 database. create tables sqlite3 manager 3.9.5 , write code copy database in appdelegete.m file , database copy successfully.
i insert data in database , insertion takes place primary key when access data database, sends null , if open database , open tables not show data entered in table. here code insert data , data database.
- (ibaction)addrecord:(id)sender { nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"ebirthdaydatabase.sqlite"]; if (sqlite3_open([path utf8string], &database) == sqlite_ok) { const char *sqlstatement = "insert name(name) values(?)"; sqlite3_stmt *compiledstatement; if (sqlite3_prepare_v2(database, sqlstatement, -1, &compiledstatement, null) == sqlite_ok){ nslog(@"name.text :%@",name.text); sqlite3_bind_text(compiledstatement, 1, [name.text utf8string], -1, sqlite_transient); // showcontactsviewcontroller *contact = [[showcontactsviewcontroller alloc]init]; // contact.username = name.text; // [contact addarray:contact]; } if(sqlite3_step(compiledstatement) != sqlite_done ) { nslog( @"error: %s", sqlite3_errmsg(database) ); } else { nslog( @"insert row id = %lld", sqlite3_last_insert_rowid(database)); } sqlite3_finalize(compiledstatement); sqlite3_close(database); } sqlite3 *database; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"ebirthdaydatabase.sqlite"]; nslog(@"%@",path); if (sqlite3_open([path utf8string], &database) == sqlite_ok) { const char *sql = "select name name"; sqlite3_stmt *selectstmt; if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, null) == sqlite_ok) { while(sqlite3_step(selectstmt) == sqlite_row) { // nsinteger primarykey = sqlite3_column_int(selectstmt, 0); // coffee *coffeeobj = [[coffee alloc] initwithprimarykey:primarykey]; showcontactsviewcontroller *contact=[[showcontactsviewcontroller alloc]init]; char *localitychars =( char *)sqlite3_column_name(selectstmt, 0); if (localitychars ==null) contact.username = nil; else contact.username = [nsstring stringwithutf8string: localitychars]; //contact.username = [nsstring stringwithutf8string:(char *)sqlite3_column_text(selectstmt, 1)]; nslog(@"%@",username); [showcontact addobject:contact]; } } }
to use managers if can't fix yours issue. example https://github.com/misato/sqlitemanager4ios
Comments
Post a Comment