0

hii every one

i am using following method to insert data into data base , but it will save the first entered value only all the time the following method is in the insertUpdateDelete class

- (void) InsertRecord {


    if(addStmt == nil) {

        NSString *nsql = [NSString stringWithFormat:@"insert into tbl_Users(FirstName,MiddleName) Values('%@','%@')",strFirstName,strMiddleName];
        const char *sql = [nsql UTF8String];



        if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        {
            NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));

        }
    }



    if(SQLITE_DONE != sqlite3_step(addStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
        intID = sqlite3_last_insert_rowid(database);

    //Reset the add statement.
    sqlite3_reset(addStmt);
}

through following code i am calling this method, tfText[0] & tfText[1] are text field variable , problem is,, on every click of save after entering some data in text field, it will save only the first entered value into the data base

 - (void) save_Clicked
    {


        iICS_testAppDelegate *appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate];

        //Create a Items Object.
        insertUpdateDelete *objInsertUpdateDelete = [[insertUpdateDelete alloc] init];

        objInsertUpdateDelete.strFirstName = tfText[0].text;
        objInsertUpdateDelete.strMiddleName = tfText[1].text;

        [appDelegate InsertRecord:objInsertUpdateDelete];

    }

can any one help me,,,,thanx in advance

1 Answer 1

2
const char *addRecord = "insert into Test(taskname, desc) values(?, ?)";
sqlite3_stmt *statement;

if(sqlite3_prepare_v2(database, addRecord, -1, &statement, NULL) != SQLITE_OK)
{
    NSLog(@"Error while Inserting Record :- '%s'", sqlite3_errmsg(database));
    sqlite3_finalize(statement);
    return -1;
}

sqlite3_bind_text(statement, 1, [Ttitle UTF8String], -1, SQLITE_TRANSIENT);

sqlite3_bind_text(statement, 3, [Tdesc UTF8String], -1, SQLITE_TRANSIENT);

if(SQLITE_DONE != sqlite3_step(statement))
{
    NSLog(@"Error1 while Inserting Record :- '%s'", sqlite3_errmsg(database));
    sqlite3_finalize(statement);
    return -1;
}
else 
{
    NSLog(@"Record Inserted Successfully.");
    sqlite3_finalize(statement);
    return sqlite3_last_insert_rowid(database);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot ,it worked fine,,,my code also correct know y its not updating data?
Just Check the value for both the variable and debug your code...You will defenately get the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.