如何根据ID在UITableView中获取SQLite行? (详细)

时间:2014-06-15 01:19:40

标签: ios sqlite uitableview

SQLite error...

您好。我在根据SQLite ID检索数据时遇到了一个小问题。在完成我的申请之前,这是我的障碍。我为大量的代码道歉,但我真的很感激一些帮助。

如果你看一下我提供的图片,你可以注意到一些事情:

当我选择第二个UITableCell时出现错误。确切地说:

-[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

我正在运行sqlite3框架,并检索只显示两个项目的查询...(StoreName:Latin Bites Peruvian Cuisine,StoreAddr:5709 Woodway Dr)

这就是你在我的UITableView中看到的内容,它来自我的一个对象。

Company *aCompany;

aCompany = [arrayOfStore objectAtIndex:indexPath.row];

cell.textLabel.text = aCompany.name;
cell.detailTextLabel.text = aCompany.address;

所以我一直在尝试做的是选择一个单元格,根据StoreID检索数据。 但是,我不知道如何根据ID进行选择,所以为了最初测试我的应用程序,我只调用了我的SQLite数据库的第一行。 segue的代码如下:

    sqlite3_stmt *statement;

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

        NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM storelist"];
        const char* query_sql = [querySql UTF8String];

        if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
            while (sqlite3_step(statement)==SQLITE_ROW) {


                NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];


                // set whatever you want here in your destination view controller


                [self retrieveData];


                NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
                Company *currentStore = [arrayOfStore objectAtIndex:indexPath.row];


                ReportsViewController *destinationVC = [segue destinationViewController];
                destinationVC.name = name;
                destinationVC.startDate = selectedStartDate;
                destinationVC.endDate = selectedEndDate;
                destinationVC.netSales = currentStore.netSales;
                destinationVC.voids = currentStore.voids;
                destinationVC.discounts = currentStore.discounts;
                destinationVC.guestCount = currentStore.guestCount;
                destinationVC.peopleServed = currentStore.peopleServed;
                destinationVC.employeeClockIn = currentStore.employeesClock;

用于segueing,你需要的另一个重要代码是

 [self retrieveData];

方法。 RetrieveData =

- (void) retrieveData
{


sqlite3_stmt *statement;

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

    NSString *querySql = [NSString stringWithFormat:@"SELECT * FROM storelist"];
    const char* query_sql = [querySql UTF8String];

    if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
        while (sqlite3_step(statement)==SQLITE_ROW) {




            // Statement 1,2,3 etc. relates to the SQLite table in order.

            NSString *host = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
            NSString *pass = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 6)];
            NSString *db = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 7)];




NSString *salesStr = @"http://";
salesStr = [salesStr stringByAppendingString:host];
salesStr = [salesStr stringByAppendingString:@":8080/sales.php?password="];
salesStr = [salesStr stringByAppendingString:pass];
salesStr = [salesStr stringByAppendingString:@"&db="];
salesStr = [salesStr stringByAppendingString:db];
salesStr = [salesStr stringByAppendingString:@"&sdate="];
salesStr = [salesStr stringByAppendingString:[selectedStartDate stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
salesStr = [salesStr stringByAppendingString:@"&edate="];
salesStr = [salesStr stringByAppendingString:[selectedEndDate stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];




NSURL * url = [NSURL URLWithString:salesStr];
NSData * data = [NSData dataWithContentsOfURL:url];



json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


NSLog(@"response type is %@",[json class]);

//Set up our cities array



arrayOfStore = [[NSMutableArray alloc]init];


{

    NSNumber * netSales = json[@"netSales"];
    NSNumber * voids = json[@"voidSales"];
    NSNumber * discounts = json[@"discountSales"];
    NSNumber * guestCount = json[@"guestCount"];
    NSNumber * servedCount = json[@"servedCount"];
    NSNumber * employeesClock = json[@"loggedIn"];

    NSString *netSalesString = [NSString stringWithFormat:@"%@",netSales];
    NSString *voidsString = [NSString stringWithFormat:@"%@",voids];
    NSString *discountsString = [NSString stringWithFormat:@"%@",discounts];
    NSString *guestCountString = [NSString stringWithFormat:@"%@",guestCount];
    NSString *peopleServedString = [NSString stringWithFormat:@"%@",servedCount];
    NSString *employeesClockString = [NSString stringWithFormat:@"%@",employeesClock];



    Company * myStore = [[Company alloc]initWithNetSales: (NSString *) netSalesString andVoids: (NSString *) voidsString andDiscounts: (NSString*) discountsString andGuestCount: (NSString *) guestCountString andPeopleServed: (NSString *) peopleServedString andEmployeesClock: (NSString  *) employeesClockString];




    [arrayOfStore addObject:myStore];

所以基本上我正在做的是从StoreID的第一行而不是中检索数据...

我的问题是:我如何根据 StoreID 而不是SQLite数据库中的第一个条目来获取SQLite行?...

非常感谢指导 谢谢。

2 个答案:

答案 0 :(得分:0)

在SQL中,使用WHERE子句过滤掉您不想要的记录:

SELECT host, pass, db
FROM storelist
WHERE StoreID = 123

(您应该避免使用SELECT *;只要表格结构发生变化,您的应用就可能会爆炸。)

答案 1 :(得分:0)

我最后只是将currentStore声明为我的objectAtIndex,

NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
currentStore = [arrayOfStore objectAtIndex:indexPath.row];

然后将其用于我的SQL代码。

NSString *query = [NSString stringWithFormat:@"SELECT StoreID, StoreName, StoreAddr, StoreHost, StorePort, StoreUser, StorePass, StoreDB FROM storelist WHERE StoreID=%d", currentStore.storeID];