【问题标题】:SQLite3 Table Only Holds 1 RowSQLite3 表仅包含 1 行
【发布时间】:2021-05-11 04:30:18
【问题描述】:

所以,我有 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)})

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

【问题讨论】:

    标签: javascript sql node-sqlite3


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 2017-07-18
      相关资源
      最近更新 更多