我需要将值稳定地插入数据库中。我已经尝试过使用此代码:
var mysql = require("mysql");
const random = require("random");
var con = mysql.createConnection({
host: "xxx",
user: "xxx",
password: "xxx",
database: "xxx"
});
con.connect(function(err) {
while (true) {
if (err) throw err;
val1_ = random.int((min = 0), (max = 100));
val2_ = random.int((min = 0), (max = 100));
val3_ = random.int((min = 0), (max = 100));
val4_ = random.int((min = 0), (max = 100));
var sql =
"INSERT INTO Table (val1, val2, val3, val4)VALUES (" +
val1_ +
"," +
val2_ +
"," +
val3_ +
"," +
val4_ +
")";
con.query(sql, function(err, result) {
if (err) throw err;
});
}
});
但是我得到这个错误:Cannot enqueue Handshake after already enqueuing a Handshake.
,我知道这可能不是完成此任务的方法,只是让您了解我想做什么。我该如何解决这个问题?
答案 0 :(得分:0)
调用连续connection.connect()
时抛出此错误。在这里,您应该删除初始con.connect()
会很好,因为con.query()
会隐式建立连接。
请参见建立连接部分here
while (true) {
val1_ = random.int((min = 0), (max = 100));
val2_ = random.int((min = 0), (max = 100));
val3_ = random.int((min = 0), (max = 100));
val4_ = random.int((min = 0), (max = 100));
var sql =
"INSERT INTO Table (val1, val2, val3, val4)VALUES (" +
val1_ +
"," +
val2_ +
"," +
val3_ +
"," +
val4_ +
")";
con.query(sql, function(err, result) {
if (err) throw err;
});
}
对于多插入(这样做不是一个好方法),建议收集数据并一次插入即可:
let sql = `INSERT INTO Table(val1, val2, val3, val4) VALUES ? `;
let valsArray = [];
//while must have a terminating condition for that query to get executed/reached
while (true) {
val1_ = random.int((min = 0), (max = 100));
val2_ = random.int((min = 0), (max = 100));
val3_ = random.int((min = 0), (max = 100));
val4_ = random.int((min = 0), (max = 100));
valsArray.push([val1_, val2_, val3_, val4_]);
}
//notice valsArray would be an array of array values
connection.query(sql, [valsArray], (err, results, fields) => {
if (err) {
return console.error(err.message);
}
// get inserted rows
console.log("Row inserted:" + results.affectedRows);
});
注意:有关其test suites之一的连续连接错误的更多详细信息
答案 1 :(得分:0)
在您的 app.js 或类似的主应用程序文件中:
const mysql = require('mysql');
const connection = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST || '127.0.0.1',
user: process.env.DB_USER || 'local_user',
password: process.env.DB_PASSWORD || 'local_password',
database: process.env.DB_NAME || 'local_database'
});
global.db = connection;
// access the pool of connection anywhere without
// additional require or module.exports
my_tasks.js 或应用程序中执行插入的其他地方
const random = require('random');
// define the query, ? is a placeholder for object to be inserted
let sql = "INSERT INTO table_name SET ?;";
// begin your while loop if needed
//define object to be inserted where keys match table field names and values are your values
let payload = {
val1_: random.int((min = 0), (max = 100)),
val2_: random.int((min = 0), (max = 100)),
val3_: random.int((min = 0), (max = 100)),
val4_: random.int((min = 0), (max = 100)),
}
db.query(sql, payload, function(error, results, fields) {
if (error) throw error;
console.log(results);
});
// end your loop
我不确定您正在while
循环中测试什么,但是可以根据需要将其应用于第二个代码块。