我正在使用this教程在我的项目中实现FMDB。但是我的代码没有返回任何值。这是反向价值的代码。
-(NSMutableArray *) getCustomers
{
NSMutableArray *customers = [[NSMutableArray alloc] init];
FMDatabase *db = [FMDatabase databaseWithPath:[Utility getDatabasePath]];
[db open];
FMResultSet *results = [db executeQuery:@"SELECT * FROM customers"];
NSLog(@"results %i,%@,%@",[results intForColumn:@"id"],[results stringForColumn:@"firstname"],[results stringForColumn:@"lastname"]);
while([results next])
{
Customer *customer = [[Customer alloc] init];
customer.customerId = [results intForColumn:@"id"];
customer.firstName = [results stringForColumn:@"firstname"];
customer.lastName = [results stringForColumn:@"lastname"];
[customers addObject:customer];
}
[db close];
return customers;
}
当我在文件目录中检查db而不是db时,这里是log
CRUD[47363:f803] db /Users/pingdanehog/Library/Application Support/iPhone Simulator/5.1/Applications/D9F19E7D-A232-4C4A-8218-58BC136053A7/Documents/Customers.db
这是我的数据库
它包含数据
但是我的结果值总是为空,请帮助此代码运行之前,但是当我为这个项目创建一个具有相同名称的新数据库而不是它已停止时。
这是结果的值。
results 0,(null),(null)
我已在appdelegate文件中初始化我的数据库
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
databaseName = @"Customers.db";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
databasePath = [documentDir stringByAppendingPathComponent:databaseName];
[self createAndCheckDatabase];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
-(void) createAndCheckDatabase
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
答案 0 :(得分:1)
如果你放行
,会发生什么NSLog(@"结果%i,%@,%@",[结果intForColumn:@" id"],....
在[结果下一步]后面,例如在while循环中?
FMDB的文档说: "在尝试访问查询中返回的值之前,您必须始终调用 - [FMResultSet next],即使您只期望一个"
答案 1 :(得分:0)
确保已将数据库初始化为AppDelegate或您使用FMDB的任何文件。
如果是和&你已经在某处初始化它,然后尝试运行这个SQL“SELECT * FROM customers”。在数据库本身,在SQL选项卡中。并检查数据库是否包含值。
Before that make sure you are running SQL in databse which resides into **Application Support**
享受编程!