SQLITE 3未写入表

SQLITE 3未写入表,sqlite,ecmascript-6,Sqlite,Ecmascript 6,我正在尝试存储这样的对象数组 "id": "EExKKTC8IuL", "date": "2019-02-18T14:57:52.997Z", "timestamp": "1550500384", "title": "Financial Giant SBI Aims to Boost Ripple and Push XRP Ahead of Bitcoin in Crypto Market CapFinancial Giant SBI Aims to Boost R

我正在尝试存储这样的对象数组

   "id": "EExKKTC8IuL",
    "date": "2019-02-18T14:57:52.997Z",
    "timestamp": "1550500384",
    "title": "Financial Giant SBI Aims to Boost Ripple and Push XRP Ahead of Bitcoin in Crypto Market CapFinancial Giant SBI Aims to Boost Ripple and Push XRP Ahead of Bitcoin in Crypto Market Cap",
    "score": 6,
    "comparative": 0.18181818181818182
进入一个更好的sqlitev3表。我通常可以用简单的id键自动递增来完成。但是我正在努力保持数据库中文章的ID,这样它们就不会重复。这是代码

    try {

    const table = "coinna"
    const dbFile = "scraped"
    const dataDir = "./data/"
    const dbExt = ".db"

    const dbConn = db(`${dataDir}${dbFile}${dbExt}`);
    dbConn.prepare("PRAGMA journal_mode = WAL").run();
    dbConn
      .prepare(
        `CREATE TABLE IF NOT EXISTS [${table}] (id PRIMARY KEY, date DATETIME, timestamp INT, title VARCHAR, score DECIMAL, comparative INT)`
      )
      .run();

    const insertStmt = dbConn.prepare(
      `INSERT INTO [${table}] (date, timestamp, title, score, comparative) VALUES (?, ?, ?, ?, ?)`
    );
    dbConn.transaction(() => {
      parsedResults.forEach(
        ({ date, timestamp, title, score, comparative }) =>
          insertStmt.run(
            date, 
            timestamp, 
            title, 
            score, 
            comparative
          )
      );
    })
    console.log(`${parsedResults.length} added to ${table}`);
} 

      catch (e) {
         console.log(e.message);
       }
问题是我缺少一个()

回答如下

   try {

    const table = "coinna"
    const dbFile = "scraped"
    const dataDir = "./data/"
    const dbExt = ".db"

    const dbConn = db(`${dataDir}${dbFile}${dbExt}`);
    dbConn.prepare("PRAGMA journal_mode = WAL").run();
    dbConn
      .prepare(
        `CREATE TABLE IF NOT EXISTS [${table}] (id PRIMARY KEY, date DATETIME, timestamp INT, title VARCHAR, score DECIMAL, comparative INT)`
      )
      .run();

    const insertStmt = dbConn.prepare(
      `INSERT INTO [${table}] (date, timestamp, title, score, comparative) VALUES (?, ?, ?, ?, ?)`
    );
    dbConn.transaction(() => {
      parsedResults.forEach(
        ({ date, timestamp, title, score, comparative }) =>
          insertStmt.run(
            date, 
            timestamp, 
            title, 
            score, 
            comparative
          )
      );
    })(); //here is where the (); needed to be.
    console.log(`${parsedResults.length} added to ${table}`);
} 

      catch (e) {
         console.log(e.message);
       }
问题是我缺少一个()

回答如下

   try {

    const table = "coinna"
    const dbFile = "scraped"
    const dataDir = "./data/"
    const dbExt = ".db"

    const dbConn = db(`${dataDir}${dbFile}${dbExt}`);
    dbConn.prepare("PRAGMA journal_mode = WAL").run();
    dbConn
      .prepare(
        `CREATE TABLE IF NOT EXISTS [${table}] (id PRIMARY KEY, date DATETIME, timestamp INT, title VARCHAR, score DECIMAL, comparative INT)`
      )
      .run();

    const insertStmt = dbConn.prepare(
      `INSERT INTO [${table}] (date, timestamp, title, score, comparative) VALUES (?, ?, ?, ?, ?)`
    );
    dbConn.transaction(() => {
      parsedResults.forEach(
        ({ date, timestamp, title, score, comparative }) =>
          insertStmt.run(
            date, 
            timestamp, 
            title, 
            score, 
            comparative
          )
      );
    })(); //here is where the (); needed to be.
    console.log(`${parsedResults.length} added to ${table}`);
} 

      catch (e) {
         console.log(e.message);
       }

为什么不把id值和其他数据一起插入呢?实际上我也试过了,但也不起作用。我没有线索。
dbConn.transaction(()=>{parsedResults.forEach({id,timestamp,title,score,comparative})=>insertStmt.run(id,timestamp,title,score,comparative))
为什么不将id值与其他数据一起插入?我实际上也尝试过,但也不起作用。我没有任何线索。
dbConn.transaction(()=>{parsedResults.forEach({id,timestamp,title,score,comparative}=>insertStmt.run(id、时间戳、标题、分数、比较));