Stuck scratching my head


#1

Okay I entered the “Fetching Data” portion of chapter 26 (up to page 358) and my program is not working. In the debugger screen I am receiving the following as I type “United” for United States.

2011-02-03 13:33:16.453 Nayshunz[26621:207] searching for U%
2011-02-03 13:33:16.970 Nayshunz[26621:207] query error: (null)
2011-02-03 13:33:16.970 Nayshunz[26621:207] searching for Un%
2011-02-03 13:33:18.950 Nayshunz[26621:207] query error: (null)
2011-02-03 13:33:18.951 Nayshunz[26621:207] searching for Uni%
2011-02-03 13:33:20.401 Nayshunz[26621:207] query error: (null)
2011-02-03 13:33:20.402 Nayshunz[26621:207] searching for Unit%
2011-02-03 13:33:20.638 Nayshunz[26621:207] query error: (null)
2011-02-03 13:33:20.639 Nayshunz[26621:207] searching for Unite%
2011-02-03 13:33:20.761 Nayshunz[26621:207] query error: (null)
2011-02-03 13:33:20.762 Nayshunz[26621:207] searching for United%

I went through my code several times to try and find what is wrong and have had no luck. I went to the solutions file to see what it is suppose to look like and I believe it to be even more broke since I do not get the feedback as each character is typed, as you can see below.

2011-02-03 13:36:05.407 Nayshunz[26920:207] query error: (null)
2011-02-03 13:36:05.678 Nayshunz[26920:207] query error: (null)
2011-02-03 13:36:05.867 Nayshunz[26920:207] query error: (null)
2011-02-03 13:36:06.037 Nayshunz[26920:207] query error: (null)
2011-02-03 13:36:06.283 Nayshunz[26920:207] query error: (null)
2011-02-03 13:36:06.430 Nayshunz[26920:207] query error: (null)

This is my method, does anyone have an idea why this isn’t working, and does the solution work for you?

  • (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
    // Only serch if the user has typed something in
    if ([searchText length] !=0) {

      // Does the statement need to be prepared?
      if (!statement) {
      	
      	// '?' is a placeholder for parameters
      	char *cQuery = "SELECT Continent, [Name], Code FROM Country "
      					"WHERE [Name] LIKE ? ORDER BY Continent, [Name]";
      	
      	// Prepare the query
      	if (sqlite3_prepare_v2(database, cQuery, -1, &statement, NULL)
      		!= SQLITE_OK)
      	{
      		NSLog(@"query error: %s", statement);
      	}
      }
      
      // Add % to the end of the search text
      searchText = [searchText stringByAppendingString:@"%"];
      
      NSLog(@"searching for %@", searchText);
      
      // This C string will get cleaned up automatically
      const char *cSearchText =
      [searchText cStringUsingEncoding:NSUTF8StringEncoding];
      
      // Replace the first (and only) parameter with the search text
      sqlite3_bind_text(statement, 1, cSearchText, -1, SQLITE_TRANSIENT);
      
      // Loop to get all the rows
      while (sqlite3_step(statement) == SQLITE_ROW) {
      	
      	// Get the string in the first column
      	const char *cContinentName =
      					(const char *)sqlite3_column_text(statement, 0);
      	
      	// Convert C string into an NSString
      	NSString *continentName = [[[NSString alloc]
      								initWithUTF8String:cContinentName] autorelease];
      	
      	// Get the string in the second column
      	const char *cCountryName =
      					(const char *)sqlite3_column_text(statement, 1);
      	
      	// Convert C string into an NSString
      	NSString *countryName = [[[NSString alloc]
      							  initWithUTF8String:cCountryName] autorelease];
      	
      	// Get the string in the third column
      	const char *cCountryCode =
      					(const char *)sqlite3_column_text(statement, 2);
      	
      	// Convert C string into an NSString
      	NSString *countryCode = [[[NSString alloc]
      							  initWithUTF8String:cCountryCode] autorelease];
      	
      	NSLog(@"%@: %@ of %@", countryCode, countryName, continentName);
      	
      }
      // Clear the query results
      sqlite3_reset(statement);
    

    }

    // Load the table with the new data
    [countryTable reloadData];
    }


#2

Hi,

You seem to be missing the open database command

    if (! statement) { 

// Open the database file
if (sqlite3_open([fullPath cStringUsingEncoding:NSUTF8StringEncoding], &database) != SQLITE_OK) {
NSLog(@“unable to open database at %@”, fullPath);
}

char *cQuery = "SELECT Continent, Name, Code FROM Country "
“WHERE Name LIKE ? ORDER BY Continent, Name”;

Gareth


#3

I actually have;

// Open the database file
const char *cFullPath = [fullPath cStringUsingEncoding:NSUTF8StringEncoding];
if (sqlite3_open(cFullPath, &database)
	!= SQLITE_OK) {
	NSLog(@"unable to open database at %@", fullPath);
}

return self;

in my init method as it appears in the book, page 357. That should be the correct place for it right? I wouldn’t want to open it every time there was a search done?

–Dave


#4

Hi,

Hmmm - if I copy your code and add the open statement then I get some results back.

I’ll keep looking…

Gareth


#5

Hi,

I took the solutions file - replaced the textDidChange method with your version and it all worked fine.

If I remove the sqlite3_open statement in the init method then I can reproduce your problem. So the only thing I can think of is either that statement isn’t getting executed or there is something wrong with the database.

Can you get data back from the database using the terminal commands listed in the “Creating the Database” section ?
Is the sqlite3_open statement getting executed with no errors ?

Gareth


#6

Thanks Gareth for taking the time. I re-downloaded the solution file just to make sure I didn’t corrupt something. I double checked the SQL exercise at the beginning of the chapter and that worked. The freshly downloaded solution would not work for me, so I then tried bringing over my method as you said you did and I received the same results as I did yesterday. As a Hail Mary, I even ran a permissions repair!

What I don’t understand is why would the solution file not work? I’ve pretty much completed every exercise in the book to this point and haven’t had the problems I am having now. :blush:

–Dave


#7

Hi,

I’m fast running out of ideas now…

When you run the solution version have you tried deleting the app from the simulator first ? Just thinking that if somehow the countries.db had got corrupted in the Documents folder it wouldn’t copy the solution version across from the bundle.

A bit of a long shot I know.

Gareth


#8

Gareth your last idea should have been your first. :smiley:

Seriously thanks for the advice, I trashed the entire folder and everything is now working. Both my version which is obviously not completed, and the solution file. Something (I guess the db file) must have been hosed in that folder. Being new to this all I never thought to do something like that. Several lessons learned today…

Thanks again for your time. It made all the difference to me.

Sincerely,
–Dave


#9

Phew - I’ve got no idea what I would have said next :smiley: