[英]How can I begin MySQL work in Node.js on Windows?
I've begun playing around with Node.js lately, for many reasons but most importantly the ease at which I can write a chat-server utilising HTML5 WebSockets. 我最近开始使用Node.js,原因很多,但最重要的是我可以轻松地使用HTML5 WebSockets编写聊天服务器。 However, I've been stuck for weeks with MySQL. 但是,我已经被MySQL困住了好几个星期。
I'm currently using this MySQL client module: https://github.com/sidorares/nodejs-mysql-native 我目前正在使用这个MySQL客户端模块: https : //github.com/sidorares/nodejs-mysql-native
I've connected to the database and managed to store data using the following code: 我已连接到数据库并使用以下代码设法存储数据:
// MySQL database
var db = require("mysql-native").createTCPClient(); // localhost:3306 by default
db.auto_prepare = true;
db.auth(dbName, dbUser, dbPass);
// Update the database
db.execute("UPDATE server_data SET value='" + new Date() + "' WHERE name='lastLoaded'");
How may I go about retrieving data from the database using a SELECT * FROM x WHERE y=z
query? 如何使用SELECT * FROM x WHERE y=z
查询从数据库中检索数据?
Is there any specific reason you chose nodejs-mysql-native
over node-mysql which is a really good node module. 您是否有任何特定的原因选择nodejs-mysql-native
over node-mysql ,这是一个非常好的节点模块。 If there is none, then you should probably try node-mysql
. 如果没有,那么你应该尝试node-mysql
。 I've tried it and it is great to start off using MySQL with Node. 我已经尝试过了,开始使用带有Node的MySQL非常棒。 You could do something like: 你可以这样做:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'your_username',
password : 'your_password',
});
connection.connect();
connection.query("UPDATE server_data SET value=? WHERE name=?", [new Date(), 'lastLoaded'] function(err, result) {
if (err) throw err;
console.log('Result: ', result);
});
connection.end();
The advantage you get by using it this way is that you can prevent SQL injection, which is taken care of internally in node-mysql (by using the connection.escape() method). 通过这种方式使用它可以获得的优势是可以防止SQL注入,这在node-mysql内部处理(通过使用connection.escape()方法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.