开发者

how store primerykey in sqlitedatabase in iphone

开发者 https://www.devze.com 2023-03-11 03:17 出处:网络
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 b

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 U开发者_如何学PythonTF8String], -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;

}


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

[appDelegate.journeyList release];

with this:

[newobj release];
0

精彩评论

暂无评论...
验证码 换一张
取 消