在这个数据库中有一个名为“菜单”的表。我想在我的 ViewController 中打印此表,但此方法不起作用并立即关闭数据库。
在 DatabaseMenu.h 中:
@interface DatabaseMenu : NSObject {
sqlite3 *databaseMenu;
DataBaseMenu *rowMenu;
}
@property (nonatomic, assign) int ID;
@property (nonatomic, copy)NSString *descrizione;
@property (nonatomic, assign) BOOL haSubMenu;
@property (nonatomic, assign) int IDPadre;
@property (nonatomic, copy) NSString *tabelleFigli;
@property (nonatomic, assign) int ordine;
@property (nonatomic, copy) NSString *custView;
@property (nonatomic, assign) BOOL visMappa;
@property (nonatomic, copy) NSMutableArray *menuArray;
-(NSMutableArray*) loadDataFromMenu;
+(DatabaseMenu*) database;
@end
在 DatabaseMenu.m 中
static DatabaseMenu * database;
+(DatabaseMenu*) database{
if (database==nil) {
database =[[DatabaseMenu alloc]init];
}
return database;
}
-(NSMutableArray*) loadDataFromMenu{
NSString *query = @"SELECT * FROM Menu";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(databaseMenu, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
//.......
}
sqlite3_finalize(statement);
} else {
sqlite3_close(databaseMenu);
}
return self.menuArray;
}
-(id) init{ //open database
self =[super init];
if(self){
NSString *sqliteDb = [[NSBundle mainBundle]pathForResource:@"China" ofType:@"sqlite"];
if(sqlite3_open([sqliteDb UTF8String], &databaseMenu)!=SQLITE_OK){
NSLog(@"Failed to open database");
}else {
NSLog(@"open the database");
[self loadDatiFromMenu];
}
return self;
}
}
调试器无法进入 while 循环。为什么?