无法从SQL数据库检索查询

时间:2014-05-12 08:56:15

标签: ios objective-c xcode sqlite uitableview

我目前在从数据库中检索数据时遇到问题。设置了textlabel.textdetailTextLabel.text

但我得到的只是我的模拟器上的空白屏幕,在Xcode上有一个NSLOG“Queries Loaded”。

有谁知道这个问题?

请帮忙。

#import "ViewController.h"

@interface ViewController ()

{
NSMutableArray *arrayOfCompany;
sqlite3 *companyDB;
NSString *dbPathString;
}


@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfCompany = [[NSMutableArray alloc]init];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
[self createOrOpenDB];
}

- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];

dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];


NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
    const char *dbPath = [dbPathString UTF8String];

    //creat db here
    if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
        const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID INTEGER     PRIMARY KEY AUTOINCREMENT, StoreHost TEXT, StorePort TEXT, StoreUser TEXT, StorePass     TEXT)";
        sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
        sqlite3_close(companyDB);
    }
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayOfCompany count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle     reuseIdentifier:CellIdentifier];
}

Company *aCompany = [arrayOfCompany objectAtIndex:indexPath.row];

cell.textLabel.text = aCompany.storeName;
cell.detailTextLabel.text = aCompany.storeAddress;

return cell;
}




- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)addPersonButton:(id)sender {

char *error;
if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) {
    NSString *inserStmt = [NSString stringWithFormat:@"INSERT INTO     storelist(StoreHost,StorePort,StoreUser,StorePass) values ('%s', '%s', '%s', '%s')",    [self.storeHost.text UTF8String], [self.storePort.text UTF8String], [self.storeUser.text     UTF8String], [self.storePass.text UTF8String]];

    const char *insert_stmt = [inserStmt UTF8String];

    if (sqlite3_exec(companyDB, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
        NSLog(@"Company Added");

        Company *company = [[Company alloc]init];


        [company setHost:self.storeHost.text];
        [company setPort:self.storePort.text];
        [company setUser:self.storeUser.text];
        [company setPass:self.storePass.text];



        [arrayOfCompany addObject:company];
    }
    sqlite3_close(companyDB);
    }

}

- (IBAction)displayPersonButton:(id)sender {

sqlite3_stmt *statement;

if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) {
    [arrayOfCompany removeAllObjects];

    NSString *querySql = [NSString stringWithFormat:@"SHOW DATABASE"];
    const char* query_sql = [querySql UTF8String];

    if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
        while (sqlite3_step(statement)==SQLITE_ROW) {
            NSString *host = [[NSString alloc]initWithUTF8String:(const char     *)sqlite3_column_text(statement, 1)];
            NSString *port = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
            NSString *user = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
            NSString *pass = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];


            Company *company = [[Company alloc]init];


            [company setHost:host];
            [company setPort:port];
            [company setUser:user];
            [company setPass:pass];


            [arrayOfCompany addObject:company];
        }
    }
    }
    [[self myTableView]reloadData];
}

- (IBAction)deletePersonButton:(id)sender {

[[self myTableView]setEditing:!self.myTableView.editing animated:YES];
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    Company *c = [arrayOfCompany objectAtIndex:indexPath.row];
    [self deleteData:[NSString stringWithFormat:@"Delete from storelist where name is     '%s'", [c.storeName UTF8String]]];
    [arrayOfCompany removeObjectAtIndex:indexPath.row];

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]     withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)deleteData:(NSString *)deleteQuery
{
char *error;

if (sqlite3_exec(companyDB, [deleteQuery UTF8String], NULL, NULL, &error)==SQLITE_OK) {
    NSLog(@"Company deleted");
}
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[[self storeHost]resignFirstResponder];
[[self storePort]resignFirstResponder];
[[self storeUser]resignFirstResponder];
[[self storePort]resignFirstResponder];
}
@end

1 个答案:

答案 0 :(得分:1)

由于在viewDidLoad处调用方法numberOfRowsInSection,此时[arrayOfCompany count]等于零。因此,它不会在tableView中创建任何单元格。您必须在调用表视图方法之前填充数组,即在viewDidLoad方法或initWithNibName方法中。此问题已通过您正在使用的 reloadData 解决,但是存在与reloadData相关的某些问题。

但如果您仍打算将按钮和tableview保留在一个视图控制器中,那么这个答案可能对您有所帮助:

http://stackoverflow.com/questions/21953009/uitableview-reloaddata-not-working-when-put-it-in-editing-did-end-ibaction-btte