将数据更新到SQLite数据库

时间:2014-05-05 14:18:41

标签: ios objective-c sqlite

我想将数据更新为sqlite db.I尝试并跟踪了许多用于更新数据的链接和方法。但是这些对我来说都不合适。

当我插入数据时,在数据库中插入数据后没有任何问题。

NSString *insertSQL = [NSString stringWithFormat:@"INSERT  INTO BusinessCardAppOneTable (NamorTitofImginTxtFld ,TxtofTxtView ,ImaGe,Location,Date,Time)VALUES(?,?,?,?,?,?);"]; 

注意:对于图像,我在数据库中创建了图像文件夹(路径)。

然后我获取并将数据从DB显示到tableview。

然后我编辑了NamorTitofImginTxtFld,TxtofTxtView,Date,Time(这些都在编辑视图控制器中 - 我从表视图获取图像,位置,日期,时间,NamorTitofImginTxtFld,TxtofTxtView(获取数据))

之后我编辑了NamorTitofImginTxtFld,TxtofTxtView,日期和时间。如果我想设置日期和时间我应该转到REMAINDER VIEW CONTROLLER(从编辑视图控制器的剩余按钮到剩余视图控制器)。我在哪里设置日期和时间选择器。一旦我设置并返回编辑视图控制器,我可以看到编辑过的部分。

所以我的更新查询是

NSString *updateSQL = [NSString stringWithFormat:@"UPDATE BusinessCardAppOneTable SET NamorTitofImginTxtFld = ?,  TxtofTxtView = ?,Date = ?   WHERE  Time =?  "];

如果我只编辑了NamorTitofImginTxtFld和TxtofTxtView,这些只在db.But中更新,如果我编辑了所有NamorTitofImginTxtFld,TxtofTxtView的日期和时间都没有更新。

任何人都可以解释数据库更新以及如何在db中成功更新这4列?

1 个答案:

答案 0 :(得分:0)

我提交了3个不同的选项

首先//it match up ur answer

update BusinessCardAppOneTable set NamorTitofImginTxtFld = ?, TxtofTxtView = ?, Date = ?,  Time =?    where id = 5   // here u need to identify your id, if it is correct it surely update

//it is related to your answer

是您可以使用参数化查询,如下所示: -

NSString *sqlString = @"update BusinessCardAppOneTable set NamorTitofImginTxtFld = ?, TxtofTxtView = ?, Date = ?,  Time =?    where id = ?";

sqlite3_stmt *stmt;
if(sqlite3_prepare(database, [sqlString UTF8String], -1, &stmt, NULL) != SQLITE_OK)
{
  //Handle Error
}


if(sqlite3_bind_text(stmt, 1, [record.name UTF8String],[record.type UTF8String],[record.state UTF8String], -1, SQLITE_TRANSIENT) != SQLITE_OK)
{
  // Handle Error
}
if(sqlite3_bind_int(stmt, 2, record.key) != SQLITE_OK)
{
  // Handle Error
}


if (sqlite3_step(statement) != SQLITE_DONE)
{
  // Handle Error
}

您的参数化代码已准备就绪。

在第三选择

请点击链接Sqlite3 update query not working in ios app