iphone apps with database(SQLITE manager)


In this tutorial we will see how fetch the data from the database into table view.  For doing this we will have to create the database in SQLITE manager and then import in our apps . This is the navigation based application. Here are some steps for it…


1. Open the xcode & choose "File->New Project".

2. Select "Application" from left menu and then "View-based Application".

3.Name your project as "DatabasePrj" and save the project.

4.Here we need to import the sqlite framework from the library.and

  right click on framework and add the libsqlite3.0 framework.

5.Now add the database file into resource folder.(Right click on to the resource folder and the database file which we have created in sqlite manager).


 

 

6.Now just select "RootViewController.h" file from left menu .

 

 

 

 

#import <UIKit/UIKit.h>

#import <sqlite3.h>


@interface RootViewController : UITableViewController {

}

 

 

 

7. Now select "RootViewController.m" file from menu for implementation.

 

#import "RootViewController.h"

#import "Animal.h"

#import "DatabasePrjAppDelegate.h"

#import "DetailViewController.h"


@implementation RootViewController



#pragma mark -

#pragma mark View lifecycle


- (void)viewDidLoad {

    [super viewDidLoad];

self.navigationItem.title=@"Title";

    

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}



// Customize the number of rows in the table view.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

DatabasePrjAppDelegate *appDelegate = (DatabasePrjAppDelegate *)[[UIApplication sharedApplication] delegate];

return appDelegate.animals.count;


}



// Customize the appearance of table view cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    DatabasePrjAppDelegate *appDelegate = (DatabasePrjAppDelegate *)[[UIApplication sharedApplication] delegate];

Animal *animal = (Animal *)[appDelegate.animals objectAtIndex:indexPath.row];

cell.textLabel.text=animal.name;

// Configure the cell.


    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    

DatabasePrjAppDelegate *appDelegate = (DatabasePrjAppDelegate *)[[UIApplication sharedApplication] delegate];

Animal *animal = (Animal *)[appDelegate.animals objectAtIndex:indexPath.row];

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

//UIImage *animalImage = [[UIImage alloc] imag:imageData cache:YES];

    // ...

     // Pass the selected object to the new view controller.

[self.navigationController pushViewController:detailViewController animated:YES];

 

[detailViewController.txt setText:[animal  discription]];


detailViewController.title = [animal name];

[detailViewController release];

 

}



#pragma mark -

#pragma mark Memory management


- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    

    // Relinquish ownership any cached data, images, etc that aren't in use.

}


- (void)viewDidUnload {

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.

    // For example: self.myOutlet = nil;

}



- (void)dealloc {

    [super dealloc];

}



@end

 

9. Then Quit and Save the Interface Builder.


10. Now you are ready for  click on "Build & Go" button. 


Now you can see the output in iphone simulator .

 

 

 

 



(1) Responses to this post

Rochi May 05 2011 at 05:59:03

YouÂ’re a real deep thinker. Thanks for shiarng.


Leave your comment here






User Login




New User Forgot password

Newsletter Subscribe

Advertisement

 
 
 
quick contact