1
假設我有一個名爲ABC的表,其中包含字段ABCid,ABCname,ABCImagePath,ABCSequence。現在這張表裏麪包含了超過40行。目標c:如何根據主鍵從sqlite表中獲取數據?
現在我有一個數字數組,說1,4,5,7,8,我想比較這些數字與ABCid並從表ABC獲取所有數據作爲這些ID。
+ (void) getSelectedData : (int)ABCId
{
NSString *dbPath = [self getDBPath];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *query =[NSString stringWithFormat:@"select * from [ABC] where ABCId=%d",ABCId];
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
[self getInitialDataToDisplay:dbPath];
ABC *DataObj = [[ABC alloc] initWithPrimaryKey:ABCId];
DataObj.ABCName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
ABCImagePath = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
DataObj.ABCSequence = sqlite3_column_int(selectstmt,8);
[appDelegate.arrGetData addObject:DataObj]; }
else
{
NSAssert1(0,@"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
NSLog(@"%d",[appDelegate.DataArrayTrans count]);
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
但是app在if條件下崩潰。另請注意,我使用pageController的下面幾行來從ABC對象文件中調用上述函數。
for(int i=0;i<[arrRId count];i++)
{
[ABC getSelectedData:[[arrRId objectAtIndex:i]integerValue]];
[arr576576 addObject:[appDelegate.arrGetData valueForKey:@"ABCId"]];
}
請指導我在哪裏出錯。謝謝。
爲什麼不使用核心數據? –