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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 07-07-2010, 05:44 PM   #1 (permalink)
zno
Registered Member
 
Join Date: Jun 2010
Posts: 51
zno is on a distinguished road
Default Can't retrieve query results from SQLite

Hi I'm trying to fill my tableView with results from the database, I've been using the same database in my java project so there's nothing wrong with the database.

Thing is my tableView never gets any content and putting some NSLogs here and there I've found which if statement it can't get past. I've marked the if statement in red.

Thanks in advance !

Code:
#import "InspectionAppDelegate.h"
#import "RootViewController.h"
#import "Spaces.h"


@implementation InspectionAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize databasePath,databaseName,queryResult;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    // Override point for customization after app launch    
	// Setup some globals
	databaseName = @"BesManLite.db3";
	
	// 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];
	
	// Execute the "checkAndCreateDatabase" function
	[self checkAndCreateDatabase];
	
	// Query the database for all animal records and construct the "animals" array
	[self readSpacesFromDatabase];
	
	[window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
	return YES;
}
-(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
	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];
}
-(void) readSpacesFromDatabase {
	// Setup the database object
	sqlite3 *database;
	
	// Init the animals Array
	queryResult = [[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 Name FROM Utrymme";
		sqlite3_stmt *compiledStatement;
		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 *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
				//NSString *aDescription = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
				//NSString *aImageUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
				NSLog(@"supposed to get statement");
				// Create a new animal object with the data from the database
				Spaces *s = [[Spaces alloc] initWithName:aName];
				
				// Add the animal object to the animals Array
				[queryResult addObject:s];
				
				[s release];
			}
		}
		// Release the compiled statement from memory
		sqlite3_finalize(compiledStatement);
		
	}
	sqlite3_close(database);
	
}


- (void)applicationWillTerminate:(UIApplication *)application {
	// Save data if appropriate
}


#pragma mark -
#pragma mark Memory management

- (void)dealloc {
	[databaseName release];
	[databasePath release];
	[queryResult release];
	[navigationController release];
	[window release];
	[super dealloc];
}


@end
zno is offline   Reply With Quote
Old 07-07-2010, 08:13 PM   #2 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
gud4nuthin is on a distinguished road
Default

i doubt this line
const char *sqlStatement = "SELECT Name FROM Utrymme";
gud4nuthin is offline   Reply With Quote
Old 07-08-2010, 08:15 AM   #3 (permalink)
zno
Registered Member
 
Join Date: Jun 2010
Posts: 51
zno is on a distinguished road
Default

Quote:
i doubt this line
const char *sqlStatement = "SELECT Name FROM Utrymme";
Why do you doubt it though ? Should I change it to an NSString instead ? Should I add a semi colon at the end of the sql command ? It works this way with java, I'm really banging my head on this one.
zno is offline   Reply With Quote
Old 07-08-2010, 10:43 AM   #4 (permalink)
zno
Registered Member
 
Join Date: Jun 2010
Posts: 51
zno is on a distinguished road
Default

Does anyone think that it's because of the format my sqlite3 database is in ? namely .db3 ? In all the tutorials people seem to have .sqlite I really don't know what the difference is.
zno 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: 343
14 members and 329 guests
akacaj, c2matrix, cgokey, esoteric, EXOPTENDAELAX, GHuebner, givensur, HemiMG, Mirotion22, mjnafjke, Pudding, SLIC, Sonuye857, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,652
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Sonuye857
Powered by vBadvanced CMPS v3.1.0

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