Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-12-2010, 11:26 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 30
Red face sqlite3_prepare_v2 function returns SQLITE_NOTADB

Hello,

I'm trying a simple SQLite tutorial and I have a small problem.
My code is the following:

Code:

-(id) init
{
	// Setup some globals
	databaseName = @"animals.sql";
	
	// Get the path to the documents directory and append the databaseName
	NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDir = [documentPaths objectAtIndex:0];
	databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
	
	return self;
}

-(void) checkAndCreateDatabase{
	// Check if the SQL database has already been saved to the users phone, if not then copy it over
	BOOL success;
	
	// Create a FileManager object, we will use this to check the status
	// of the database and to copy it over if required
	NSFileManager *fileManager = [NSFileManager defaultManager];
	
	// Check if the database has already been created in the users filesystem
	success = [fileManager fileExistsAtPath:databasePath];

	// If the database already exists then return without doing anything
	// TODO uncheck this if(success) return;
	
	// If not then proceed to copy the database from the application to the users filesystem
	
	// Get the path to the database in the application package
	NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
	
	// Copy the database from the package to the users filesystem
	[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
	
	//[fileManager release];
}


-(NSMutableArray*) getBrands {
	
	// Setup the database object
	sqlite3 *database;
	
	// Init the animals Array
	NSMutableArray *brands = [[NSMutableArray alloc] init];
	
	// Open the database from the users filessytem
	if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
		
		// Setup the SQL Statement and compile it for faster access
		const char *sqlStatement = "select * from animals";
		sqlite3_stmt *compiledStatement;
		
		NSLog(@"sql run = %i",sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL));
		
		if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
		{
			// Loop through the results and add them to the feeds array
			while(sqlite3_step(compiledStatement) == SQLITE_ROW) 
			{
				
				// Read the data from the result row
				NSString *brandName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
							
				[brands addObject:brandName];
				
				//[brandName release];
				
			}
		}
		
		// Release the compiled statement from memory
		sqlite3_finalize(compiledStatement);
		
	}
	
	sqlite3_close(database);
	
	return brands;
	
}
The problem is that my database gets copied right, but somehow on the red line above I get return int 26 which means SQLITE_NOTADB -> that my file is not a DB.

My file animals.sql is the following:

Code:
CREATE TABLE animals ( id INTEGER PRIMARY KEY, name VARCHAR(50), description TEXT, image VARCHAR(255) );

INSERT INTO animals (name, description, image) VALUES ('Elephant', 'desc1', 'img1.jpg');
INSERT INTO animals (name, description, image) VALUES ('Monkey', 'desc2', 'img2.jpg');
Any ideas on why I get that error message? I have everything right, the file structure, it copies right where it should be, but somehow it doesn't recognizes it as a DB. I have added also sqlite3.h and libsqlite3.0.dylib. I really don't know what's wrong.
Thank you in advance for taking time to read my post.

Last edited by an5w3r_ro; 02-12-2010 at 11:29 AM.
an5w3r_ro is offline   Reply With Quote
Old 02-13-2010, 02:39 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 30
Default

Hehe So stupid from here... beeing my first time when I play with SQLite I think I should be forgiven :P

You need to add to the project the compiled version of the sql, after you run it in terminal with sqlite3 and also you need it to be saved with the sqlite extension.
I hope this will help others not to do the same mistakes as I did
an5w3r_ro is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 241
12 members and 229 guests
ADY, CKAmike, Dani77, Duncan C, HemiMG, nick.keroulis, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:22 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0