SQLite 更新正在用空值替换字段
好吧,我最近一直在阅读和使用 SQLite(这里有人实际上建议了它)。
它非常适合我需要的东西——内存数据库。然而,我遇到的问题是它们在这里是重复的记录,所以我想做的是插入记录,但如果它已经存在,那么只需填写缺少的字段即可。
现在我尝试的第一个方法是将 id 设置为主键并插入或替换。问题是以前的内容被删除了。
所以我现在做的是更新查询。我正在检查更改的行数。如果它低于 1,那么我运行插入查询(如果有更好的方法请分享,因为我知道这会产生额外的开销)。
无论如何,我的问题(最后)是,即使使用更新查询,记录也会被空值覆盖。
我浓缩了下面的代码片段:
$stmt1 = $db->prepare("UPDATE results SET
field1=?, field2=?, field3=?
WHERE id=?
");
$stmt1->execute(array(
$elm['content1'], $elm['content2'], $elm['content3'], $elm['id']
));
$count = $stmt1->rowCount();
if ($count < 1) {
$stmt2 = $db->prepare("INSERT INTO results (id, field1, field2, field3)
VALUES (:id,:field1, :field1, :field1 )
");
$stmt2->execute(array(":id" => $recordID, ":field1" => $elm['content1'], ":field2" => $elm['content2'], ":field3" => $elm['content3']));
}
上面的代码是在 foreach 循环中进行的。
如果内容已经在数据库中,他们是否会阻止内容被覆盖。因此,如果更新时该字段为空,则添加新内容,如果其中已经包含某些内容,则保持不变并继续到下一个字段。我读过有关 IFNULL 的信息,它可以在 SQLite 中使用,但我不确定如何在 PDO 准备好的语句中使用它。
任何帮助、指导、示例将不胜感激:)
ps 我将 PHP5 与 SQLite 一起使用
Okay so ive been reading up on and working with SQLite recently (someone on here suggested it actually).
It works great for what I need - an in-memory database. The problem I am having however is that their are duplicate records in here so what I wanted to do was insert the record but if it already exists then just fill in the missing fields.
Now the first method I tried was setting the id as a primary key and INSERT OR REPLACE into. The issue with this was the previous contents were being wiped out.
So what im doing now is an update query. Im them checking the number of rows changed. if its below 1 then I run the insert query (if there a better way do share as I know this has extra overhead).
Anyway my issue (finally).. is that even with the update query the records are being overwritten with null values.
Ive condensed the snippets of code below:
$stmt1 = $db->prepare("UPDATE results SET
field1=?, field2=?, field3=?
WHERE id=?
");
$stmt1->execute(array(
$elm['content1'], $elm['content2'], $elm['content3'], $elm['id']
));
$count = $stmt1->rowCount();
if ($count < 1) {
$stmt2 = $db->prepare("INSERT INTO results (id, field1, field2, field3)
VALUES (:id,:field1, :field1, :field1 )
");
$stmt2->execute(array(":id" => $recordID, ":field1" => $elm['content1'], ":field2" => $elm['content2'], ":field3" => $elm['content3']));
}
The above is going on within a foreach loop.
Is their anyway to stop the content being overwritten if its already in the db. So if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field. Ive read about an IFNULL which can be used in SQLite but im not sure how I use that within my PDO prepared statements.
Any help, guidance, examples would be appreciated :)
p.s Im using PHP5 with SQLite
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果更新时该字段为空,则添加新内容,如果其中已包含某些内容,则保持不变并移至下一个字段。
您可以使用
coalesce
这样做;这是一个示例:因此,您的
stmt1
将是:if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field.
You can use
coalesce
to do this; here is an example:So, your
stmt1
would be: