0

我需要使用 node-sqlite3 获得相同的结果,因为我想在 rest api 中使用它并且它需要是异步的。我怎样才能做到这一点?

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

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

在更好的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' } ]
4

0 回答 0