objective c - how can i alter table in sqlite database through the obj c methods in iphone project -
hii every one
i need add 1 column existing table how can alter table in sqlite database through obj c ,i using following code inserting data table in same way how can write updata table method
- (void) insertrecord { if(addstmt == nil) { const char *sql = "insert tbl_users(firstname,middlename) values(?,?)"; if(sqlite3_prepare_v2(database, sql, -1, &addstmt, null) != sqlite_ok) nsassert1(0, @"error while creating add statement. '%s'", sqlite3_errmsg(database)); } sqlite3_bind_text(addstmt, 1, [strfirstname utf8string], -1, sqlite_transient); sqlite3_bind_text(addstmt, 2, [strmiddlename utf8string], -1, sqlite_transient); //sqlite3_bind_text(addstmt, 3, [strlogin utf8string], -3, sqlite_transient); if(sqlite_done != sqlite3_step(addstmt)) nsassert1(0, @"error while inserting data. '%s'", sqlite3_errmsg(database)); else //sqlite provides method last primary key inserted using sqlite3_last_insert_rowid //productid = sqlite3_last_insert_rowid(database); //reset add statement. sqlite3_reset(addstmt);
}
can 1 me,,thanx in advance
this might of use. header file
nsstring *databasepath;
.m file //make call these methods
[self checkandcreatedb ]; [self alterdb]; -(void)checkandcreatedb { nsstring* databasename = @"masterdb.sqlite"; nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdir = [documentpaths objectatindex:0]; databasepath = [documentsdir stringbyappendingpathcomponent:databasename]; bool success1; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; success1 = [filemanager fileexistsatpath:databasepath]; if(success1) return; nsstring *databasepathfromapp = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:databasename]; [filemanager copyitematpath:databasepathfromapp topath:databasepath error:nil]; } -(void) alterdb{ sqlite3 *database; sqlite3_stmt *statement; if(sqlite3_open([databasepath utf8string], &database) == sqlite_ok) { nsstring *updatesql = [nsstring stringwithformat: @"alter table user add column testcolumn text"]; const char *update_stmt = [updatesql utf8string]; sqlite3_prepare_v2(database, update_stmt, -1, &statement, null); if(sqlite3_step(statement)==sqlite_done) { uialertview *alert = [[uialertview alloc] initwithtitle:@"db altered" message:@"success" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; [alert release]; alert=nil; } else { uialertview *alert = [[uialertview alloc] initwithtitle:@"db updation" message:@"db not altered" delegate:self cancelbuttontitle:@"ok" otherbuttontitles:nil]; [alert show]; [alert release]; alert=nil; } // release compiled statement memory sqlite3_finalize(statement); sqlite3_close(database); } }
Comments
Post a Comment