|
|
|
const path = require('path'); |
|
|
|
|
|
const sqlite3 = require('sqlite3').verbose(); |
|
|
|
const fs = require('fs'); |
|
|
|
|
|
console.log(__dirname); |
|
|
|
const dbFilePath = path.resolve(__dirname,'../database/data.db'); |
|
console.log('The dbpath is', dbFilePath); |
|
|
|
const db = new sqlite3.Database(dbFilePath, (err) => { |
|
if (err) { |
|
console.error('Error connecting to SQLite database:', err.message); |
|
console.log("Current working directory:", process.cwd()); |
|
} else { |
|
console.log('Connected to SQLite database'); |
|
} |
|
}); |
|
|
|
|
|
function runQuery(query, params = []) { |
|
return new Promise((resolve, reject) => { |
|
db.run(query, params, function (err) { |
|
if (err) { |
|
reject(err); |
|
} else { |
|
resolve(this); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
|
|
function runSelectQuery(query, params = []) { |
|
return new Promise((resolve, reject) => { |
|
db.all(query, params, (err, rows) => { |
|
if (err) { |
|
reject(err); |
|
} else { |
|
resolve(rows); |
|
} |
|
}); |
|
}); |
|
} |
|
|
|
module.exports = {runQuery, runSelectQuery} |
|
|