0

The following code:

$stm = $sql->prepare('INSERT OR REPLACE INTO "vehicle_service_invoice" (
        invoice, "date", unit, odometer, sublet, sub, po, categories
    ) VALUES (
        :invoice, :date, :unit, :odometer, :sublet, :sub, :po, :categories
    ) WHERE rowid = :rowid;'
);
$stm->bindParam(':invoice', $_POST['invoice']);
$stm->bindParam(':date', $_POST['date']);
$stm->bindParam(':unit', $_POST['unit']);
$stm->bindParam(':odometer', $_POST['odometer']);
$stm->bindParam(':sublet', $_POST['sublet']);
$stm->bindParam(':sub', $_POST['sub']);
$stm->bindParam(':po', $_POST['po']);
$stm->bindParam(':categories', $categories);
$stm->bindParam(':rowid', $_POST['rowid']);
$stm->execute();

Produces the following query:

INSERT OR REPLACE INTO "vehicle_service_invoice" (
    invoice,
    "date",
    unit,
    odometer,
    sublet,
    sub,
    po,
    categories
) VALUES (
    7230,
    '2013-02-07',
    558,
    34863,
    0,
    0,
    1486347,
    5
) WHERE rowid = 1

That produces the following error:

ERROR: near "WHERE": syntax error.


What I am trying to do is make a single path for both my INSERT and UPDATE logic to follow, so after I found out that I could do INSERT OR REPLACE, I figured I could just update the information based on the ROWID of each item. To me the syntax looks correct, what am I doing wrong?

It should be noted that I don't care about changing ROWID values as I understand that is a tripping point on doing INSERT OR REPLACE statements. Everything is joined together in other queries based off of the INVOICE column. I only want to use the ROWID to refer to that row.

4

1 回答 1

1

在 INSERT 语句中,WHERE 子句没有意义。

INSERT OR REPLACE 的工作原理如下:将具有指定值的记录插入到表中。如果这会导致违反 UNIQUE 约束,则删除旧记录。

要替换可能已经存在的记录,标识该记录的列必须是要插入的值的一部分。换句话说,您必须插入rowid值:

INSERT OR REPLACE INTO vehicle_service_invoice(rowid, ...) VALUES (1, ...)
于 2013-10-27T09:13:01.733 回答