我正在尝试使用 node.js 插入一些数据。我通过 npm 安装了 MySQL 支持,但似乎仍然无法使用 INSERT INTO 输入正确的字符串。(数据库中的行类型是 tinytext,字符串小于 255 个字符。)
这是我的代码 https://hastebin.com/komikahatu.http
预期的输出是它插入参数(/name 捕获的内容)和执行命令的用户的用户名(/name)。
console.log('Dirname: ' + __dirname);
const bot = require('../core.js');
bot.on("polling_error", (msg) => console.log(msg));
var config = require('../config.json');
var mysql = require('mysql');
var sqlhost = config.sql.host;
var sqluser = config.sql.user;
var sqlpass = config.sql.password;
var sqldb = config.sql.database;
var con = mysql.createConnection({
host: sqlhost,
user: sqluser,
password: sqlpass,
database: sqldb
});
bot.onText(/\/name (.+)/, (msg, match) => {
const chatId = msg.chat.id;
// 'msg' is the received Message from Telegram
// 'match' is the result of executing the regexp above on the text content
// of the message
let username;
const name = match[1].toString(); // the captured name
if(msg.from.username !== ''){
username = msg.from.username;
}
var setnickname = {
username_: username,
nickname_: name
};
var sql = con.query('INSERT INTO nickname_data VALUES (?)', setnickname, function (err, result){
console.log(sql.sql);
if (err) {
console.error(err)
return;
}
console.error(result);
});
if (name !== "undefined") {
bot.sendMessage(chatId, 'Welcome ' + name + '!');
// bot.sendMessage(chatId, 'Welcome ' + msg.from.name + '!');
// config.information.normalusername = name;
}
});