无法在node-sqlite3中执行此操作(使用for循环)

时间:2019-01-15 01:41:01

标签: node-sqlite3 better-sqlite3

我需要在node-sqlite3中获得相同的结果,因为我想在rest api中使用它,并且它必须是异步的。我该怎么办?

当我在for循环的node-sqlite3中尝试它时,结果总是随机的。我不明白为什么。请帮助我。

我认为这与node-sqlite3的异步性有关。但是后来我意识到我需要它是异步的。

在Better-sqlite3中

const Database = require('better-sqlite3');

const tsePath = './tseFiles/94-ENAR-000.tse';

const db = new Database(tsePath);

let rows = [];

const sqlGetLastId = db.prepare("SELECT COUNT(ROWID) AS countFilledIn FROM WRITE WHERE T2 not null");
const sqlGetLatestRead = db.prepare("SELECT * FROM READ WHERE ROWID = ?");
const sqlGetLanguage = db.prepare("SELECT Dir, Subtag FROM EXTDIR WHERE LangID=?");

const count = sqlGetLastId.get().countFilledIn;
for (let i = 1; i <= count; i++) {
   let option = {
      id: i,
      sourceLang: "",
      sourceDir: "",
      sourceText: "",
      translationLang: "",
      translationDir: "",
      translationText: ""
   };
   const result1 = sqlGetLatestRead.get(i);
   option.sourceText = result1.source_text;
   option.translationText = result1.translated_text1;
   const sourceLanguage = result1.source_language;
   const translationLanguage = result1.target_language;
   const result2 = sqlGetLanguage.get(sourceLanguage);
   option.sourceLang = result2.Subtag;
   option.sourceDir = result2.Dir;
   const result3 = sqlGetLanguage.get(translationLanguage);
   option.translationLang = result3.Subtag;
   option.translationDir = result3.Dir;
   rows.push(option);
}

console.log(rows);


The output is:

[ { id: 1,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'It says—and I’m paraphrasing here—that the key to reading more books is to stop being so precious about it, you idiot.',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'ن الرائع، هذا النوع من ' },
  { id: 2,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText: '11 softballs',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AA' },
  { id: 3,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'Especially if they want to be the "authority" on what\'s hot and what\'s not!',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAA' },
  { id: 4,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'These wallets require more than one user to enter their key before funds can be transferred.',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAAA' },
  { id: 5,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'The Linksys EA8300 Max-Stream router boasts top speeds and tons of features for medium-size homes, and it\'s affordable at $200 or £150 in the UK.',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAAAA' },
  { id: 6,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText: 'Things to See and Do in Hong Kong',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAAAAA' },
  { id: 7,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'Retail isn\'t dying but it is evolving and through that evolution you will see a lot of store closures, but you will see stronger brands emerge.',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAAAAAA' },
  { id: 8,
    sourceLang: 'en',
    sourceDir: 'ltr',
    sourceText:
     'Anybody from Iran in the comment section ever hear of these rich kids actually getting in legal trouble or does their families\' wealth protect them? Genuinely just curious.',
    translationLang: 'ar',
    translationDir: 'rtl',
    translationText: 'AAAAAAAA' } ]

0 个答案:

没有答案