Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rickjackson

macrumors newbie
Original poster
Dec 24, 2010
25
0
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

8bc17718a5991e7c2bcfda715d97b7d200bc1c9957ef75b72f9559e6294a2cdc6g.jpg


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

0be04ac20fac2e93089477020895d1f5ecac5f7582604e1b432daad0aee6c1576g.jpg


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
 
Last edited:
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
 
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.
 
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.

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
 
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.
 
Code:
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databasePath];

Look at that copy again
 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.