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 6016003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:55:15+00:00 2026-05-23T02:55:15+00:00

I am creating a database to store the value in table with a primary

  • 0

I am creating a database to store the value in table with a primary key but I am getting a problem with the primary key. It is not taking. If I enter the first time it’s shown in the database record but if I enter a second time the record is not showing in the database. My application shows bad access in the console.

+(void) getInitialDataToDisplay:(NSString *)dbPath
{
    SqltestAppDelegate *appDelegate =(SqltestAppDelegate *)[[UIApplication sharedApplication]delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        const char *sql = "select JourneyID,JourneyName,LocationName,Description from UserJourney";
        sqlite3_stmt *selectstmt;
        if (sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
        {
            while (sqlite3_step(selectstmt) == SQLITE_ROW)
            {
                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                NewJourney *newobj = [[NewJourney alloc]initWithPrimaryKey:primaryKey];
                newobj.journeyName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                newobj.isDirty = NO;
                [appDelegate.journeyList addObject:newobj];
                [appDelegate.journeyList release];
            }
        }
    }
    else {
        sqlite3_close(database);
    }

}


+(void) finalizeStatements
{
    if (database) sqlite3_close(database);
    if (database) sqlite3_finalize(deleteStmt);
    if (database) sqlite3_finalize(addStmt);
    if (database) sqlite3_finalize(detailSmt);
    if (database) sqlite3_finalize(updateStmt);
}

-(id) initWithPrimaryKey:(NSInteger)pk
{
    [super init];
    journeyID = pk;
    isDetailViewHydrated = NO;
    return self;
}

-(void) deleteCoffee
{
    if (deleteStmt == nil)
    {
        const char *sql = "delete from UserJourney where JourneyID = ?";
        if (sqlite3_prepare_v2(database, sql, -1, &deleteStmt,NULL) != SQLITE_OK)
        {
            NSAssert1(0,@"Error while creating delete statemnet.'%s'",sqlite3_errmsg(database));
        }
        //when binding parameters, index starts from 1 and not zero.
        sqlite3_bind_int(deleteStmt, 1, journeyID);

        if (SQLITE_DONE != sqlite3_step(deleteStmt))
        {
            NSAssert1(0,@"Error while deleting. '%s'",sqlite3_errmsg(database));

        }
        sqlite3_reset(deleteStmt);
    }
}

-(void) addCoffee
{

    if (addStmt == nil)
    {
        const char *sql = "insert into UserJourney(JourneyName,LocationName,Description) Values(?,?,?)";
        if (sqlite3_prepare_v2(database, sql, -1,&addStmt , NULL) != SQLITE_OK)
        {
            NSAssert1(0,@"Error while creating add statement.'%s'",sqlite3_errmsg(database));
        }
        sqlite3_bind_text(addStmt, 1 , [journeyName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 2 , [locationName UTF8String],-1, SQLITE_TRANSIENT);
        sqlite3_bind_text(addStmt, 3, [description UTF8String],-1, SQLITE_TRANSIENT);
        if (SQLITE_DONE != sqlite3_step(addStmt))
        {
            NSAssert1(0,@"Error while inserting data. '%s'",sqlite3_errmsg(database));
        }else
        {
            journeyID = sqlite3_last_insert_rowid(database);
        }
        sqlite3_reset(addStmt);
    }
}

/*-(void) hydrateDetailViewData
{

    if (isDetailViewHydrated) return;

    if (detailSmt == nil) {
        const char *sql = "select JourneyName,Description from UserJourney where JourneyID = ?";
        if (sqlite3_prepare_v2(database, sql, -1, &detailSmt,NULL) != SQLITE_OK)
            NSAssert1(0,@"Error while creating detail view statement. '%s'",sqlite3_errmsg(database));

    }
    sqlite3_bind_int(detailSmt,1,journeyID);
    if (SQLITE_DONE != sqlite3_step(detailSmt))
    {
        <#statements#>
    }

}*/

-(void)saveAllData
{
    if (isDirty) {
        if (updateStmt == nil) {
            const char *sql = "update UserJourney Set JourneyName = ?,LocationName = ?, Description = ? Where JourneyID =?";
            if (sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
                NSAssert1(0,@"Error while creating update statement. '%s'",sqlite3_errmsg(database));

        }
        sqlite3_bind_text(updateStmt, 1, [journeyName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 2, [locationName UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(updateStmt, 3, [description UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_int(updateStmt,4, journeyID);

        if(SQLITE_DONE != sqlite3_step(updateStmt))
            NSAssert1(0, @"Error while updating. '%s'", sqlite3_errmsg(database));

        sqlite3_reset(updateStmt);

        isDirty = NO;

    }
    [journeyName release];
    journeyName = nil;
    isDetailViewHydrated = NO;

}
  • 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-23T02:55:16+00:00Added an answer on May 23, 2026 at 2:55 am

    You have to release your newobj not the array.
    Replace this:

    [appDelegate.journeyList release];
    

    with this:

    [newobj release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I am creating a new database table, what factors should I take into
I am creating an application which will store a (semi) real-time feed of a
I am creating a small database application. I want to store the count of
I'm currently facing a performance problem with creating POCO objects from my database. I'm
When creating a database structure, what are good guidelines to follow or good ways
We currently deploy web applications by creating a database and running SQL scripts through
I'm tired of opening Dia and creating a database diagram at the beginning of
I would like to learn the best practices to employ when creating a database
I am creating a mock database for import export tests (of the algorithm reading
Using DTS I'm dynamically creating an access database. After the file is created (which

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.