Home  >  Article  >  Database  >  Implementation of the basic writing method of adding, deleting, modifying and querying MySql in NodeJS

Implementation of the basic writing method of adding, deleting, modifying and querying MySql in NodeJS

零下一度
零下一度Original
2017-06-27 09:59:051423browse

Purely record the most basic writing method. There is almost no logic and the writing method is not very perfect (because I have just figured out how to write like this...= =!) I hope experts can give me some advice and I hope it can help. To a newbie who is newer than me....

//1.insert operation

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn