首页 > 解决方案 > SQLite3 表仅包含 1 行

问题描述

所以,我有 2 个表,我已经根据文档添加了这些表,但是每当我尝试向它们添加一行时,就会发生一些奇怪的事情。数据被写入 .db 文件,但每当我查询它时,它只返回第一行。我仍然可以正常更新和读取这些行,但是我写入它们的任何数据都不会更新表。它只是被写入的第一行。

我查看了我所有的陈述,我知道他们是对的,因为第一个陈述将它添加到表中,但第二个没有。根据文档,我正在使用来自 npm 的 sqlite3 的基本配置,所以我不知道我做错了什么。

我没有任何错误,预期的结果是我可以添加尽可能多的行

db.serialize(function() {
 db.run("CREATE TABLE users (userId int, user varchar(255))");
 db.run("CREATE TABLE notes (userId int, uuid varchar(50), name varchar(255), noteData varchar(1024), file BLOB(21845))")
});

db.run("INSERT INTO users (userId, user) VALUES (0, 'User')")
db.run(`INSERT INTO notes (userId, uuid, name, noteData) VALUES (0, 'uuid', 'First Note','This will be readable.')`)

//This statement will add the data to the file, but the query won't read it.
db.run(`INSERT INTO notes (userId, uuid, name, noteData) VALUES (1, 'uuid2', 'First Note','This will not show.')`)
db.get("SELECT * FROM notes",[],(err,row)=>{console.log(row)})

此外,这不是异步问题。在示例中,我添加了最后一行,但它实际上不在我的代码中。几分钟后我请求它,我可以确认文本在数据库中,它只是决定不阅读它。

标签: javascriptsqlnode-sqlite3

解决方案


你必须使用db.all(). db.get()只返回第一行。


推荐阅读