Comment joindre mes deux tables de données dans un autre écran dans ce Am Struck avec le NSDictionary, NSArray et NSSsortingng comment append un object dans NSDictionary? Ci-dessous est mon DBClass.m
#import "DBClass.h" @implementation DBClass +(NSSsortingng *)connectdb { NSArray *docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *dbFolder=[docDir objectAtIndex:0]; NSFileManager *manager=[NSFileManager defaultManager]; if (![manager fileExistsAtPath:dbFolder]) { [manager createDirectoryAtPath:dbFolder withIntermediateDirectories:YES atsortingbutes:nil error:nil]; } NSSsortingng *dbPath=[dbFolder ssortingngByAppendingPathComponent:@"JoinsDB.sqlite"]; if (![manager fileExistsAtPath:dbPath]) { [manager copyItemAtPath:[[[NSBundle mainBundle]resourcePath]ssortingngByAppendingPathComponent:@"JoinsDB.sqlite"] toPath:dbPath error:nil]; } NSLog(@"%@",dbPath); return dbPath; } +(BOOL)createTable { NSSsortingng *dbpath=[DBClass connectdb]; sqlite3 *dbObj; if (sqlite3_open([dbpath UTF8Ssortingng], &dbObj)==SQLITE_OK) { sqlite3_stmt *stmt=nil; const char *sql="create table Company(ID INTEGER,Name VARCHAR,Age INTEGER,Address VARCHAR,Salary INTEGER)"; sqlite3_prepare_v2(dbObj, sql, -1, &stmt, nil); if (sqlite3_step(stmt)==SQLITE_DONE) { NSLog(@"tabel created successfull"); } else { NSLog(@"tabel already created"); } sqlite3_finalize(stmt); sqlite3_close(dbObj); } return YES; } +(int)saveData:(NSMutableArray *)data { sqlite3 *dbObj; sqlite3_stmt *stmt=nil; NSSsortingng *dbPath=[DBClass connectdb]; const char *sql=[[NSSsortingng ssortingngWithFormat:@"insert into Company(ID,Name,Age,Address,Salary) values(\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",[data objectAtIndex:0],[data objectAtIndex:1],[data objectAtIndex:2],[data objectAtIndex:3],[data objectAtIndex:4]]UTF8Ssortingng]; if (sqlite3_open([dbPath UTF8Ssortingng], &dbObj)==SQLITE_OK) { sqlite3_bind_text(stmt, 1, [[data objectAtIndex:0]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 2, [[data objectAtIndex:1]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 3, [[data objectAtIndex:2]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 4, [[data objectAtIndex:3]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 5, [[data objectAtIndex:4]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_prepare_v2(dbObj, sql, -1, &stmt, nil); if (sqlite3_step(stmt)==SQLITE_DONE) { NSLog(@"data insertion stmnt executed properly"); } else NSLog(@"data insertion stmnt not executed"); sqlite3_finalize(stmt); sqlite3_close(dbObj); } return sqlite3_last_insert_rowid(dbObj); } +(BOOL)createTable2 { NSSsortingng *dbpath=[DBClass connectdb]; sqlite3 *dbObj; if (sqlite3_open([dbpath UTF8Ssortingng], &dbObj)==SQLITE_OK) { sqlite3_stmt *stmt=nil; const char *sql="create table Department(ID INTEGER,Dept VARCHAR,Emp_ID INTEGER)"; sqlite3_prepare_v2(dbObj, sql, -1, &stmt, nil); if (sqlite3_step(stmt)==SQLITE_DONE) { NSLog(@"tabel created successfull"); } else { NSLog(@"tabel already created"); } sqlite3_finalize(stmt); sqlite3_close(dbObj); } return YES; } +(int)saveData2:(NSMutableArray *)data { sqlite3 *dbObj; sqlite3_stmt *stmt=nil; NSSsortingng *dbPath=[DBClass connectdb]; const char *sql=[[NSSsortingng ssortingngWithFormat:@"insert into Department(ID,Dept,Emp_ID) values(\"%@\",\"%@\",\"%@\")",[data objectAtIndex:0],[data objectAtIndex:1],[data objectAtIndex:2]]UTF8Ssortingng]; if (sqlite3_open([dbPath UTF8Ssortingng], &dbObj)==SQLITE_OK) { sqlite3_bind_text(stmt, 1, [[data objectAtIndex:0]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 2, [[data objectAtIndex:1]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 3, [[data objectAtIndex:2]UTF8Ssortingng], -1, SQLITE_TRANSIENT); sqlite3_prepare_v2(dbObj, sql, -1, &stmt, nil); if (sqlite3_step(stmt)==SQLITE_DONE) { NSLog(@"data insertion stmnt executed properly"); } else NSLog(@"data insertion stmnt not executed"); sqlite3_finalize(stmt); sqlite3_close(dbObj); } return sqlite3_last_insert_rowid(dbObj); } +(NSMutableArray *)getData { sqlite3 *dbobj; NSSsortingng *dbpath =[DBClass connectdb]; NSMutableArray *readArray=[[NSMutableArray alloc]init]; if(sqlite3_open([dbpath UTF8Ssortingng], &dbobj)==SQLITE_OK) { sqlite3_stmt *statement=nil; //**** NSSsortingng *ssortingng=@"SELECT name FROM emptable"; NSSsortingng *ssortingng=@"SELECT EMP_ID, Age, DEPT FROM COMPANY INNER JOIN DEPARTMENT ON COMPANY.ID = DEPARTMENT.EMP_ID"; const char *query=[ssortingng UTF8Ssortingng]; if(sqlite3_prepare_v2(dbobj, query, -1, &statement, NULL)==SQLITE_OK) { while (sqlite3_step(statement)==SQLITE_ROW) { NSMutableDictionary *readDic=[[NSMutableDictionary alloc] init]; [readDic setObject:[NSSsortingng ssortingngWithUTF8Ssortingng:(char *)sqlite3_column_text(statement, 2)] forKey:@"Emp_ID"]; [readDic setObject:[NSSsortingng ssortingngWithUTF8Ssortingng:(char *)sqlite3_column_text(statement, 1)] forKey:@"Name"]; [readDic setObject:[NSSsortingng ssortingngWithUTF8Ssortingng:(char *)sqlite3_column_text(statement, 1)] forKey:@"Dept"]; [readArray addObject:readDic]; NSLog(@"%@",readDic); } } sqlite3_finalize(statement); } NSLog(@"%@",readArray); sqlite3_close(dbobj); return readArray; } @end
Comment afficher dans UITableview, et dans Viewdidload, que dois-je faire Ceci est mon JoinViewController.m
#import "JoinViewController.h" #import "DBClass.h" @interface JoinViewController () @end @implementation JoinViewController @synthesize data,array; @synthesize Table; - (id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; array=[[NSMutableArray alloc]init]; data=[[NSMutableArray alloc]init]; array=[DBClass getData]; for( NSDictionary *obj in array) { NSLog(@"%@",obj); [data addObject:[obj objectForKey:@"Emp_ID"]]; [data addObject:[obj objectForKey:@"Name"]]; [data addObject:[obj objectForKey:@"Dept"]]; } NSLog(@"\n%d",[data count]); [Table reloadData]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //#warning Potentially incomplete method implementation. // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. // Return the number of rows in the section. //return 1; return [data count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSSsortingng *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.textLabel.text = [[data objectAtIndex:indexPath.row]objectForKey:@"Emp_ID"]; cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"Name"]; cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"Dept"]; return cell; } #pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Navigation logic may go here. Create and push another view controller. /* <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; // ... // Pass the selected object to the new view controller. [self.navigationController pushViewController:detailViewController animated:YES]; */ } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } @end
Dans votre méthode getdata, votre sql request EMP_ID, Age, DEPT, mais vous stockez datatables comme Emp_ID, Name, Dept. L'un dit age, l'autre dit name. En outre, vous dupliquez les numéros de colonne, obtenant une valeur de la colonne 2, qui serait DEPT, et vous le stockez dans Emp_ID, et vous obtenez la valeur de la colonne 1 deux fois, ce qui serait l'âge, et le stockage dans Nom et Département. La colonne 0 devrait être Emp_ID, 1 est Age (que vous voudrez probablement changer en Name), 2 est DEPT.
Vous pouvez également simplifier un peu votre instruction SQL avec:
SELECT EMP_ID, Age, DEPT FROM COMPANY, DEPARTMENT where COMPANY.ID = DEPARTMENT.EMP_ID;