【发布时间】:2014-08-08 23:37:31
【问题描述】:
你好。我在检索基于 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 db 的第一行。 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];
方法。 检索数据 =
- (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 行?...
非常感谢您的指导 谢谢。
【问题讨论】:
标签: ios sqlite uitableview