Help with nested queries #2311
-
Hello, I'm currently building a discord.js bot and have up until now been using sqlite for my database but has just now moved to the mysql2 library, however I'm running into an issue: databaseConnection.query("SELECT * FROM table WHERE row1 = ?", row1, (err, result) => {
if (err) {
return console.error(err)
}
if (!result[0]) {
databaseConnection.query("INSERT INTO table (row1, row2) VALUES (?,?)", row1,row2, (err) => {
if (err) {
return console.error(err)
}
console.log("Success")
})
}
}) However it has come to my attention that you aren't able to actually run this code because this library (mysql2) requires async and promises? I have a few questions:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
likely your driver exposes synchronous api. mysql2 is built on top of node
you could try or wrap your code as an executable script and call it with
pass your parameters as an array: |
Beta Was this translation helpful? Give feedback.
-
@sidorares Thank you so much! You lead me in the right direction, I was able to use |
Beta Was this translation helpful? Give feedback.
likely your driver exposes synchronous api. mysql2 is built on top of node
net
functionality and only provides async interface.you could try
https://www.npmjs.com/package/sync-mysql
or wrap your code as an executable script and call it with
execSync
I'd imagine performance is going to be much worse with sync versions
pass your parameters as an array:
databaseConnection.query("INSERT INTO table (row1, row2) VALUES (?,?)", row1,row2, (err) => {
. Sin…