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

零下一度
Release: 2017-06-27 09:59:05
Original
1367 people have browsed it

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!