Reputation:
I am using this tutorial to implement FMDB in my project. But my code does not return any value. here is the code for retrive value.
-(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;
}
When i check for db than db exist at document directory here is log
CRUD[47363:f803] db /Users/pingdanehog/Library/Application Support/iPhone Simulator/5.1/Applications/D9F19E7D-A232-4C4A-8218-58BC136053A7/Documents/Customers.db
Here is my db
And it contain data
But my resulset value always be null please help this code have run before but when i create new db with same name for this project than it have stopped.
Here is the value of result.
results 0,(null),(null)
I have initialize my DB in appdelegate file
- (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];
}
Upvotes: 0
Views: 869
Reputation: 1279
What happens, if you place the line
NSLog(@"results %i,%@,%@",[results intForColumn:@"id"], ....
behind the [results next], e.g. inside the while loop?
The documentation of FMDB says: "You must always invoke -[FMResultSet next] before attempting to access the values returned in a query, even if you're only expecting one"
Upvotes: 1
Reputation: 4733
Make sure you have initialized your db into AppDelegate or any of your file where you are using FMDB.
If yes & you have initialized it somewhere, then try to run this SQL "SELECT * FROM customers" . in database itself, in SQL tab. And check whether database contains the values or not.
Before that make sure you are running SQL in databse which resides into **Application Support**
Enjoy Programming!
Upvotes: 0