我想创建一个与mysql交互的管理器,但是我无法将它带入主程序。我主要只想尝试使用javascript来解决这个问题(java,c background)。
我有两个名为main.js和MSYQLConnector.js的文件。我想从
使用MYSQLConnectormain.js
var root = __dirname, express = require('express');
var app = express();
var sql = require('./DBConnectors/MYSQLConnector.js');
var a = sql.sqlTest;//????? fail....
MYSQLConnector.js
var sqlTest = function (){
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'xxxx'
});
connection.connect();
connection.query('SELECT * from asset', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
};
如何进行导入?感谢
答案 0 :(得分:2)
您需要从模块中导出该功能。添加:
exports.sqlTest = sqlTest;
到MYSQLConnector.js文件的底部。
另请参阅the nodejs api documentation了解详情。
答案 1 :(得分:1)
你几乎在那里,添加"出口"在它面前:
exports.sqlTest = function (){
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'xxxx'
});
connection.connect();
connection.query('SELECT * from asset', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
};