[英]sqlite not returning(select clause) data in the order data was inserted from NSArray
[英]iOS NSArray of NSDictionaries from SQLite in UITableView
在過去的兩天里,我一直在為這個問題而苦苦掙扎,但我似乎還不太清楚。 我有一個具有以下結構的SQlite數據庫。
這是List和List_Items之間的一對多關系
我訪問數據庫並創建一個對象,然后將該對象添加到NSDictionary中,該對象又添加到NSArray中。 我執行兩次,一次用於List表,一次用於List_Items。 然后,我使用列表數組來計數我的表視圖行的列表數,然后將它們添加到表視圖中。
問題出在我了解方法時
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
我似乎無法弄清楚如何匹配Lists和List_Items中的記錄,以在向下鑽取的表格視圖中顯示與該列表有關的List Items。
具體的代碼示例可能會有所幫助,因為我現在對此很不了解:(
這是我當前的Writersblock擁有的相關代碼。
//#******************************************************#
// *******Start Database*******
//#******************************************************#
-(void)checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)readItemsFromDatabase
{
// Setup the database object
sqlite3 *database;
// Init the Items Array
items = [[NSMutableArray alloc] init];
lists = [[NSMutableArray alloc] init];
//---------------### SELECT THE LISTS #####---------------//
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"SQL Opened");
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "SELECT * from List";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aListName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aUserID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aListID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSLog(@"SQL Compiled");
// Create a new list object with the data from the database
List *list = [[List alloc] initWithlistName:(NSString *)aListName userID:(NSString *)aUserID listID:(NSString *)aListID];
listNames = [NSDictionary dictionaryWithObjectsAndKeys:list.listName,@"listName",list.listID,@"listID",list.listID,@"listID",nil];
// Add the Shopping object to the list Array
[lists addObject:listNames];
}
}
else { NSLog(@"Database Not Found");}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
//---------------### SELECT THE LIST_ITEMS #####---------------//
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSLog(@"SQL Opened");
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "SELECT * from List_Items";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aBrandName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aItemName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *aItemQuantity = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSString *aListID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
NSLog(@"SQL Compiled");
// Create a new items object with the data from the database
Shopping *shopping = [[Shopping alloc] initWithlistID:(NSString *)aListID brandName:(NSString *)aBrandName itemName:(NSString *)aItemName itemQuantity:(NSString *)aItemQuantity imageURL:(NSString *)aImageUrl];
itemList = [NSDictionary dictionaryWithObjectsAndKeys:shopping.listID,@"listID",shopping.brandName,@"brandName",shopping.itemName,@"itemName",shopping.itemQuantity,@"itemQuantity",shopping.imageURL,@"imageURL",nil];
// Add the Shopping object to the items Array
[items addObject:itemList];
}
}
else { NSLog(@"Database Not Found");}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
NSLog(@"%@",items);
NSLog(@"%@",lists);
}
sqlite3_close(database);
}
//#******************************************************#
// *******END Database*******
//#******************************************************#
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int rowcount;
rowcount = [lists count];
return rowcount;
}
-(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];
}
// Set up the cell...
NSString *cellValue = [[lists objectAtIndex:indexPath.row] objectForKey:@"listName"];
cell.textLabel.text = cellValue;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[lists objectAtIndex:indexPath.row] objectForKey:@"listID"] != NULL)
{
NSString *listIndex = [[lists objectAtIndex:indexPath.row] objectForKey:@"listID"];
int i = [listIndex intValue];
NSLog(@"indexPath: %d",i);
}
}
編輯**********
單個Sql語句返回多個Listname。 這是一個問題,因為我只需要每個列表名稱之一。
因此,首先,您要創建一個List對象,然后創建一個與List對象幾乎相同的NSDictionary對象。 為什么? 為什么不只是將List對象添加到Array中呢? 如果您不對List項中的屬性執行任何功能,則根本不要使用List對象,只需將字段直接放在NSDictionary中即可。
其次,不要執行兩個不同的SQL調用來獲取信息,而只能使用一個來同時獲取List和該列表的list_items。 然后,如果您使用的是List對象,請添加NSMutableArray屬性調用項,然后將listItems添加到該數組。 您可以在NSDictionary中執行相同的操作,只需為關鍵項添加NSMutableArray對象,然后將list_items添加至該數組。
現在,您可以設置表視圖以執行所需的操作。
修改答案以回應以下評論
Select * FROM List, List_Items WHERE List.list_id = List.list_id
任何對此感興趣的人都是我弄清楚的方法。 我得到列表的listID,然后創建一個數組並遍歷列表項,並與listID進行比較。 如果它們與listID相同,則將它們添加到數組中,並在完成for循環后將結果添加到我的dataobject協議中(以使其可用於下一個視圖),然后我將提供一個模式視圖控制器並加載數據對象的數組。 一旦我完成了模態視圖,就將dataobject設置回nil和Tada! 有用。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[lists objectAtIndex:indexPath.row] objectForKey:@"listID"] != NULL)
{
[listTableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *listIndex = [[lists objectAtIndex:indexPath.row] objectForKey:@"listID"];
NSMutableArray *itemArray = [[NSMutableArray alloc]init];
int i; int itemCount = [items count];
for (i = 0; i < itemCount; i++)
{
if ([[[items objectAtIndex:i] objectForKey:@"listID"] isEqual:listIndex])
{
[itemArray addObject:[items objectAtIndex:i]];
NSLog(@"Item Array:%@",itemArray);
}
}
if (i == itemCount)
{
AppDataObject* theDataObject = [self theAppDataObject];
theDataObject.itemArray = itemArray;
ItemView *temp = [[ItemView alloc] initWithNibName:@"ItemView" bundle:nil];
[self presentModalViewController: temp animated: YES];
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.