Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 440165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:50:43+00:00 2026-05-12T20:50:43+00:00

I was successfully accessing my database to get a list of cities on the

  • 0

I was successfully accessing my database to get a list of cities on the App launch. I tried running a second query against it right afterward to get the list of States but all that happens is that my app blows up with no usable error in the console (simply says “Program received signal: EXEC_BAD_ACCESS” and nothing more).

Here is the code, I was hoping someone could potentially explain to me what I’m doing wrong:

-(void) initializeDatabase{
// The database is stored in the application bundle
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"mydatabase.sqlite"];

// Open the database.  The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK){
    [self initializeCities:database];
    [self initializeStates:database];
} else {
    // Even though the open failed, call close to properly clean up resources.
    sqlite3_close(database);
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
    // Additional error handling, as appropriate...
}
    }

-(void) initializeCities:(sqlite3 *)db {
NSMutableArray *cityArray = [[NSMutableArray alloc] init];
self.cities = cityArray;
[cityArray release];

// Get the primary key for all cities.
const char *sql = "SELECT id FROM my_table ORDER BY state";
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK){
    while (sqlite3_step(statement) == SQLITE_ROW){
        int primaryKey = sqlite3_column_int(statement, 0);
        City *city = [[City alloc] initWithPrimaryKey:primaryKey database:db];
        [cities addObject:city];
        [city release];
    }
}

// "Finalize" the statement - releases the resources associated with the statement.
sqlite3_finalize(statement);
}

-(void) initializeStates:(sqlite3 *)db {
NSMutableArray *statesArray = [[NSMutableArray alloc] init];
self.states = statesArray;
[statesArray release];

// Get the primary key for all cities.
const char *sql = "SELECT DISTINCT state FROM my_table ORDER BY state";
sqlite3_stmt *statement;

if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL) == SQLITE_OK){
    // We "step" through the results - once for each row
    while (sqlite3_step(statement) == SQLITE_ROW){
        NSString *state;
        state = (NSString *)sqlite3_column_text(statement, 0);

        [states addObject:state];
        [state release];
    }
}

// "Finalize" the statement - releases the resources associated with the statement.
sqlite3_finalize(statement);
}

I can’t debug this code as the debugger never hits my breakpoints at all.
If I remove the initializeStates method the app works as expected (albiet without a list of states).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:50:44+00:00Added an answer on May 12, 2026 at 8:50 pm

    You are releasing “state” without having allocated it. Try something like this:

    while (sqlite3_step(statement) == SQLITE_ROW){
        NSString *state = [[NSString alloc] initWithCString:(char*)sqlite3_column_text(statement, 0) encoding:NSASCIIStringEncoding];
        //state = (NSString *)sqlite3_column_text(statement, 0);
    
        [states addObject:state];
        [state release];
    }
    

    Update: add cast above to fix compiler warning

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble accessing a dataset. I've pulled data from an Access database (successfully
I have successfully created a db in Matlab using the Visual Query builder (database
I want to block non-browser clients from accessing certain pages / successfully making a
I successfully connected to my database when it was SQL Server 2008 R2. However
After successfully opening Pop3Folder , and retrieving messages from it, I then sometimes get
After successfully acquiring an oauth_token for a user, I am able to get successful
I successfully compiled ActionBarSherlock in Eclipse. But I get 75 errors when I use
I'm accessing an internal database using MATLAB's urlread command, everything was working fine until
I'm using JDBC to get a large amount of data. The call completes successfully,
I have a question about login and accessing database for mobile web applications. Namely,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.