Saturday, August 3, 2019

iphone - Problem with inserting data into a table



sqlite3 *database;
sqlite3_stmt *statement;
NSString *dPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"UserData.sqlite"];

const char *dbpath = [dPath UTF8String];
// NSLog(@"%@",dbpath);
//UserArray = [[NSMutableArray alloc]init];



if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{

NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (UserName,FullName,Email,PhoneNo) VALUES (\"%@\", \"%@\", \"%@\" ,\"%@\")",txtUserName.text,txtFullName.text ,txtEmail.text , txtPhoneNo.text];

const char *insert_stmt = [insertSQL UTF8String];


sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);

if(sqlite3_step(statement)==SQLITE_DONE)
{
txtUserName.text=@"";
txtFullName.text=@"";
txtEmail.text=@"";
txtPhoneNo.text=@"";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];
[alert release];
alert=nil;

}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];

alert=nil;
}


// Release the compiled statement from memory
sqlite3_finalize(statement);

sqlite3_close(database);
}



->> i am trying to insert data into table. the code execute properly but the record is not added into the table. so please help me or this problem..thank's for your response.


Answer



try executing commit, so setting the db to autocommit



// COMMIT
const char *sql2 = "COMMIT TRANSACTION";
sqlite3_stmt *commit_statement;
if (sqlite3_prepare_v2(database, sql2, -1, &commit_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));

}
success = sqlite3_step(commit_statement);
if (success != SQLITE_DONE) {
NSAssert1(0, @"Error Message: '%s'.", sqlite3_errmsg(database));
}
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...