SQLite 更新正在用空值替换字段

发布于 2024-10-16 12:14:32 字数 1188 浏览 5 评论 0原文

好吧,我最近一直在阅读和使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恬淡成诗 2024-10-23 12:14:32

如果更新时该字段为空,则添加新内容,如果其中已包含某些内容,则保持不变并移至下一个字段。

您可以使用coalesce这样做;这是一个示例:

~ e$ sqlite3
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo (a,b,c);
sqlite> insert into foo values (1,NULL,3);
sqlite> select * from foo;
1||3
sqlite> update foo set a = a + 1, b = coalesce(b,'b'), c = coalesce(c,'c');
sqlite> select * from foo;
2|b|3
sqlite> 

因此,您的 stmt1 将是:

$stmt1 = $db->prepare("UPDATE results SET 
    field1=coalesce(field1,?), field2=coalesce(field2,?), field3=coalesce(field3,?)
    WHERE id=?
");

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:

~ e$ sqlite3
SQLite version 3.7.5
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo (a,b,c);
sqlite> insert into foo values (1,NULL,3);
sqlite> select * from foo;
1||3
sqlite> update foo set a = a + 1, b = coalesce(b,'b'), c = coalesce(c,'c');
sqlite> select * from foo;
2|b|3
sqlite> 

So, your stmt1 would be:

$stmt1 = $db->prepare("UPDATE results SET 
    field1=coalesce(field1,?), field2=coalesce(field2,?), field3=coalesce(field3,?)
    WHERE id=?
");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文