SQlite Database ios UITableView -
i have change whole database...
now have 2 tables display below
first table:- category table
cat_id categoryname ==================================== 1 jesus 2 buddha 3 billgates
second table :- sayingtable
say_id cat_id sayings =================================== 1 1 never false 2 1 god bless 3 1 boy 4 2 make peaceful world 5 2 never lie people
and have used uitableview in firstview controller display below image
when click on jesus christ cell..or other cell crash..
my code below
firstviewcontroller.m
- (void)viewdidload { dataaray = [[nsmutablearray alloc] init];; [dataaray addobject:@"jesus christ"]; [dataaray addobject:@"budhdha"]; [dataaray addobject:@"billgates"]; jesus = [[nsmutablearray alloc] init]; buddha = [databaseoperation selectcategory:2]; buddha = [buddha valueforkey:@"saying"]; nslog(@"buddha bhagwan category=%@",buddha); -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.dataaray count]; } -(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { nsstring *identifier = @"cellidentifier"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:identifier]; if (cell==nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier]; cell.textlabel.text = [dataaray objectatindex:indexpath.row]; cell.accessorytype = uitableviewcellaccessorydisclosureindicator; } return cell; } -(void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath { detailviewcontroller *mydetail = [[detailviewcontroller alloc] initwithnibname:@"detailviewcontroller" bundle:nil]; if ([[dataaray objectatindex:indexpath.row] isequal:@"jesus christ"]) { mydetail.flag=1; int selectedrow = (indexpath.row+1); jesus = [databaseoperation selectcategory:selectedrow]; jesus = [jesus valueforkey:@"saying"]; nslog(@"jesus category===%@",jesus); mydetail.jesus2array = [jesus mutablecopy]; //note jesus2 array of detailviewcontroller } else if ([[dataaray objectatindex:indexpath.row] isequaltostring:@"budhdha"]) { mydetail.flag=2; selectedrow = (indexpath.row+1); buddha = [databaseoperation selectcategory:selectedrow]; jesus = [jesus valueforkey:@"saying"]; nslog(@"jesus category===%@",jesus); mydetail.buddha2array = [buddha mutablecopy]; //note jesus2 array of detailviewcontroller }
detailviewcontroller.m
- (void)viewdidload { nslog(@"flag value===%d",flag); nslog(@"jesus 2 array===%@",jesus2array); } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { if (flag==1) { return [jesus2array count]; } if(flag==2) { return [buddha2array count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier] autorelease]; if (flag==1) { cell.textlabel.text = [jesus2array objectatindex:indexpath.row]; }
databaseoperation.h
#import <foundation/foundation.h> #import "databaseoperation.h" #import <sqlite3.h> @interface databaseoperation : nsobject { } +(void)check_create_database; +(nsmutablearray*)selectcategory:(int)category_id;
databaseoperation.m
#import "databaseoperation.h" #import <sqlite3.h> static nsstring *dbpath; static nsstring *databasename=@"message.sqlite"; @implementation databaseoperation +(void)check_create_database { dbpath =[[nshomedirectory() stringbyappendingpathcomponent:@"documents"] stringbyappendingpathcomponent:databasename]; bool success; nsfilemanager *fm=[nsfilemanager defaultmanager]; success=[fm fileexistsatpath:dbpath]; if(success){ return; } nsstring *dbpathfromapp=[[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:databasename]; nslog(@"dbpathfromapp = %@",dbpathfromapp); [fm copyitematpath:dbpathfromapp topath:dbpath error:nil]; } +(nsmutablearray*)selectcategory:(int)category_id //+(nsmutablearray*)selectedcategory { sqlite3 *database; nsmutablearray *dic=[[nsmutablearray alloc] init]; nsstring *saying; if(sqlite3_open([dbpath utf8string] , &database) == sqlite_ok) { nsstring *sqltmp=[nsstring stringwithformat:@"select * sayingtable cat_id=%d",category_id]; const char *sqlstmt=[sqltmp utf8string]; sqlite3_stmt *cmp_sqlstmt; if(sqlite3_prepare_v2(database, sqlstmt, -1, &cmp_sqlstmt, null)==sqlite_ok) { while(sqlite3_step(cmp_sqlstmt)==sqlite_row) { nslog(@"sql statements====%s",sqlstmt); saying=[nsstring stringwithutf8string:(char *) sqlite3_column_text(cmp_sqlstmt, 2)]; nsmutabledictionary *dicobj=[[nsmutabledictionary alloc] init]; [dicobj setvalue:[nsstring stringwithformat:@"%@",saying] forkey:@"saying"]; [dic addobject: dicobj]; } } sqlite3_finalize(cmp_sqlstmt); } sqlite3_close(database);
when click cell detailview controller not loading.....i don't know whats wrong in code please help..
thank you...
Comments
Post a Comment