Table Not Found Using FMDB

Discussion in 'iOS Programming' started by rickjackson, Mar 29, 2011.

  1. rickjackson, Mar 29, 2011
    Last edited: Mar 29, 2011

    rickjackson macrumors newbie

    Joined:
    Dec 24, 2010
    #1
    Hello,

    I am implementing Sqlite in my project i am using FMDB as a sqlite wrapper in my code. But whenever i insert the records i am getting the following error : DB Error: 1 "no such table: Tradeshow"

    Here is the code :

    Code:
    NSString *databaseName = [[NSString alloc] initWithString:@"iPad.sqlite"];
    	NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentsDir = [documentsPath objectAtIndex:0];
    	NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    	
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	
    	BOOL success = [fileManager fileExistsAtPath:databasePath];
    	
    	if (!success) {
    		
    		NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databasePath];
    		
    		[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    	}
    	
    	[fileManager release];
    	
    	db = [FMDatabase databaseWithPath:databasePath];
    	
    	[db setLogsErrors:TRUE];
    	[db setTraceExecution:TRUE];
    	
    	if (![db open]) {
    		NSLog(@"Failed To Open Database");
    	}
    	else 
    	{
    		NSLog(@"Opened Database Successfully"); 
    		
    		
    		[db executeUpdate:@"insert into Tradeshow (TradeshowID, TradeshowName, TradeshowDescription, TradeshowPlace, TradeshowDate) values (?, ?, ?, ?, ?)" ,
    		 [NSString stringWithFormat:@"number %d", 1],
    		 [NSString stringWithFormat:@"number %d",2],
    		 [NSString stringWithFormat:@"number %d",3],
    		 [NSString stringWithFormat:@"number %d",4],
    		 [NSString stringWithFormat:@"number %d",5] ];
    		
    		FMResultSet *rs = [db executeQuery:@"select TradeshowID from Tradeshow"]; //where a = ?", @"safari's compass"];
    		
          	while ([rs next]) {
    			int RecipeID = [rs intForColumn:@"TradeshowID"];
    		}
    		
            [rs close];
    		
    	}
    Here have a look at my console screen

    [​IMG]

    Here is the image of my Sqlite File its contains the tradeShow table

    [​IMG]

    Here is the link of my Sample App : http://www.filefactory.com/file/ca6c8fc/n/Ipad_Test_2.zip

    i dont know what the issue. Can any 1 point me where i am going wrong
     
  2. rickjackson thread starter macrumors newbie

    Joined:
    Dec 24, 2010
    #2
    Hey Guyz i Figure out 1 thing is that when i navigated to the folder where iphone Simulator host files : /Users/indianic/Library/Application Support/iPhone Simulator then after finding the sqlite file and after opening the sqlite file in SQLITE MANAGER i saw that TABLES ARE NOT CREATED but it contains the sqlite file.

    Now hear i got some confustion i have created the tables then why they are not published inside simulator
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    Are you sure that you're opening the right file? How does your db get into the Documents folder? By default if you ask sqlite to open a file and the file doesn't exist it creates it.
     
  4. rickjackson thread starter macrumors newbie

    Joined:
    Dec 24, 2010
    #4
    Hey Phoney Developer Thanks for your reply....

    Yes I have only 1 sqlite file in my project. so i am opening the right file can u just check out with my sample application so that you can point to some error.


    Thanks
     
  5. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #5
    OK, I downloaded your project and the error was what I said it was. Your code for copying the db from your bundle to the Documents folder is faulty. You don't check any errors in that code (copyItemAtPath both returns a BOOL to indicate success or failure and returns an NSError* in the case of failure). Check both of those and you will understand why your code doesn't work.

    Since there wasn't a valid copy of the db in the Documents folder when you asked sqlite to open the file it made a new one, which of course didn't have your table in it.
     
  6. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #6
    Code:
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databasePath];
    
    Look at that copy again
     
  7. rickjackson thread starter macrumors newbie

    Joined:
    Dec 24, 2010
    #7
    Thanks All Of You For Helping me....

    Phoney Developer special thanks for you.....

    The Problem is with this line

    Code:
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databasePath];
    
    to:
    
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    Thanks Again
     

Share This Page