 |
BOOK: Beginning iOS 4 Application Development
 | This is the forum to discuss the Wrox book Beginning iOS 4 Application Development by Wei-Meng Lee; ISBN: 978-0-470-91802-9 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning iOS 4 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 25th, 2011, 09:21 AM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
chapter 11
this one doesn't work for me too. I cannot find the database.sql file at all
it crashes while inserting data to the database on this line:
Code:
NSAssert(0, @"Table failed to create");
these are the error messages:
Quote:
2011-04-25 14:15:36.359 Databases[318:207] *** Assertion failure in -[DatabasesViewController createTableNamed:withField1:withField2:], /Users/c0d0man/Documents/Databases/Databases/DatabasesViewController.m:34
2011-04-25 14:15:36.368 Databases[318:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Table failed to create'
|
contents of the file:
Code:
//
// DatabasesViewController.m
// Databases
//
// Created by Martin Chrobot on 25/04/2011.
// Copyright 2011 home. All rights reserved.
//
#import "DatabasesViewController.h"
@implementation DatabasesViewController
-(NSString *)filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"database.sql"];
}
-(void)openDB {
//create a database
if(sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Database failed to open");
}
}
-(void)createTableNamed:(NSString *) tableName withField1:(NSString *) field1 withField2:(NSString *) field2 {
char *err;
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXIST '%@' ('%@' TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Table failed to create");
}
}
-(void) insertRecordsIntoTableNamed:(NSString *) tableName
withField1:(NSString *) field1
field1Value:(NSString *) field1Value
andField2:(NSString *) field2
field2Value:(NSString *) field2Value {
NSString *sql = [NSString stringWithFormat:@"INSERT OR REPLACE INTO '%@' ('%@', '%@') VALUES ('%@', '%@')", tableName, field1, field2, field1Value, field2Value];
char *err;
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Error updating table");
}
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[self openDB];
[self createTableNamed:@"Contacts" withField1:@"email" withField2:@"name"];
for(int i=0; i<=2; i++){
NSString *email = [[NSString alloc] initWithFormat:@"user%[email protected]",i];
NSString *name = [[NSString alloc] initWithFormat:@"user %d", i];
[self insertRecordsIntoTableNamed:@"Contacts" withField1:@"email" field1Value:email andField2:@"name" field2Value:name];
[email release];
[name release];
}
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
I'd really appreciate if someone would help me out here as this is the 3rd example that doesn't work for me and it's really putting me off....
|
|

April 27th, 2011, 08:13 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
If you send me the project files I will take a look and try to sort out your issues. I have been through a previous incarnation of this book, but don't have this one (didn't like the other one too muchâ¦) but should be able to help. It will be quicker to track down the problem with the access to the full project.
[email protected]
Bob
|
|

April 30th, 2011, 02:01 PM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks, sent
|
|

April 30th, 2011, 04:08 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
|
|
One little typoâ¦
Code:
-(void)createTableNamed:(NSString *) tableName withField1:(NSString *) field1 withField2:(NSString *) field2 {
char *err;
// NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXIST '%@' ('%@' TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' TEXT PRIMARY KEY, '%@' TEXT);", tableName, field1, field2];
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL, &err) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Table failed to create");
}
}
The commented out line is what you had, the bold is what you need.
You might find something of value in this other thread if you implement deleting rows.
Chapter 10: Deleting rows from a table
Bob
|
|

April 30th, 2011, 05:13 PM
|
|
Registered User
|
|
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
wow - didn't see that. Thanks a lot :)
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Chapter 11 |
icculus1 |
BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 |
1 |
July 30th, 2009 05:15 PM |
| Chapter 11 |
Moorish |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
11 |
June 4th, 2008 04:57 PM |
| chapter 11 figure 11-7 relative positioning |
pelopito |
BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 |
2 |
November 29th, 2007 06:11 AM |
| Chapter 11 |
zaidqais |
Visual Basic 2005 Basics |
0 |
June 2nd, 2006 04:09 AM |
|
 |
|