unable to insert data into sqlite database in obj c
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 sharedApplica开发者_StackOverflow社区tion] 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
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);
}
精彩评论