Вы находитесь на странице: 1из 8

Tutorial on Creating an iOS SQLite Database Application |

iOS | iPhone
(See figure 12, 13).
@synthesize winerating;
MyWine.wineId = sqlite3_column_int(sqlStatement, 0);
[self.winename setText:((WineList *) [self.wines objectAtIndex:0]).wine];
if (++currentIndex == [self.wines count])
Finally implement the GetWineListing method, therefore when the user clicks around the button, the
index gets incremented as well as retrieves the information in the selected index number.
If the particular database is effectively opened, your sqlite3_prepare will try to execute the
particular sqlStatement. With Regard To the particular image, repeat exactly the actual same
operation as the two preceding ones. Setup the getter along with setters:
[self setWinename:nil];
For the particular sake associated with this tutorial, I will pre-populate the database by
incorporating wine entries and also images from the web. Select your Objective-C node then the
particular Objective-C class template.
NSString *dbPath = [[[NSBundle mainBundle] resourcePath
]stringByAppendingPathComponent:@"IOSDB.sqlite"];
In the viewDidLoad, which may be referred for you to as once the app is actually finished initializing
itself, add pointers to keep the particular original data within the array and thus the app will display
some information as well as image that is located at index 0.
//
Next declare a new NSFileManager object, any NSString object along along with a Bool object:
NSLog(@"An exception occured: %@", [exception reason]);
const char *sql = "SELECT id, Wine, Rating, Photo FROM WineTbl";
- (void)viewDidLoad
// MyWineLists.m
MyWine.wine = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
int rawLen = sqlite3_column_bytes(sqlStatement, 3);
Source Code
sqlite3_stmt *sqlStatement;
Click around the new Database button (figure 2) to create a brand name new database. In Addition
add a NSMutableArray object:
#import "MyWineLists.h"
@synthesize wines;
NSString *dbPath = [[[NSBundle mainBundle] resourcePath
]stringByAppendingPathComponent:@"IOSDB.sqlite"];
//
if(!success)
whilst (sqlite3_step(sqlStatement)==SQLITE_ROW)
sqlite3_stmt *sqlStatement;
The dbPath will contain for the filename along with path with the SQLite database which usually will
be passed to the fileMgr. This particular connection will even be an IBOutlet and the identify will be
: wineViewer. Just About All legal rights reserved.
NSLog(@"An error has occured.");
. Click on the run button to launch your current app. Just Take note where you're saving it simply
because you tend to be most likely to copy your file later on in your project.
The SQLite Manager can be launched from the Firefox menu or perhaps Equipment menu depending
around the edition you are utilizing (see figure 1).
Ok, we are done. Subsequent test to see in the event the file had been located of course, if not
necessarily log an error. the next step will involve environment up the actual UI, creating IBActions
and also IBOutlets connections. With Regard To this tutorial it is definitely just a R (read) operation.
next create a couple of Objective-C implementation files and also corresponding header files. Title it
"Model". In the event that your statement will be successfully executed resulting in the result set
becoming returned, then execute a while loop in order to traverse the result set assigning the values
towards the NSMutableArray fields.
NSLog(@"An exception occured: %@", [exception reason]);
const char *raw = sqlite3_column_blob(sqlStatement, 3);
NSLog(@"Cannot locate database file '%@'.", dbPath);
Next open up the particular WineList.m (implementation) file in order to create the actual getter as
well as setter methods. Thus to become able to setup the particular IBAction as well as IBOutlet,
open the header file alongside the actual storyboard by simply clicking on the Assistant Editor, the
face icon inside the Toolbar (Figure 14). Give it a meaningful identify along with select Storyboard
and ARC. open it and open the particular lib folder. A Person should have the single blank scene
(View Controller). Ok now the applying is likely to want DAO lessons for the CRUD (Read)
operations, thus if you haven't currently completed so, generate a fresh Objective-C class:
MyWineLists or whatever you would like such a long time because the declaration as well as
implementation works. Inside this file, your gut in the large event the operations will just take place.
WineList *MyWine = [[WineList alloc]init];
while (sqlite3_step(sqlStatement)==SQLITE_ROW)
http://hubpages.com/hub/Tutorial-on-Creating-an-IOS-5-SQLite-Database-Application-IOS-5-SQLite
- (IBAction)GetWineListing:(id)sender
NSMutableArray *wineArray = [[NSMutableArray alloc] init];
// MyWineList
int rawLen = sqlite3_column_bytes(sqlStatement, 3);
To commence create the NSMutableArray technique getMyWines and add an array pointer variable:
[wineArray addObject:MyWine];
#import "WineList.h"
// Copyright (c) 2011 kCodebook. Thus your own WineList must include 4 @synthesize statements,
one four each along with every property (Figure 11).
return wineArray;
NSLog(@"Problem with prepare statement");
@implementation MyWineLists
@finally
MyWine.wine = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
Next select the particular project root, right click and select Display inside Finder. (figure 6).
self.wines = [mywines getMyWines];
...
Configure SQLite
Start through locating along with opening your storyboard file. Select the particular Model group
and in the File menu or Context menu | select New File. Scroll down until anyone get the
sqlite_3.0.lib. Finally drag a new link line from the button inside the Toolbar. Anyone can
additionally add data through deciding about the table and deciding on the browse along with data
tab. Once Again select the particular NSObject as the Subclass and create the particular file (Figure
9).
...
...
Next open your implementation file. In Order To upload the image, click about the paper clip icon
next to the blob field. for your images, drag an UIImageView on the scene. (Figure 4 as well as
figure 5).
Here is the complete source code regarding the many files that were created.
in your viewDidUnload set your current properties to be able to nil to release these through memory
return wineArray;
...
MyWineLists * mywines =[[MyWineLists alloc] init];
Create UI Operations
MyWine.photo = [[UIImage alloc] initWithData:data];
@try
To implement these objects, open up the particular MyWineLists.m file. As soon As FireFox can be
installed, install the particular SQLite Manager in the Add-on Manager.
[wineArray addObject:MyWine];
NSLog(@"Cannot find database file '%@'.", dbPath);
WineList *aWine = (WineList *) [self.wines objectAtIndex: currentIndex];
[self.winerating setText:aWine.rating];
MyWine.wineId = sqlite3_column_int(sqlStatement, 0);
This just about manages the particular with the cRud operations. Note, your SQLite extension is
going to be instantly appended. Since a final step for the UI, drag a new UIToolbar along with stick
it at the underside in the screen and rename the included button: Subsequent Bottle (Figure 14).
if(!success)
[super viewDidLoad];
// Produced by simply Kevin Languedoc upon 11/25/11.
...
...
[self.winename setText:aWine.wine];
NSMutableArray *wineArray = [[NSMutableArray alloc] init];
MyWine.photo = [[UIImage alloc] initWithData:data];
...
iOS along with SQLite create a powerful combination for building data persistent iPad, iPhone as
well as iPod Touch mobile applications. This specific tutorial will walk anyone by means of the way
to setup a SQLite database application and furthermore to read text along with images in the
database in to become able to a scene.
(c) klanguedoc, 2011
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
Create a fresh Team (File | New Group) as well as in the (Context Menu | New Group). Setup the Git,
or even not, source control and finish the particular development of the project. Pertaining To this
tutorial, my named the table wineTbl as well as I are creating 4 columns: id [primary, autoinc,
integer], winename [varchar], winerating [varchar] as well as wineimage [blob].
Create CRUD Operations
Now you are able to shut your database from the Firefox menu as well as Firefox too since we won't
need anymore for that tutorial.
//
- (NSMutableArray *) getMyWines
[self.winerating setText:((WineList *) [self.wines objectAtIndex:0]).rating];
@synthesize rating;
Create the actual database
BOOL achievement = [fileMgr fileExistsAtPath:dbPath];
Test your App
NSFileManager *fileMgr = [NSFileManager defaultManager];
const char *raw = sqlite3_column_blob(sqlStatement, 3);
NSData *data = [NSData dataWithBytes:raw length:rawLen];
BOOL good results = [fileMgr fileExistsAtPath:dbPath];
NSLog(@"Problem with prepare statement");
- (void)viewDidUnload
...
...
@synthesize wineViewer;
Launch XCode 4.2 as well as create a Single-View IOS 5 application. Repeat the method for your
subsequent pair of files: MyWineList, (or anyone can choose a reputation similar to WinesDAO). The
following operation will try to open up the particular database, sqlite3_open prior to environment up
the actual Select statement along with sql3_stmt:
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
For the particular WineList class create four properties in the WineList.h (header) file, 1 with regard
to each column inside the wineTbl (Figure 10):
MyWineLists.m
...
[self.wineViewer setImage:aWine.photo];
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
@end
@catch (NSException *exception)
@synthesize winename;
To start with, an individual will need FireFox from Mozilla and the SQLite Database Manager plugin.
Setup DAO Operations
@try
else
...
...
const char *sql = "SELECT id, Wine, Rating, Photo FROM WineTbl";
You needs for you to have small filled in dot within the margin indicating in which connections have
been made.
NSFileManager *fileMgr = [NSFileManager defaultManager];
Next, click on the new table button (figure 3) to produce a brand new table, once more I will depart
up to an individual personally to title it one thing useful. Right After the particular app is actually
finished initializing anyone should have via data and image about screen. in the Popup, select
IBOutlet and type in a reputation like: winename. If the particular file can be located, success will
probably be true. Click on your Subsequent Bottle for you to get the next listing.
WineList *MyWine = [[WineList alloc]init];
@synthesize wine;
static NSInteger currentIndex = 0;
@synthesize wineId;
Expand the particular Frameworks folder, proper click upon among your frameworks along with
select Show in Finder to open Finder at the Framework location. start through selecting the first
label along with dragging any connection series (Ctrl+left mouse button) towards the header file
between your final curly brace and furthermore the @end directive. When you do not have got them,
these people is available along with installed from the FireFox web site. the iOS SDK provides native
assistance pertaining to SQLite by means of the actual utilisation of the C programming language. A
Person may give virtually any meaningful identify an individual want. Drag the particular file to your
Frameworks fostering to be able to NOT copy file in to end up being able to the frameworks,
nevertheless ONLY create a reference (Figure 7).
@synthesize photo;
Create IOS 5 Project
[self setWinerating:nil];
@catch (NSException *exception)
@finally
[super viewDidUnload];
Well CRUD can be a bit of a stretch. Regarding the actual MyWineLists header file, the sqlite3 object
can be declared as well as an NSMutableArray approach (figure 11):
[self.wineViewer setImage:((WineList *) [self.wines objectAtIndex:0]).photo];
To finish off your app, some code needs to become additional towards the ViewController header
along with implementation files. continue along with second label that may include the actual
ranking information. This will be also an IBOutlet as well as the title will be: winerating. This kind of
is likely to be an IBAction and the identify of the method: GetWineListing. for this part, four labels
(UILabel) are required: one regarding Wine name and the worth from your database and likewise for
the two others: 1 for Wine Ranking and the corresponding value in the database that will is planning
to be stored within the NSMutableArray. A Person is planning to be prompted to conserve lots of the
particular file for the file system, (naturally). An Individual will must add the libsqlite_3.0.dylib file
for you to any project (figure 6), so progress up 2 or even 3 levels (see Go for you to Enclosing folder
within the Finder menu) until you get towards the usr folder. locate your current sql database you
developed inside the very first a part of this tutorial along with copy it into the project team in places
you project header as well as implementations files are generally (Figure 8).
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
NSData *data = [NSData dataWithBytes:raw length:rawLen];
currentIndex=0;
NSLog(@"An error provides occured.");
...
...
//
[self setWineViewer:nil];
MyWine.rating = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
MyWine.rating = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
Give your own file a name: WineList (if you may be following this tutorial), select NSObject as the
Subclass that will create your file

Вам также может понравиться