简体   繁体   English

Node.js和MySQL函数用法

[英]Node.js and MySQL function usage

New to node.js and just cant figure out how to do the following: node.js的新功能,并且无法弄清楚如何执行以下操作:

I have this on my db module: 我在我的数据库模块上有这个:

var mysql = require('mysql');
var MY_DATABASE='nodejs_mysql';

var client = mysql.createClient({
user: 'root',
password: 'root',
});

and im building this table: 和我建立这个表:

client.query(
'CREATE TEMPORARY TABLE '+USER+
'(username VARCHAR(255), '+
'password VARCHAR(255), '+
'name VARCHAR(255), '+
'picture VARCHAR(255), '+
'PRIMARY KEY(username))'
);

and later on, i want to perform this: 然后,我想执行此操作:

client.query('select username, password from ' + USER + 'where username=?',[req.body.username] , 'AND password=?', [req.body.password] 
function (err, results, fields) {
        if (err) {
            throw err;
        }
   //some actions performed here
});

all of those are in the same dataBase.js file. 所有这些都在同一个dataBase.js文件中。

how can i send username and password from another file named: server.js 如何从另一个名为server.js的文件中发送用户名和密码

as parameters to the query written above and get a certain value back? 作为上面写的查询的参数并获得一定的值?

is there any way to do that? 有没有办法做到这一点?

Ok, I think I get it now. 好的,我想我现在明白了。 You create temporary table in dataBase.js and want to perform query in this table in request handler in server.js . 您在dataBase.js创建临时表,并希望在server.js中的请求处理程序中执行此表中的查询。 If that's it, you should consider following aproach: 如果就是这样,你应该考虑遵循以下方法:

// dataBase.js

var mysql = require('mysql');
var MY_DATABASE='nodejs_mysql';

// connect to database
var client = mysql.createClient({
  user: 'root',
  password: 'root',
});

// create temporary table
client.query(
  'CREATE TEMPORARY TABLE '+USER+
  '(username VARCHAR(255), '+
  'password VARCHAR(255), '+
  'name VARCHAR(255), '+
  'picture VARCHAR(255), '+
  'PRIMARY KEY(username))'
);

// that's what I think you need. And yes, it looks like "a good to rap" for me.
module.exports.getInfoFromTemporaryTable : function( username, password, callback) {
  client.query(
    'select username, password from ' + USER + 'where username=?',
    [req.body.username] , 'AND password=?', [req.body.password], 
    callback
  );
}

The only thing I can't figure out is where you get USER variable from. 我唯一无法弄清楚的是你从哪里得到USER变量。 Pay attention to this moment. 注意这一刻。 Maybe pass it to getInfoFromTemporaryTable() function. 也许将它传递给getInfoFromTemporaryTable()函数。

// server.js
var db = require('./lib/dataBase');

app.get(someRoute, function(req, res) {
  db.getInfoFromTemporaryTable( req.body.username, req.body.password,
    function(err, results, fields) {
      if (err) {
        // handle errors or something
        return;
      }

      // do what you need to do, using results you got from temp table
  });
});

I'm not familiar with MySQL module you using, so above code is more like a general idea of what you need to implement. 我不熟悉您使用的MySQL模块,因此上面的代码更像是您需要实现的一般概念。 Hope it will help. 希望它会有所帮助。

how can i send username and password from another file named: server.js 如何从另一个名为server.js的文件中发送用户名和密码

as parameters to the query written above and get a certain value back? 作为上面写的查询的参数并获得一定的值?

Use the exports object. 使用exports对象。 See here for more information on NodeJS's module system. 有关NodeJS模块系统的更多信息,请参见此处

// server.js
exports.username = function {
  return "foo";
};

exports.password = function {
  return "bar";
};

// database.js
require('./server.js');
var client = mysql.createClient({
  user:     server.username,   // exports.username in server.js
  password: server.password,   // exports.password in server.js
});

Why are you splitting your query up? 为什么要拆分查询?

// server.js
exports.username = "Your username.";
exports.password = "Your password.";

// dataBase.js
// ... mysql connection setup ...
var server = require("./server");
client.query('SELECT username, password FROM `' + USER + '` WHERE username=? AND password=?',
  [server.username, server.password],
  function (err, results, fields) {
    if (err) {
      throw err;
    }
    // ... some actions performed here ...
  }
);

I'm not exactly sure what you were asking, but I think this should at least get you closer to your answer. 我不确定你在问什么,但我认为这至少应该让你更接近你的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM