I need to make MySQL query using "WHERE IN". This is my query:
var myQuery = 'SELECT uid FROM ' +tableName+ ' where Gender IN (' + Info.Gender.join() + ')';
If i print Info.Gender
it will be ['Male', 'Female'], as a string.
but when the query is done it says
SELECT uid FROM appUsers where Gender IN (Male, Female)
But should be:
SELECT uid FROM appUsers where Gender IN ('Male', 'Female')
That means it takes the Female not as a string.
Any ideas?
Step 1, install the NPM package called sqlite3 (Read the sqlite3 docs here). sqlite3 helps you to connect to your SQLite database and to run queries. Step 2, In your the JS file where you want to run the SQLs, import/require sqlite3 and fs (No, you don't need to install this one. It comes with NodeJS).
Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver.
const connection = mysql. createConnection({ host: 'localhost', // host for connection port: 3306, // default port for mysql is 3306 database: 'test', // database from which we want to connect out node application user: 'root', // username of the mysql connection password: 'root' // password of the mysql connection });
You should use query escaping (provided that you're using node-mysql
):
var myQuery = 'SELECT uid FROM ?? where Gender IN (?)';
connection.query(myQuery, [ tableName, Info.Gender ], ...);
You need single quotes in your query:
var myQuery = "SELECT uid FROM " +tableName+ " where Gender IN ('" + Info.Gender.join("','") + "')";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With