嗨,我遇到了一些代码。我有一个文件 catalog.db 和一个让我使用它的类。当我尝试从数据库中检索数据时,它似乎是空的。通过几个 nslog,我可以看到它连接并进入数据库,我可以看到它进入其中,但它不会从中获得任何价值。我试图查看外部数据库管理器软件的查询是否有问题,并且查询工作正常......
这是我的课
#import "DBAccess.h"
sqlite3* database;
@implementation DBAccess
-(id)init{
self = [super init];
if (self){
[self initializeDatabase];
}
return self;
}
-(void)initializeDatabase{
NSString *path = [[NSBundle mainBundle]pathForResource:@"catalog" ofType:@"db"];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) { NSLog(@"Opening Database"); }
else {
sqlite3_close(database);
NSAssert1(0, @"FAILED to open database '%s'", sqlite3_errmsg(database));
}
}
-(void)closeDatabase{
if (sqlite3_close(database) != SQLITE_OK){
NSAssert1(0, @"ERROR to close databse: '%s'", sqlite3_errmsg(database));
}
}
-(NSMutableArray *)getAllProduct{
NSMutableArray *products = [[NSMutableArray alloc]init];
const char *sql = "SELECT product.ID,product.Name, Manufacturer.name, product.details,product.price, product.quantityOnHand,country.country,product.image FROM product,manufacturer,country WHERE manufacturer.manufacturerID = product.manufacturerID and product.countryOfOriginID = country.countryID";
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
NSLog(@"sqlResult: %d", sqlResult);
if (sqlResult == SQLITE_OK){
NSLog(@"sql step statement: %d",sqlite3_step(statement));
NSLog(@"QUERY DONE");
while (sqlite3_step(statement) == SQLITE_ROW){
NSLog(@"TEST");
Product *product = [[Product alloc]init];
char *name = (char *)sqlite3_column_text(statement, 1);
NSLog(@"n %s",name);
char *manufacturer = (char *)sqlite3_column_text(statement, 2);
NSLog(@"m %s", manufacturer);
char *details = (char *)sqlite3_column_text(statement, 3);
NSLog(@"d %s", details);
char *countryoforigin = (char *)sqlite3_column_text(statement, 6);
NSLog(@"%s", countryoforigin);
char *image = (char *)sqlite3_column_text(statement, 7);
NSLog(@"%s", image);
product.ID = sqlite3_column_text(statement, 0);
product.name = (name)?[NSString stringWithUTF8String:name]:@"";
product.manufacturer = (manufacturer)?[NSString stringWithUTF8String:manufacturer]:@"";
product.details = (details)?[NSString stringWithUTF8String:details]:@"";
product.price = sqlite3_column_double (statement, 4);
product.quantity = sqlite3_column_int(statement, 5);
product.countryOfOrigin = (countryoforigin)?[NSString stringWithUTF8String:countryoforigin]:@"";
product.image = (image)?[NSString stringWithUTF8String:image]:@"";
[products addObject:product];
}
sqlite3_finalize(statement);
}
else {
NSLog(@"Problem with database %d",sqlResult);
}
return products;
}
@end
这就是我在控制台中得到的
2013-08-14 12:38:58.505 Catalog[1642:c07] Opening Database
2013-08-14 12:38:58.508 Catalog[1642:c07] sqlResult: 0
2013-08-14 12:38:58.509 Catalog[1642:c07] sql step statement: 101
2013-08-14 12:38:58.509 Catalog[1642:c07] QUERY DONE
2013-08-14 12:38:58.510 Catalog[1642:c07] ()
我的问题是什么?谢谢