You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// data3.jsconstsql=require('sqlite3');//const sql = require('duckdb');constcsvParser=require('csv-parser');conststream=require('stream');// CSV data as a stringconstcsvData=`Wind speed (m/s),Output power (kW)0,01,02,03,04,805,1406,3607,6108,10009,147010,190011,232012,269013,285014,295015,300016,300017,300018,300019,300020,300021,300022,300023,300024,300025,3000`;// Function to create a database, table, and insert dataasyncfunctioncreateAndStoreData(){console.log('createAndStoreData()');constdb=newsql.Database('power_curve.db');returnnewPromise((resolve,reject)=>{db.serialize(()=>{db.run('DROP TABLE IF EXISTS power_curve');db.run('CREATE TABLE IF NOT EXISTS power_curve (ws DECIMAL PRIMARY KEY, power DECIMAL)');conststmt=db.prepare('INSERT INTO power_curve VALUES (?, ?)');constdataStream=newstream.Readable();dataStream.push(csvData);dataStream.push(null);dataStream.pipe(csvParser()).on('data',(row)=>{stmt.run(row['Wind speed (m/s)'],row['Output power (kW)']);}).on('end',()=>{stmt.finalize();db.close((err)=>{if(err){reject(err);}else{console.log('Data stored in power_curve.db successfully.');resolve();}});}).on('error',(error)=>{reject(error);});});});}// Function to read data from the databaseasyncfunctionreadData(){console.log('readData()');constdb=newsql.Database('power_curve.db');returnnewPromise((resolve,reject)=>{db.serialize(()=>{db.all('SELECT * FROM power_curve',(err,rows)=>{if(err){reject(err);}else{console.table(rows);console.log('Data read from power_curve.db successfully.');resolve();}db.close((err)=>{if(err){reject(err);}});});});});}asyncfunctionmain(){try{awaitcreateAndStoreData();awaitreadData();}catch(error){console.error('Error:',error.message);}}if(require.main===module){main();}
Running the code with node data.js would give the following error:
createAndStoreData()
Data stored in power_curve.db successfully.
readData()
Error: Connection Error: Connection was never established or has been closed already
It could be overcome at the moment if I use the same database connection among the two function.
//const sql = require('sqlite3');constsql=require('duckdb');constcsvParser=require('csv-parser');conststream=require('stream');// CSV data as a stringconstcsvData=`Wind speed (m/s),Output power (kW)0,01,02,03,04,805,1406,3607,6108,10009,147010,190011,232012,269013,285014,295015,300016,300017,300018,300019,300020,300021,300022,300023,300024,300025,3000`;// Function to create a database, table, and insert dataasyncfunctioncreateAndStoreData(db){console.log('createAndStoreData()');returnnewPromise((resolve,reject)=>{db.serialize(()=>{db.run('DROP TABLE IF EXISTS power_curve');db.run('CREATE TABLE IF NOT EXISTS power_curve (ws DECIMAL PRIMARY KEY, power DECIMAL)');conststmt=db.prepare('INSERT INTO power_curve VALUES (?, ?)');constdataStream=newstream.Readable();dataStream.push(csvData);dataStream.push(null);dataStream.pipe(csvParser()).on('data',(row)=>{stmt.run(row['Wind speed (m/s)'],row['Output power (kW)']);}).on('end',()=>{stmt.finalize();console.log('Data stored in power_curve.db successfully.');resolve();}).on('error',(error)=>{reject(error);});});});}// Function to read data from the databaseasyncfunctionreadData(db){console.log('readData()');returnnewPromise((resolve,reject)=>{db.serialize(()=>{db.all('SELECT * FROM power_curve',(err,rows)=>{if(err){reject(err);}else{console.table(rows);console.log('Data read from power_curve.db successfully.');resolve();}});});});}asyncfunctionmain(){try{constdb=newsql.Database('power_curve.db');awaitcreateAndStoreData(db);awaitreadData(db);db.close((err)=>{if(err){console.error('Error closing database:',err.message);}});}catch(error){console.error('Error:',error.message);}}if(require.main===module){main();}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have posted a question in https://stackoverflow.com/questions/77475071/sequential-run-of-sql-functions-in-javascript/77475150#77475150
Here is the code that to be run from node.js:
Running the code with
node data.js
would give the following error:It could be overcome at the moment if I use the same database connection among the two function.
Beta Was this translation helpful? Give feedback.
All reactions