barcode maker vb.net 5: Stick Around: Building Data-Driven Applications with SQLite in Objective-C

Generator QR Code in Objective-C 5: Stick Around: Building Data-Driven Applications with SQLite

CHAPTER 5: Stick Around: Building Data-Driven Applications with SQLite
Encoding QR Code JIS X 0510 In Objective-C
Using Barcode generation for iPhone Control to generate, create Quick Response Code image in iPhone applications.
www.OnBarcode.com
Barcode Generation In Objective-C
Using Barcode encoder for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
Make sure to @synthesize this property in the RootViewController.m file and release it in the dealloc method. Find and replace the implementations of the following functions in the .m file as well:
QR Code Printer In Objective-C
Using Barcode printer for iPhone Control to generate, create Denso QR Bar Code image in iPhone applications.
www.OnBarcode.com
DataMatrix Creator In Objective-C
Using Barcode maker for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
- (void)viewDidLoad { [super viewDidLoad]; self.results = [NSArray arrayWithObjects: @"Apple", @"Banana", nil]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: _ (NSInteger)section { return [results count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: _ (NSIndexPath *)indexPath { static NSString *CellIdentifier = @"GroceryCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30000 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; #else cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero _ reuseIdentifier:CellIdentifier] autorelease]; #endif } #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30000 cell.textLabel.text = [results objectAtIndex:indexPath.row]; #else cell.text = [results objectAtIndex:indexPath.row]; #endif return cell; }
Creating EAN128 In Objective-C
Using Barcode printer for iPhone Control to generate, create EAN128 image in iPhone applications.
www.OnBarcode.com
Generating Barcode In Objective-C
Using Barcode creator for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
The iPhone version check here lets you use the recommended methods for the iPhone 3.0 SDK while remaining compatible with older versions. Building and running should show you a simple list of grocery items, as shown in Figure 5-4.
EAN13 Generator In Objective-C
Using Barcode generation for iPhone Control to generate, create UPC - 13 image in iPhone applications.
www.OnBarcode.com
EAN / UCC - 8 Drawer In Objective-C
Using Barcode creation for iPhone Control to generate, create EAN-8 image in iPhone applications.
www.OnBarcode.com
CHAPTER 5: Stick Around: Building Data-Driven Applications with SQLite
Paint QR-Code In None
Using Barcode creation for Online Control to generate, create QR Code JIS X 0510 image in Online applications.
www.OnBarcode.com
Generating Quick Response Code In None
Using Barcode encoder for Software Control to generate, create QR Code ISO/IEC18004 image in Software applications.
www.OnBarcode.com
Figure 5-4. A simple grocery list
Data Matrix 2d Barcode Reader In Visual C#
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Scan EAN13 In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Creating and Initializing the Database
Printing PDF 417 In Java
Using Barcode encoder for Java Control to generate, create PDF 417 image in Java applications.
www.OnBarcode.com
QR Code Scanner In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Next you ll create the file that contains the code that wraps the database methods. Create a new NSObject subclass. I used my company name prefix and kept it simple, naming the class ISDatabase. The following functions will use three properties that must be declared in the header. Your header should start out looking like this:
Draw Code 128A In Java
Using Barcode maker for Java Control to generate, create Code 128 Code Set B image in Java applications.
www.OnBarcode.com
GS1 DataBar Limited Creator In .NET
Using Barcode maker for .NET framework Control to generate, create GS1 DataBar Expanded image in .NET framework applications.
www.OnBarcode.com
#import <sqlite3.h> @interface ISDatabase : NSObject { NSString *pathToDatabase; BOOL logging; sqlite3 *database; } @property (nonatomic, retain) NSString *pathToDatabase; @property (nonatomic) BOOL logging; - (id) initWithPath: (NSString *) filePath; - (id) initWithFileName: (NSString *) fileName; @end
QR-Code Scanner In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Making QR Code 2d Barcode In None
Using Barcode maker for Excel Control to generate, create Denso QR Bar Code image in Excel applications.
www.OnBarcode.com
Remember to @synthesize these in the .m file. Next, you ll create some simple init functions to aid in creating a database in the resources directories for the application:
Code 39 Recognizer In VB.NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Paint Barcode In .NET
Using Barcode drawer for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
- (id) initWithPath: (NSString *) filePath {
CHAPTER 5: Stick Around: Building Data-Driven Applications with SQLite
if(self = [super init]) { self.pathToDatabase = filePath; [self open]; } return self; } - (id) initWithFileName: (NSString *) fileName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, _ NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [self initWithPath: [documentsDirectory _ stringByAppendingPathComponent:fileName]]; }
The first init function creates and opens a connection to a database at the given path; the second is a convenience function that creates or opens a database in the application documents directory with the given file name.
Opening a Database Connection
All data in a SQLite database is stored in a single cross-platform file on disk. To work with a SQLite database, you first have to open a connection and specify the database file. When done working with the database, you should close that connection. You ll add two functions to do this:
- (void) close { if(sqlite3_close(database) != SQLITE_OK) { [self raiseSqliteException:@"failed to close database with message '%S'."]; } } - (void) open { //opens database, creating the file if it does not already exist if(sqlite3_open([self.pathToDatabase UTF8String], &database) != SQLITE_OK) { sqlite3_close(database); [self raiseSqliteException:@"Failed to open database with message '%S'."]; } }
These are pretty straightforward. open opens the connection and stores the database handle in the database property. They report errors by calling the raiseSqliteException function:
- (void) raiseSqliteException: (NSString *) errorMessage
CHAPTER 5: Stick Around: Building Data-Driven Applications with SQLite
{ [NSException raise:@"ISDatabaseSQLiteException" format:errorMessage, _ sqlite3_errmsg16(database)]; }
This calls sqlite3_errmsg16, which takes the database handle and returns the error message in plain English. This is then wrapped in an NSException and raised. You then clean up the database connection in the dealloc (along with pathToDatabase):
- (void) dealloc { [self close]; [pathToDatabase release]; [super dealloc]; }
If you compile now, you will see two warnings appear. The first warning is in the init method, as shown in Figure 5-5.
Figure 5-5. You ve been warned.
The compiler needs to be informed about methods that are declared later in the class that are used earlier, such as the open method here. The other warning is similar, informing you that the compiler cannot find the raiseSqliteException: method. Instead of rearranging the methods to fit the demands of the machine rather than readability, add a private category to contain these method declarations. Add the following to the top of the ISDatabase.m file:
@interface ISDatabase(PrivateMethods) - (void) open; - (void) raiseSqliteException: (NSString *) errorMessage; @end
Recompile, and the warnings are no more. Now that you ve completed these methods, you can see them in action. Open GroceryListAppDelegate.h, and add ISDatabase as a forward class and a property called database to hold the database instance. GroceryListAppDelegate.h should look like this:
Copyright © OnBarcode.com . All rights reserved.