P
P
prolina2018-10-30 20:28:37
JavaScript
prolina, 2018-10-30 20:28:37

Storing an array in a DB (sqlite)?

Hello! There is a csv file, I parsed it, I got an array, now I need to transfer this array to the database. How to do this using node js and sqlite??

class Person {
 getData(filename) {
        return new Promise((resolve, reject) => {
            let arrayData = [];
            const csv = require('fast-csv');
            csv.fromPath(`${filename}.csv`, {
                headers: true,
                strictColumnHandling: true,
                delimiter: ',',
                ignoreEmpty: true
            })
                .on('data', data => arrayData.push(data))
                .on('end', () => {
                    resolve({
                        arrayData,
                        title: filename,
                        columnCountData: Object.keys(arrayData[0]).length,
                        rowCountData: arrayData.length + 1, // data plus header
                    });
                })
                .on('error', (error) => {
                  reject(error);
                });

        });
    }
}
let person = new Person();
person.getData('person')
      .then((array) => {
          const sqlite3 = require('sqlite3').verbose();
// open the database
          let db = new sqlite3.Database('./ook.db',sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
              if (err) {
                  console.error(err.message);
              }
              console.log("DB is connected");
          });
          db.close((err) => {
              if (err) {
                  console.error(err.message);
              }
              console.log('Close the database connection.');
          });
          console.log(array);
      })
      .catch((error) => {
          console.log('все накрылось, потому что: ', error)
      });

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question