Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 25th, 2011, 09:21 AM
Registered User
 
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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%d@learn2develop.net",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....
Reply With Quote
  #2 (permalink)  
Old April 27th, 2011, 08:13 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

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.

ipadhelper@me.com

Bob
Reply With Quote
  #3 (permalink)  
Old April 30th, 2011, 02:01 PM
Registered User
 
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks, sent
Reply With Quote
  #4 (permalink)  
Old April 30th, 2011, 04:08 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old April 30th, 2011, 05:13 PM
Registered User
 
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

wow - didn't see that. Thanks a lot :)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05:11 AM
Chapter 11 zaidqais Visual Basic 2005 Basics 0 June 2nd, 2006 04:09 AM



All times are GMT -4. The time now is 09:48 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.