iphone - making map coordinates(lan,& long)storing in sqlite database -
how created application records series of longitude , latitude values in sqlite database , display them coloured track on mapactivity.
i need help. how can store map coordinates in sqlite database , display journey details in table .for example have done 1 journey mumbai pune .then how can store data database can available future reference .when user click on journey name should give details
if new sqlite class data base create 2 database file following
---->>>> database.h
write following code in file
#import <foundation/foundation.h> #import <sqlite3.h> @interface database : nsobject { sqlite3 *database; } +(database *) sharedatabase; -(bool) createdatabase:(nsstring *)databasename; -(nsstring*) getdatabasepath:(nsstring *)database; -(nsmutablearray *) getalldataforquery:(nsstring *)sql fordatabase:(nsstring *)database; -(void) inseryquery:(nsstring *) insertsql fordatabase:(nsstring *)database1; -(void) deletequery:(nsstring *) deletesql fordatabase:(nsstring *)database1; -(void) updatequery:(nsstring *) updatesql fordatabase:(nsstring *)database1; @end ---->>>> database.m
write following code in file
#import "database.h" @implementation database static database *sampledatabase =nil; +(database*) sharedatabase{ if(!sampledatabase){ sampledatabase = [[database alloc] init]; } return sampledatabase; } -(nsstring *) getdatabasepath:(nsstring *)database1{ [self createdatabase:database1]; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; return [documentsdirectory stringbyappendingpathcomponent:database1]; } -(bool) createdatabase:(nsstring *)databasename{ bool success; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *writabledbpath = [documentsdirectory stringbyappendingpathcomponent:databasename]; success = [filemanager fileexistsatpath:writabledbpath]; if (success) return success; nsstring *defaultdbpath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:databasename]; success = [filemanager copyitematpath:defaultdbpath topath:writabledbpath error:&error]; if (!success) { uialertview *alert = [[uialertview alloc] initwithtitle:@"error!!!" message:@"failed create writable database" delegate:self cancelbuttontitle:@"cancel" otherbuttontitles:nil]; [alert show]; [alert release]; } return success; } -(nsmutablearray *) getalldataforquery:(nsstring *)sql fordatabase:(nsstring *)database1{ sqlite3_stmt *statement = nil ; nsstring *path = [self getdatabasepath:database1]; nsmutablearray *alldata; alldata = [[nsmutablearray alloc] init]; if(sqlite3_open([path utf8string],&database) == sqlite_ok ) { nsstring *query = sql; if((sqlite3_prepare_v2(database,[query utf8string],-1, &statement, null)) == sqlite_ok) { while(sqlite3_step(statement) == sqlite_row) { nsmutabledictionary *currentrow = [[nsmutabledictionary alloc] init]; int count = sqlite3_column_count(statement); (int i=0; < count; i++) { char *name = (char*) sqlite3_column_name(statement, i); char *data = (char*) sqlite3_column_text(statement, i); nsstring *columndata; nsstring *columnname = [nsstring stringwithcstring:name encoding:nsutf8stringencoding]; if(data != nil) columndata = [nsstring stringwithcstring:data encoding:nsutf8stringencoding]; else { columndata = @""; } [currentrow setobject:columndata forkey:columnname]; } [alldata addobject:currentrow]; } } sqlite3_finalize(statement); } sqlite3_close(database); return alldata; } -(void) inseryquery:(nsstring *) insertsql fordatabase:(nsstring *)database1{ sqlite3_stmt *statement = nil ; nsstring *path = [self getdatabasepath:database1]; if(sqlite3_open([path utf8string],&database) == sqlite_ok ) { if((sqlite3_prepare_v2(database,[insertsql utf8string],-1, &statement, null)) == sqlite_ok) { if(sqlite3_step(statement) == sqlite_ok){ } } sqlite3_finalize(statement); } sqlite3_close(database); } -(void) updatequery:(nsstring *) updatesql fordatabase:(nsstring *)database1{ sqlite3_stmt *statement = nil ; nsstring *path = [self getdatabasepath:database1]; if(sqlite3_open([path utf8string],&database) == sqlite_ok ) { if((sqlite3_prepare_v2(database,[updatesql utf8string],-1, &statement, null)) == sqlite_ok) { if(sqlite3_step(statement) == sqlite_ok){ } } sqlite3_finalize(statement); } sqlite3_close(database); } -(void) deletequery:(nsstring *) deletesql fordatabase:(nsstring *)database1{ sqlite3_stmt *statement = nil ; nsstring *path = [self getdatabasepath:database1]; if(sqlite3_open([path utf8string],&database) == sqlite_ok ) { if((sqlite3_prepare_v2(database,[deletesql utf8string],-1, &statement, null)) == sqlite_ok) { if(sqlite3_step(statement) == sqlite_ok){ } } sqlite3_finalize(statement); } sqlite3_close(database); } @end now data use following code
nsstring *sql = @"select * userinfo"; <br> userinfo = [[database sharedatabase] getalldataforquery:sql fordatabase:@"sample.db"]; it return array of row in form of nsdictionary.
to add new record use following code
nsstring *sql = [nsstring stringwithformat:@"insert userinfo values ('city','name','phone')"]; [[database sharedatabase] inseryquery:sql fordatabase:@"sample.db"]; in same way there method update , delete record.
so best example have seen need call 1 method fetch, insert , update or delete.
thanks seeing question,
Comments
Post a Comment