SQLITE_ERROR:靠近“输出”:语法错误

e5njpo68  于 2023-08-06  发布在  SQLite
关注(0)|答案(1)|浏览(129)

在使用OUTPUT插入数据库的过程中,我试图获取插入到数据库中的新行的ID。但是,我得到了这个错误:

SQLITE_ERROR: near "OUTPUT": syntax error

字符串
下面是涉及INSERT的代码块:

global.db.all(
    `INSERT INTO articles(creation_date, publish_date, last_modified_date, title, subtitle, article_text, likes, author_id, published)
    OUTPUT INSERTED.*
    VALUES (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?);`,
    newValues,
    function (err, rows) {
        if (err) {
            next(err); //send the error on to the error handler
        } else {
            res.json({message: "Saved", newRow: rows});
        }
    }
);


如何使用此OUTPUT获取所需的内容?我一直在尝试其他人提供的例子,但似乎不起作用。

nuypyhwy

nuypyhwy1#

SQLite不支持OUTPUT子句。
使用RETURNING

INSERT INTO articles(creation_date, publish_date, last_modified_date, title, subtitle, article_text, likes, author_id, published)
VALUES (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?, ?, ?, ?, ?)
RETURNING *;

字符串
另外,如果你想让列creation_datepublish_datelast_modified_date的默认值是当前时间戳,那么在表的CREATE TABLE语句中定义它会更简单,比如:

CREATE TABLE articles(
  .................................................
  creation_date TEXT DEFAULT CURRENT_TIMESTAMP, 
  publish_date TEXT DEFAULT CURRENT_TIMESTAMP, 
  last_modified_date TEXT DEFAULT CURRENT_TIMESTAMP,
  .................................................
);


然后INSERT语句可以简化为:

INSERT INTO articles(title, subtitle, article_text, likes, author_id, published)
VALUES (?, ?, ?, ?, ?, ?)
RETURNING *;


参见demo

相关问题