let insertSql = 'insert into User(username ,password,name,gender,age) values (?,?,?,?,?)';
let insertParams = [username, password, name, gender, age];
mysqlConnection.query(insertSql, insertParams, function (error, results, fields) {
if (error) throw error;
return results;
});
//2.select operation (when I tested it myself, I found that string type columns need to add additional quotation marks to the parameters, otherwise they will be treated as numeric types or column names)
let selectSql = 'select id from User where username = \''.concat(username, '\' and password = \'', password, '\'');
mysqlConnection.query(selectSql, function (error, results, fields) {
if(error) throw error;
return results;
});
//3.delete operation
let deleteSql = 'delete from user where id = \''.concat(id, '\'');
mysqlConnection.query(deleteSql, function (error, results, fields) {
if (error) throw error;
return results;
}) ;
//4.update operation
let updateSql = 'update User set age = 30 where id = \''.concat(id, ' \'');
mysqlConnection.query(updateSql, function (error, results, fields) {
if (error) throw error;
return results;
});
The above is the detailed content of Implementation of the basic writing method of adding, deleting, modifying and querying MySql in NodeJS. For more information, please follow other related articles on the PHP Chinese website!