Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

My iPhone SQLite Tutorial

lookaflyingdonkeylookaflyingdonkey Posts: 10Registered Users
edited April 2011 in Tutorial Discussion
Hey Guys,

I have been releasing some iPhone SDK tutorials over the past week or so and I just released one on using SQLite databases.

It goes over how to create the actual database through to pulling the data out and displaying it. It is a simple example with a database of animals with their name, description and an image linked to them.

The actual app is a simple TableView which when you select a row (animal) it takes you to a view with their description and their picture.

I am also in the process of writing an addition to the tutorial to allow you to add/remove animals from the database.

Hope that it helps someone.

iPhone SDK Tutorial: Reading data from a SQLite Database | dBlog.com.au

Thanks,
-Dean
Post edited by lookaflyingdonkey on

Replies

  • mekmek Posts: 8New Users
    edited September 2008
    thank you so much
  • jinhaogxyjinhaogxy Posts: 22Registered Users
    edited December 2008
    Thanks a lot.
  • angel.pinazoangel.pinazo Posts: 1New Users
    edited December 2008
    Hey, it's a great tutorial! I was looking for something like this but I only found some snippets and confusing guides.

    Thanks for your effort...! :)
  • Dave_DevDave_Dev Posts: 2New Users
    edited January 2009
    Thank you so much.

    I'm trying to develop an application which uses an SQL database and this tutorial is going to be very helpful.

    Thanks ;)
  • lookaflyingdonkeylookaflyingdonkey Posts: 10Registered Users
    edited March 2009
    Just a quick update, I have published the first installment of my new RSS Reader series that will take you step-by-step through the application development process through to the actual publishing of an application.

    There is a new thread for it over at http://www.iphonedevsdk.com/forum/tutorial-discussion/13358-multi-part-tutorial-advanced-rss-reader-very-depth-idea-publishing.html#post60416

    Remember to grab me on twitter at @bigclick_dean to make sure you dont miss out on an installment!

    Cheers,
    Dean
  • odysseus31173odysseus31173 Posts: 295Registered Users
    edited May 2009
    I'm gonna check out your tutorial when I get home from work later. Thanks in advance
    <a href="http://www.phantomdev.com&quot; target="_blank">Phantom Dev!</a> My blog about tech.
  • xtr33mextr33me Posts: 32Registered Users
    edited May 2009
    Just wanted to thank you for this post. Sounds like it will be just what I am looking for. Thanks again!!
  • alibozorgkhanalibozorgkhan Posts: 2New Users
    edited August 2009
    Hi
    thank you so much for the great tutorial but my program doesn't go through the second if ( if(sqlite3_prepare_v2(...)) { } )and I can't connect to my database. any idea?
    thanx in advance
  • subsksubsk Posts: 1New Users
    edited August 2009
    Check out StepSqlite PL/SQL compiler for Sqlite on iPhone Metatranz StepSqlite alpha It makes writing database interfaces for Sqlite super easy - you can use variables directly in SQL - no more writing binds by hand, write stored procedures, functions, packages, triggers (with procedural code),cursors, even do date operations easily using built in operators -

    There is an easy to follow tutorial on the site.
  • alextrobalextrob Posts: 16Registered Users
    edited January 2010
    Wow – really in-depth tutorial and really useful for learning about SQL on the iPhone.


    A few little things I noticed (not related to SQL):

    1. The tutorial was obviously made for 2.x firmwares, but setText on cells has since been deprecated, so they should be something like this:
    [cell[B].textLabel[/B] setText:animal.name];
    

    2. There's a typo in the AnimalViewController where "animalDescription" is misspelled as "animalDesciption" (the 'r' is missing).

    3. Not sure if this is because of the way I created the view controller, but when I was doing the connections in AnimalViewController.xib, I had to click on the View and make a New Referencing Outlet to the File's Owner's view because otherwise the app crashes saying:
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "AnimalViewController" nib but the view outlet was not set.'
  • Almeister9Almeister9 Posts: 1New Users
    edited January 2010
    Thanks for your tutorial. It was very informative.
    I am an absolute newb and it was very helpful.

    I noticed that you tutorial App downloads its images from the internet.
    I am desperately trying to make an App that stores its images locally so that it will work offline. I have been buying and reading books and scouring the internet but I cannot seem to find out how to do it. Your tutorial is the closest I have been.

    I am trying to build an App that has two levels of Drill-down (I assume two tables to the database) and on the Detail View it shows an Address, a Description, and an image.
    e.g. State - navigate to - Laundromat - navigate to DetailView showing that particular Laundromats Address, Description and image.

    I have been able to find tutorials on two level drill-downs with Detail Views that I have been able to convert to my idea, using Core Data and xml files, sqlite, and even plist files but none of them tell me how to deal with images.

    I have even worked out how to get images into a BLOB in a sqlite database using SQLite Manager, but dont know how to access it in my App.

    Could you possibly show me an amended tutorial showing me how to store images locally in the sqlite database and read them into the Detail View.
    If you could include how to deal with two related tables to make the two level drill-down as well I would be over the moon.

    Thank you for your time in reading this.
    I await in hopeful anticipation.
    Cheers.
  • ejaaejaa Posts: 5New Users
    edited March 2010
    can we edit the data in this program?
  • ManWithMaskManWithMask Posts: 88Registered Users
    edited April 2010
    Hi Dean

    I am new to iPhone SDK.

    I have a simple set of tables which all store one record (e.g. personal details of the iPhone user). So using Arrays does not really make sense for me, since the relationships are all one-to-one.

    I would really appreciate some guidance as there is really zip information I can find on anyone doing this...simply reading data straight from the database to to the view objects. Maybe it can't be done on the iPhone and so I am dealing with old programming habits.

    Here is my code...hope it helps:

    //
    //  IfaAppDelegate.h
    //  Ifa
    //
    //  Created by Marc Tison on 2010/03/21.
    //  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
    //
    
    #import "MyDetailsDatamodel.h"	
    
    @class IfaViewController;
    
    @class MyDetailsDatamodel;
    
    @interface IfaAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        IfaViewController *viewController;
    	
    	//Creat array to hold My Details data item objects
    	NSMutableArray *mydetailsArray;
    		
    }
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet IfaViewController *viewController;
    @property (nonatomic, retain) NSMutableArray *mydetailsArray;
    
    - (void) copyDatabaseIfNeeded;
    - (NSString *) getDBPath;
    
    @end
    //
    //  IfaAppDelegate.m
    //  Ifa
    //
    //  Created by Marc Tison on 2010/03/21.
    //  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
    //
    
    #import "IfaViewController.h"
    
    @implementation IfaAppDelegate
    
    @synthesize window, viewController;
    @synthesize mydetailsArray;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {  
    	
    	//Copy database to the user's phone if needed.
    	[self copyDatabaseIfNeeded];
    	
    	//Initialize the My Details array.
    	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    	self.mydetailsArray = tempArray;
    	[tempArray release];
    	
    	//Once the db is copied, get the initial data to display on the screen.
    	[MyDetailsDatamodel getInitialDataToDisplay:[self getDBPath]];
    
    	
    	// Configure and show the window
    	[window addSubview:viewController.view];  
        [window makeKeyAndVisible];
    	
    }
    - (void)applicationWillTerminate:(UIApplication *)application {
    	
    	// Save data if appropriate
    	[MyDetailsDatamodel finalizeStatements];
    }
    //we get the NSFileManager object to make a copy of the database on the phone
    - (void) copyDatabaseIfNeeded {
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	NSError *error;
    	NSString *dbPath = [self getDBPath];
    	BOOL success = [fileManager fileExistsAtPath:dbPath];
    	
    	if(!success) {
    		
    		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@iFAdvisor.sql];
    		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    		
    		if (!success)
    			NSAssert1(0, @Failed to create writable database file with message '%@'.", [error localizedDescription]);
    	}
    }
    //Gives us the database location on the user's phone
    - (NSString *) getDBPath {
    		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    	NSString *documentsDir = [paths objectAtIndex:0];
    	return [documentsDir stringByAppendingPathComponent:@iFAdvisor.sql];
    }
    //Clear memory
    - (void)dealloc {
    	[mydetailsArray release];
    	[viewController release];
    	[window release];
    	[super dealloc];
    }
    @end
    //
    //  MyDetails.h
    //  View
    //
    //  Created by marcti on 2010/03/17.
    //  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
    //
    
    #import <sqlite3.h>
    
    @interface MyDetailsDatamodel : NSObject {
    	
    	//Declare database fields
    	NSInteger PKMyDetails;
    	NSString *Name;
    	NSString *Surname;
    	NSString *DateOfBirth;
    	NSString *Gender;
    	NSString *IdNumber;
    	NSString *CellNumber;
    	NSString *EmailAddress;
    	
    	//Internal variables to keep track of the state of the object.
    	BOOL isDirty;
    	BOOL isDetailViewHydrated;
    	
    }
    
    @property (nonatomic, readonly) NSInteger PKMyDetails;
    @property (nonatomic, retain) NSString *Name;
    @property (nonatomic, retain) NSString *Surname;
    @property (nonatomic, retain) NSString *DateOfBirth;
    @property (nonatomic, retain) NSString *Gender;
    @property (nonatomic, retain) NSString *IdNumber;
    @property (nonatomic, retain) NSString *CellNumber;
    @property (nonatomic, retain) NSString *EmailAddress;
    
    @property (nonatomic, readwrite) BOOL isDirty;
    @property (nonatomic, readwrite) BOOL isDetailViewHydrated;
    
    //Static methods.
    + (void) getInitialDataToDisplay:(NSString *)dbPath;
    + (void) finalizeStatements;
    
    //Instance methods.
    - (id) initWithPrimaryKey:(NSInteger)pk;
    
    @end
    //
    //  MyDetails.m
    //  View
    //
    //  Created by marcti on 2010/03/17.
    //  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
    //
    
    #import "MyDetailsDatamodel.h"
    
    static sqlite3 *database = nil;
    
    @implementation MyDetailsDatamodel
    
    @synthesize PKMyDetails, Name,Surname,DateOfBirth,Gender,IdNumber,CellNumber,EmailAddress;
    @synthesize isDirty,isDetailViewHydrated;
    
    + (void) getInitialDataToDisplay:(NSString *)dbPath {
    	
    	IfaAppDelegate *appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];
    	
    	if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    		
    		const char *sql = "select PKMyDetails, Name, Surname, IdNumber, Gender,DateOfBirth,CellNumber,EmailAddress from MyDetails";
    		sqlite3_stmt *selectstmt;
    		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
    			
    			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
    				
    				NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
    				MyDetailsDatamodel *mydetailsObj = [[MyDetailsDatamodel alloc] initWithPrimaryKey:primaryKey];
    				mydetailsObj.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
    				mydetailsObj.Surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
    				mydetailsObj.IdNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
    				mydetailsObj.Gender = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
    				mydetailsObj.DateOfBirth = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
    				mydetailsObj.CellNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
    				mydetailsObj.EmailAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
    				
    				mydetailsObj.isDirty = NO;
    				
    				[appDelegate.mydetailsArray addObject:mydetailsObj];
    				
    				[mydetailsObj release];
    			}
    		}
    	}
    	else
    		sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }
    + (void) finalizeStatements {
    	
    	if(database) sqlite3_close(database);
    }
    
    //Initialise the primary key
    - (id) initWithPrimaryKey:(NSInteger) pk {
    	
    	[super init];
    	PKMyDetails = pk;
    	
    	isDetailViewHydrated = NO;
    	
    	return self;
    }
    
    - (void)dealloc {
    	[Name release];
    	[Surname release];
    	[IdNumber release];
    	[Gender release];
    	[DateOfBirth release];
    	[CellNumber release];
    	[EmailAddress release];
        [super dealloc];
    }
    
    @end
    //  MyDetailsController.m
    //  iFA
    //
    //  Created by marcti on 2010/03/10.
    //  Copyright 2010 Marblesharp 147 (Pty) Ltd. All rights reserved.
    //
    
    #import "MyDetailsController.h"
    #import "MyDetailsDatamodel.h"
    
    @implementation MyDetailsController
    
    @synthesize myhealthController, theScroller;
    @synthesize NameField, SurnameField, IdNumberField, CellNumberField,EmailAddressField;
    @synthesize soundFileURLRef, soundFileObject;
    
    // Implement viewDidLoad to do additional setup after loading the view
    - (void) viewDidLoad {
    	
    	[super viewDidLoad];
    	
    	theScroller.contentSize=CGSizeMake(280, 650);
    	
    	// Get the main bundle for the app
    	CFBundleRef mainBundle;
    	mainBundle = CFBundleGetMainBundle ();
    	
    	// Get the URL to the sound file to play
    	soundFileURLRef  =	CFBundleCopyResourceURL (mainBundle,CFSTR ("tap"),CFSTR ("aif"),NULL);
    	
    	// Create a system sound object representing the sound file
    	AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
    	
    	//Using the appDelegate we can access the mydetailsArray
    	appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    	//return appDelegate.mydetailsArray.count;
    	if(appDelegate.mydetailsArray != nil){
    	//	NSLog(@My Details Array:%@",appDelegate.mydetailsArray);
    		//**** we get here, but myDetailsArray is undeclared?
    		//NameField.text = mydetailsArray.Name;
    		//SurnameField.text = mydetailsArray.Surname;
    		//IdNumberField.text = mydetailsArray.IdNumber;
    		//CellNumberField.text = mydetailsArray.CellNumber;
    		//EmailAddressField.text = mydetailsArray.EmailAddress;
    	}		
    	
    }	
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    - (void)dealloc {
    	[theScroller release];
    	[myhealthController release];
    	//	[mydetailsDatamodel release];
    	[super dealloc];
    }
    @end
    
    <b>iPhone Apps</b>: Who SAid™, iFRICA™, iFA™, iReceipt™,eCash™
  • pyr0b0ypyr0b0y Posts: 15Registered Users
    edited April 2010
    The link doesn't work, can you repost it please?
  • ManWithMaskManWithMask Posts: 88Registered Users
    edited April 2010
    Hi Dean

    Thanks I managed to get it working last night. With singletons and via my view controller!

    Marc
    <b>iPhone Apps</b>: Who SAid™, iFRICA™, iFA™, iReceipt™,eCash™
  • SLezlSLezl Posts: 1New Users
    edited June 2010
    Hi all, I've recently accidently deleted my iPhone calendar and all I could find back was one back-up file. I extracted the calender with the iPhone backup extractor . The callender was a sqlitedb file, which I converted into an excell file. Then I sorted the events by date. So I got pretty far in retrieving my appointments :)

    My question is: How do you read the start and end dates? I've tried finding explanations on it, unsuccesfully and with common sense I alse don't get any wiser.

    calendersqlite.th.jpg
  • B-DubbzB-Dubbz Posts: 43Registered Users
    edited July 2010
    Does anyone know how to delete a database entry? I added the delete button and it deletes the row, but it does not delete it from the database.
  • odxodx Posts: 2New Users
    edited July 2010
    B-Dubbz wrote: »
    Does anyone know how to delete a database entry? I added the delete button and it deletes the row, but it does not delete it from the database.

    You would have to send a statement like "DELETE FROM animals WHERE name ='foo'" to your database.
  • aminfoaminfo Posts: 2New Users
    edited July 2010
    Thanks for the SQLite tutorial. I was able to copy it, change it to my setup and variables -- and load data from SQLite with NO ERRORS!
  • deepak.hwsdeepak.hws Posts: 1New Users
    edited September 2010
    In this code you have use
    + (void) getInitialDataToDisplay:(NSString *)dbPath for fetching database.

    here sqlite fetching data sequentially in row by row, it take much time when data record's more then (approx 100). So application response is very low.

    Can you suggest how to increase application response time?

    Thanks
    Deepak :)
    Hi Dean

    I am new to iPhone SDK.

    I have a simple set of tables which all store one record (e.g. personal details of the iPhone user). So using Arrays does not really make sense for me, since the relationships are all one-to-one.

    I would really appreciate some guidance as there is really zip information I can find on anyone doing this...simply reading data straight from the database to to the view objects. Maybe it can't be done on the iPhone and so I am dealing with old programming habits.

    Here is my code...hope it helps:

    //
    //  IfaAppDelegate.h
    //  Ifa
    //
    //  Created by Marc Tison on 2010/03/21.
    //  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
    //
    
    #import "MyDetailsDatamodel.h"	
    
    @class IfaViewController;
    
    @class MyDetailsDatamodel;
    
    @interface IfaAppDelegate : NSObject <UIApplicationDelegate> {
        UIWindow *window;
        IfaViewController *viewController;
    	
    	//Creat array to hold My Details data item objects
    	NSMutableArray *mydetailsArray;
    		
    }
    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet IfaViewController *viewController;
    @property (nonatomic, retain) NSMutableArray *mydetailsArray;
    
    - (void) copyDatabaseIfNeeded;
    - (NSString *) getDBPath;
    
    @end
    //
    //  IfaAppDelegate.m
    //  Ifa
    //
    //  Created by Marc Tison on 2010/03/21.
    //  Copyright Marblesharp (Pty) Ltd 2010. All rights reserved.
    //
    
    #import "IfaViewController.h"
    
    @implementation IfaAppDelegate
    
    @synthesize window, viewController;
    @synthesize mydetailsArray;
    
    - (void)applicationDidFinishLaunching:(UIApplication *)application {  
    	
    	//Copy database to the user's phone if needed.
    	[self copyDatabaseIfNeeded];
    	
    	//Initialize the My Details array.
    	NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    	self.mydetailsArray = tempArray;
    	[tempArray release];
    	
    	//Once the db is copied, get the initial data to display on the screen.
    	[MyDetailsDatamodel getInitialDataToDisplay:[self getDBPath]];
    
    	
    	// Configure and show the window
    	[window addSubview:viewController.view];  
        [window makeKeyAndVisible];
    	
    }
    - (void)applicationWillTerminate:(UIApplication *)application {
    	
    	// Save data if appropriate
    	[MyDetailsDatamodel finalizeStatements];
    }
    //we get the NSFileManager object to make a copy of the database on the phone
    - (void) copyDatabaseIfNeeded {
    	NSFileManager *fileManager = [NSFileManager defaultManager];
    	NSError *error;
    	NSString *dbPath = [self getDBPath];
    	BOOL success = [fileManager fileExistsAtPath:dbPath];
    	
    	if(!success) {
    		
    		NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@iFAdvisor.sql];
    		success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
    		
    		if (!success)
    			NSAssert1(0, @Failed to create writable database file with message '%@'.", [error localizedDescription]);
    	}
    }
    //Gives us the database location on the user's phone
    - (NSString *) getDBPath {
    		NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    	NSString *documentsDir = [paths objectAtIndex:0];
    	return [documentsDir stringByAppendingPathComponent:@iFAdvisor.sql];
    }
    //Clear memory
    - (void)dealloc {
    	[mydetailsArray release];
    	[viewController release];
    	[window release];
    	[super dealloc];
    }
    @end
    //
    //  MyDetails.h
    //  View
    //
    //  Created by marcti on 2010/03/17.
    //  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
    //
    
    #import <sqlite3.h>
    
    @interface MyDetailsDatamodel : NSObject {
    	
    	//Declare database fields
    	NSInteger PKMyDetails;
    	NSString *Name;
    	NSString *Surname;
    	NSString *DateOfBirth;
    	NSString *Gender;
    	NSString *IdNumber;
    	NSString *CellNumber;
    	NSString *EmailAddress;
    	
    	//Internal variables to keep track of the state of the object.
    	BOOL isDirty;
    	BOOL isDetailViewHydrated;
    	
    }
    
    @property (nonatomic, readonly) NSInteger PKMyDetails;
    @property (nonatomic, retain) NSString *Name;
    @property (nonatomic, retain) NSString *Surname;
    @property (nonatomic, retain) NSString *DateOfBirth;
    @property (nonatomic, retain) NSString *Gender;
    @property (nonatomic, retain) NSString *IdNumber;
    @property (nonatomic, retain) NSString *CellNumber;
    @property (nonatomic, retain) NSString *EmailAddress;
    
    @property (nonatomic, readwrite) BOOL isDirty;
    @property (nonatomic, readwrite) BOOL isDetailViewHydrated;
    
    //Static methods.
    + (void) getInitialDataToDisplay:(NSString *)dbPath;
    + (void) finalizeStatements;
    
    //Instance methods.
    - (id) initWithPrimaryKey:(NSInteger)pk;
    
    @end
    //
    //  MyDetails.m
    //  View
    //
    //  Created by marcti on 2010/03/17.
    //  Copyright 2010 Marblesharp (Pty) Ltd. All rights reserved.
    //
    
    #import "MyDetailsDatamodel.h"
    
    static sqlite3 *database = nil;
    
    @implementation MyDetailsDatamodel
    
    @synthesize PKMyDetails, Name,Surname,DateOfBirth,Gender,IdNumber,CellNumber,EmailAddress;
    @synthesize isDirty,isDetailViewHydrated;
    
    + (void) getInitialDataToDisplay:(NSString *)dbPath {
    	
    	IfaAppDelegate *appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];
    	
    	if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    		
    		const char *sql = "select PKMyDetails, Name, Surname, IdNumber, Gender,DateOfBirth,CellNumber,EmailAddress from MyDetails";
    		sqlite3_stmt *selectstmt;
    		if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
    			
    			while(sqlite3_step(selectstmt) == SQLITE_ROW) {
    				
    				NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
    				MyDetailsDatamodel *mydetailsObj = [[MyDetailsDatamodel alloc] initWithPrimaryKey:primaryKey];
    				mydetailsObj.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
    				mydetailsObj.Surname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
    				mydetailsObj.IdNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
    				mydetailsObj.Gender = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
    				mydetailsObj.DateOfBirth = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];
    				mydetailsObj.CellNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 6)];
    				mydetailsObj.EmailAddress = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 7)];
    				
    				mydetailsObj.isDirty = NO;
    				
    				[appDelegate.mydetailsArray addObject:mydetailsObj];
    				
    				[mydetailsObj release];
    			}
    		}
    	}
    	else
    		sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
    }
    + (void) finalizeStatements {
    	
    	if(database) sqlite3_close(database);
    }
    
    //Initialise the primary key
    - (id) initWithPrimaryKey:(NSInteger) pk {
    	
    	[super init];
    	PKMyDetails = pk;
    	
    	isDetailViewHydrated = NO;
    	
    	return self;
    }
    
    - (void)dealloc {
    	[Name release];
    	[Surname release];
    	[IdNumber release];
    	[Gender release];
    	[DateOfBirth release];
    	[CellNumber release];
    	[EmailAddress release];
        [super dealloc];
    }
    
    @end
    //  MyDetailsController.m
    //  iFA
    //
    //  Created by marcti on 2010/03/10.
    //  Copyright 2010 Marblesharp 147 (Pty) Ltd. All rights reserved.
    //
    
    #import "MyDetailsController.h"
    #import "MyDetailsDatamodel.h"
    
    @implementation MyDetailsController
    
    @synthesize myhealthController, theScroller;
    @synthesize NameField, SurnameField, IdNumberField, CellNumberField,EmailAddressField;
    @synthesize soundFileURLRef, soundFileObject;
    
    // Implement viewDidLoad to do additional setup after loading the view
    - (void) viewDidLoad {
    	
    	[super viewDidLoad];
    	
    	theScroller.contentSize=CGSizeMake(280, 650);
    	
    	// Get the main bundle for the app
    	CFBundleRef mainBundle;
    	mainBundle = CFBundleGetMainBundle ();
    	
    	// Get the URL to the sound file to play
    	soundFileURLRef  =	CFBundleCopyResourceURL (mainBundle,CFSTR ("tap"),CFSTR ("aif"),NULL);
    	
    	// Create a system sound object representing the sound file
    	AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
    	
    	//Using the appDelegate we can access the mydetailsArray
    	appDelegate = (IfaAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    	//return appDelegate.mydetailsArray.count;
    	if(appDelegate.mydetailsArray != nil){
    	//	NSLog(@My Details Array:%@",appDelegate.mydetailsArray);
    		//**** we get here, but myDetailsArray is undeclared?
    		//NameField.text = mydetailsArray.Name;
    		//SurnameField.text = mydetailsArray.Surname;
    		//IdNumberField.text = mydetailsArray.IdNumber;
    		//CellNumberField.text = mydetailsArray.CellNumber;
    		//EmailAddressField.text = mydetailsArray.EmailAddress;
    	}		
    	
    }	
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    - (void)dealloc {
    	[theScroller release];
    	[myhealthController release];
    	//	[mydetailsDatamodel release];
    	[super dealloc];
    }
    @end
    
  • longstlongst Posts: 115Registered Users
    edited October 2010
    Great tutorial ! by the way if there are some same kinds of tutorial talking about write data to SQLite
    Thank you
  • iPhone918iPhone918 Posts: 6New Users
    edited April 2011
    thanks alot. I will learn it...
Sign In or Register to comment.