0

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 enter image description here

And it contain data

enter image description here

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];
}
  • you open database from /Users/pingdanehog/Library/Application Support/iPhone Simulator/5.1/Applications/D9F19E7D-A232-4C4A-8218-58BC136053A7/Documents/Customers.db this or from project folder (directory) – DharaParekh Apr 04 '13 at 13:00
  • @DharaParekh I am copy customer.db when application first time load into document directory and check that is thair already exist or not. –  Apr 04 '13 at 13:02
  • @DharaParekh check my question now i have edited code i used to copy database in document directory –  Apr 04 '13 at 13:05
  • databaseWithPath method creates a database for you if it does not exist already. You should not do it by using NSFileManager. – nsinvocation Apr 04 '13 at 13:06
  • if possible then first check with NSString *dbPath = @"/Users/Tech/Desktop/Farm/Database/Farm.sqlite" means path of db of your project directory. – DharaParekh Apr 04 '13 at 13:10

2 Answers2

1

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"

Jojo.Lechelt
  • 1,259
  • 12
  • 15
0

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!

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
  • Yes i have initialized my db in appdelegate file chaeck my question now i have edited it –  Apr 04 '13 at 13:04
  • But isnt it easy to use FMDB in stead of actual SQL query. and thats not my downvote. –  Apr 04 '13 at 13:09
  • No,by SQL, I meant run this query into the software which you have used for creating db.For ex, mesaSQLite or SQLite Manager or from terminal itself – Niru Mukund Shah Apr 04 '13 at 13:11