Ios 如何从SQLite读取一列并存储在数组中
我有一个SQLite数据库,它有5列,分别命名为:Ios 如何从SQLite读取一列并存储在数组中,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我有一个SQLite数据库,它有5列,分别命名为:Name,ID,ChildID,ParentID,brotherrid 在这个数据库中,我有很多记录,我想在数组中存储所有列中的一个值并返回这个数组。例如,我想获取ParentID列中的所有值。我使用以下查询代码: 从表1中选择ParentID(表1是表的名称) 这是我从特定列获取数组的代码: /*================================================================== METHOD
Name
,ID
,ChildID
,ParentID
,brotherrid
在这个数据库中,我有很多记录,我想在数组中存储所有列中的一个值并返回这个数组。例如,我想获取ParentID
列中的所有值。我使用以下查询代码:
从表1中选择ParentID
(表1是表的名称)
这是我从特定列获取数组的代码:
/*==================================================================
METHOD FOR GETTING MIDIFIED FROM DATABASE
==================================================================*/
- (NSMutableArray*)readingModified
{
ModiArray = [[NSMutableArray alloc] init];
// Setup the database object
sqlite3 *database2;
// Open the database from the users filessytem
if(sqlite3_open([[self DatabaseSqlite] UTF8String], &database2) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
//SQLIte Statement
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select ParentID from Table1"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Init the Data Dictionary
NSMutableDictionary *_dataDictionary2=[[NSMutableDictionary alloc] init];
NSString *_recordParentID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
[_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_recordModified] forKey:@"ParentID"];
[array addObject:_dataDictionary2];
}
}
else
{
NSLog(@"No Data Found");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database2);
return ModiArray;
}
请告诉我我的错误。我的几个朋友小心点 此代码是正确的,但您犯了错误: [array addObject:_dataDictionary2]强> 而是
array
putModiArray
/*==================================================================
METHOD FOR GETTING MIDIFIED FROM DATABASE
==================================================================*/
- (NSMutableArray*)readingModified
{
ModiArray = [[NSMutableArray alloc] init];
// Setup the database object
sqlite3 *database2;
// Open the database from the users filessytem
if(sqlite3_open([[self DatabaseSqlite] UTF8String], &database2) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
//SQLIte Statement
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select ParentID from Table1"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database2, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Init the Data Dictionary
NSMutableDictionary *_dataDictionary2=[[NSMutableDictionary alloc] init];
NSString *_recordParentID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
[_dataDictionary2 setObject:[NSString stringWithFormat:@"%@",_recordModified] forKey:@"ParentID"];
[ModiArray addObject:_dataDictionary2];
}
}
else
{
NSLog(@"No Data Found");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database2);
return ModiArray;
}
您正在将对象添加到
数组
,并分配和返回修改数组