Ios 为什么不使用我的数据库SQLite xcode
我有一个项目,我在其中创建了sqlite DB。现在我想从Sqlite读取数据并存储在NSMutableArray中,并在UITableView中显示这是我的代码,但当我一步一步编译这个程序时。此编译从此行跳转:Ios 为什么不使用我的数据库SQLite xcode,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我有一个项目,我在其中创建了sqlite DB。现在我想从Sqlite读取数据并存储在NSMutableArray中,并在UITableView中显示这是我的代码,但当我一步一步编译这个程序时。此编译从此行跳转: if(sqlite3_prepare_v2(database1, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) NSString *sqlStatement_us
if(sqlite3_prepare_v2(database1, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from Table1 ParentID = %@",ThisParentID];
转到此行,但不工作:
sqlite3_close(database1);
这是我的完整代码:
/*==================================================================
METHOD FOR READING DATA FROM DATABASE
==================================================================*/
-(NSMutableArray *)readInformationFromDatabase
{
array = [[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 =[NSString stringWithFormat:@"Select ID from Table1 where ParentID = 0"];
sqlite3_stmt *compiledStatement2;
if(sqlite3_prepare_v2(database2, [sqlStatement UTF8String], -1, &compiledStatement2, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement2) == SQLITE_ROW)
{
// Init the Data Dictionary
_OwnID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement2, 0)];
}
}
else
{
NSLog(@"No Data Found");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement2);
}
sqlite3_close(database2);
ThisParentID = _OwnID;
do {
// Setup the database object
sqlite3 *database1;
// Open the database from the users filessytem
if(sqlite3_open([[self DatabaseSqlite] UTF8String], &database1) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
//SQLIte Statement
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from Table1 ParentID = %@",ThisParentID];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database1, [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 *_dataDictionary=[[NSMutableDictionary alloc] init];
NSString *_recordName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *_recordID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *_recordParentID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
_recordBrotherID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *_recordChildID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
NSString *_recordType = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)];
NSString *_recordModified = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)];
ThisParentID = _recordID;
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordName] forKey:@"Name"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordID] forKey:@"ID"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordParentID] forKey:@"ParentID"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordBrotherID] forKey:@"BrotherID"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordChildID] forKey:@"ChildID"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordType] forKey:@"Type"];
[_dataDictionary setObject:[NSString stringWithFormat:@"%@",_recordModified] forKey:@"Modified"];
[array addObject:_dataDictionary];
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database1);
}
else
{
NSLog(@"NOT Found");
}
} while (![_recordBrotherID isEqualToString:@"0"]);
return array;
}
您可能有更多机会使用SQLite包装器,比如,或者更好的基于模型的包装器。我的朋友,当您编写许多行代码时,应该小心。该代码是正确的,但在该代码中有一个小错误,该错误在这一行:
if(sqlite3_prepare_v2(database1, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from Table1 ParentID = %@",ThisParentID];
要从具有特定列的SQlite读取数据,您应该使用以下代码:
从TableName中选择*,其中Column=SomeValue
因此,您应该更改代码并编写以下内容:
NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from Table1 Where ParentID = %@",ThisParentID];
我希望这对您有所帮助。sqlite3\u prepare\u v2的返回值是多少?