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
Hi Guru
I used the code below it works perfectly in Nodejs.
var query= "SELECT * FROM sensorva WHERE time BETWEEN '2023-03-01 22:30:00' AND '2023-03-01 22:31:00'; "
Actually, I want to change time value to variables in Nodejs.
With a little knowledge in mysql2 package and Nodejs,
So I asked ChatGPT ,he helped to code this.
const mysql = require('mysql2');
connection.query('SELECT * FROM sensorva WHERE time BETWEEN :start_time AND :end_time',
{
start_time: ${date1} ${time1},
end_time: ${date2} ${time2}
},
function(err, results) {
if (err) {
console.error(err);
return;
}
console.log(results);
}
);
Unfortunately,it did not work and returned the error message.
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':start_time AND :end_time' at line 1",
sql: 'SELECT * FROM sensorva WHERE time BETWEEN :start_time AND :end_time'
}
Please give me some advice , how to use the parameters or variables in mysql2 correctly.
Thank you very much
Banatus Soiraya
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
-
Hi Guru
I used the code below it works perfectly in Nodejs.
var query= "SELECT * FROM sensorva WHERE time BETWEEN '2023-03-01 22:30:00' AND '2023-03-01 22:31:00'; "
Actually, I want to change time value to variables in Nodejs.
With a little knowledge in mysql2 package and Nodejs,
So I asked ChatGPT ,he helped to code this.
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
const date1 = '2023-03-01';
const time1 = '22:30:00';
const date2 = '2023-03-02';
const time2 = '22:31:00';
connection.query('SELECT * FROM sensorva WHERE time BETWEEN :start_time AND :end_time',
{
start_time:
${date1} ${time1}
,end_time:
${date2} ${time2}
},
function(err, results) {
if (err) {
console.error(err);
return;
}
console.log(results);
}
);
Unfortunately,it did not work and returned the error message.
code: 'ER_PARSE_ERROR',
errno: 1064,
sqlState: '42000',
sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ':start_time AND :end_time' at line 1",
sql: 'SELECT * FROM sensorva WHERE time BETWEEN :start_time AND :end_time'
}
Please give me some advice , how to use the parameters or variables in mysql2 correctly.
Thank you very much
Banatus Soiraya
Beta Was this translation helpful? Give feedback.
All reactions