我已经做了很多关于更新创建的sqlite表的谷歌搜索仍然我无法在我的示例应用程序中更新我的表。可以任何人请告诉我我的下面代码有什么问题.IT工作正常直到 sqlite3_prepare_v2 。一旦达到 if(sqlite3_prepare_v2(数据库,sql,-1,&语句,NULL)== SQLITE_OK)条件,它不会进入这个if()条件可以一个人告诉我们这里发生了什么?
const char *dbpath = [[self DBPath] UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySql=[NSString stringWithFormat:@"UPDATE Table1 SET AppEndTime = %@ WHERE AppID= %d",AppEndTime,appID];
const char *sql=[querySql UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL)==SQLITE_OK){
sqlite3_bind_int(statement, 1, sessionID);
sqlite3_bind_text(statement, 6, [sessionEndTime UTF8String], -1, SQLITE_TRANSIENT);
}
}
char* errmsg;
sqlite3_exec(database, "COMMIT", NULL, NULL, &errmsg);
if(SQLITE_DONE != sqlite3_step(statement)){
NSLog(@"Error while updating. %s", sqlite3_errmsg(database));
}
else{
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
sqlite3_close(database);
答案 0 :(得分:1)
请替换下面给出的代码。我希望它能解决您的问题。
if (sqlite3_prepare_v2(database, sql, -1, &Statement1, NULL) == SQLITE_OK) {
sqlite3_bind_text(Statement1, 1, [status UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(Statement1, 2, [messageID UTF8String], -1, NULL);
int success = sqlite3_step(Statement1);
if(success != SQLITE_ERROR)
{
// NSLog(@"Success");
}
sqlite3_finalize(Statement1);
}
答案 1 :(得分:0)
这是我更新创建表的代码。看看希望它会帮助你。
-(BOOL)updateMyTable{
BOOL isGood = YES;
@try {
NSString *fName = [user_dic valueForKey:@"fName"];
NSString *lName = [user_dic valueForKey:@"lName"];
NSString *email_id = [user_dic valueForKey:@"email_id"];
NSString *employee_id = [user_dic valueForKey:@"employee_id"];
fName= [fName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
lName= [lName stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
email_id= [email_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
employee_id= [employee_id stringByReplacingOccurrencesOfString:@"'" withString:@"''"];
// NSString *autoId = [user_dic valueForKey:@"autoId"];
NSString *sql = [NSString stringWithFormat:@"UPDATE tbl_profile SET fName = '%@', lName = '%@', email_id = '%@' ,employee_id = '%@' WHERE autoId = '%@'",fName,lName,email_id,employee_id,autoId];
[database executeUpdate:sql,nil];
if (MyDelegate.isLogging) {
NSLog(@"edit user QUERY---- >>>>%@",sql);
NSLog(@"edit user RESULT CODE ---- >>>>%d:", [database lastErrorCode]);
NSLog(@"edit user RESULT ERROR MESSAGE ---- >>>>%@",[database lastErrorMessage]);
}
}@catch (NSException *exception) {
isGood = NO;
}
@finally {
}
return isGood;
答案 2 :(得分:0)
请确保您的Sqlite文件位于文档目录中,而不是在项目文件夹中。 当且仅当文件位于文档目录
时,更新和插入才有效