如何在Node.JS中使用SQL [英] How to use SQL with Node.JS

查看:248
本文介绍了如何在Node.JS中使用SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我对整个后端世界还算陌生,现在我正在使用node.js进行编码. 我有一个项目,我必须将用户存储在几个字段中. 但是我不知道如何使用节点连接到数据库并进行查询.

So,I'm fairly new to the whole backend world, and I'm now coding with node.js. I have a project that I have to store a users with a few fields. But I don't know how to connect to the database with node and make queries.

而且,我有WAMP堆栈,因为我曾经编写过PHP(非常麻烦),我也可以将它的MySQL服务器用于node.js吗?还是我需要去MySQL站点,下载并安装它?

And,I have the WAMP stack, because I used to code PHP (very litle), can I use its MySQL server for node.js too? Or do I need to go to the MySQL site,download and install it?

推荐答案

如果您只需要进行少量SQL访问,而不是Sequelize,那么直接使用mysql javascript驱动程序可能会更快乐.

Rather than Sequelize, if you just need to do a little SQL access, you might be happier just using the mysql javascript driver directly.

npm install mysql

您应该能够很好地使用现有的mysql服务器,前提是您可以从放置节点应用程序的机器上访问它(访问=网络访问 和您的mysql帐户访问控制设置.

You should be able to use your existing mysql server just fine, provided you have access to it from the machine you are putting your node app on (access = network access and your mysql account access control settings.

一个非常简单的用法示例是:

A very simple example of usage is:

var mysql      = require('mysql');
var connection = mysql.createConnection({
    host     : 'locahost',
    user     : 'foo',
    password : 'bar',
    database : 'test'
});

// the callback inside connect is called when the connection is good
connection.connect(function(err){
    var sql = "select 'Joe' as name from dual";

    connection.query(sql, function(err, rows, fields) {
        if (err) return console.log(err);
        //  you need to end your connection inside here.
        connection.end();
        console.log(rows[0].name);
    });
});

上面的sql可以是任何有效的sql.如果要使用变量替换,请将?插入查询中,并将替换数组作为第二个参数传递给connection.query,例如:

The sql above could be any valid sql. If you want to use variable substitution, insert ? into your query and pass an array of substitutions as the second parameter to connection.query, for example:

var sql='update customer set name=? where id=?';
connection.query(sql,[req.body.name,req.body.id], function(...

您可能会发现这个问题对于总体结构,我回答很有帮助.请注意大量使用回调.不可避免的菜鸟动作涉及到没有正确的回调结构,从而导致部分代码在必要的事情发生之前运行(即,您的代码正在尝试在实际上已经建立数据库连接的之前运行查询. ).

You might find this question I answered helpful regarding the general structure. Please note the heavy use of callbacks. The inevitable rookie moves involve not having the callback structure correct, thereby causing portions of your code to run before necessary things have happened (i.e. your code is trying to run a query before it actually has established the database connection).

如果您要使用MySQL做更多事情,那么某种抽象的ORM库可能对您更有意义.但是请注意性能的影响-其中一些是重量级的.参见

If you are going to do more with MySQL then some sort of abstracted ORM library might make more sense for you. Be aware of the performance implications though - some of them are fairly heavyweight. See this article as an example.

希望这会有所帮助.如果遇到问题,请提出问题,并提出导致问题的代码,您将在这里得到非常及时的答复.

Hope this helps. If you have trouble, post a question, with the code causing problems, and you'll get pretty timely answers here.

这篇关于如何在Node.JS中使用SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆