DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+3 votes

I can't figure out where should I be using async/await. I'm getting many errors related to that:

const sqlite3 = require('sqlite3');

function main() {
  sqliteTest();
}

function sqliteTest() {
  const db = openDatabase('metadata.sqlite');
  createDatabaseSchema(db);
  sql=`
    INSERT INTO file(name, hash, path)
      VALUES (?, ?, ?)
  `;
  insertRowDatabase(db, sql, ['file_name', 'file_hash', 'file_path']);

  sql = `SELECT * FROM file`;
  queryDatabase(db, sql);
}

function queryDatabase(db, sql) {
  db.all(sql, [], (err, rows) => {
    rows.forEach((row) => {
      console.log(row);
    });
  });
}

function openDatabase(dbName) {
  return new sqlite3.Database(dbName, sqlite3.OPEN_READWRITE, (err) => {
    if (err) return console.error(err.message);
    console.log('Successful db connection');
  });
}

async function createDatabaseSchema(db) {
  sql="CREATE TABLE if not exists file(name, hash, path)";
  await db.run(sql);
}

async function insertRowDatabase(db, sql, values) {
  await db.run(sql, values, (err) => {
    if (err) return console.error(err.message);
    console.log("Created " + values);
  });
}

main();

One error is:

await db.run(sql);
           ^
TypeError: db.run is not a function

Another error:

SQLITE_ERROR: no such table: file
/home/user/code/js/jedi-archives/sqlite.js:34
    rows.forEach((row) => {
         ^

TypeError: Cannot read properties of undefined (reading 'forEach')
by
0

Is this node.js?

1 Answer

+1 vote
 
Best answer

I've ran your code with Node but I don't see the same errors. Actually, it woks fine. Do you already have a database? Because if you don't, you need to use sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE.

by
selected by
0

node yes. I've been modifying it so I guess it was finally right when I copied it.

Contributions licensed under CC0
...