javascript - mysql模块中的sqlMessage比较错误无法正常工作
问题描述
- 我正在使用nodejs MySQL模块创建一个每个月都会自动创建的表,所以我需要检查是否
err.sqlMessage == Table 'tableName' already exists
因为它存在继续执行我的代码,如果它不存在 - 它将创建一个带有月份名称的新表和继续执行我的代码 - 我正在使用一个var,它将通过
Date()
方法自动更新,所以当我尝试比较语法时应该是这样的err.sqlMessage == "Table '"+varName+"' already exists"
- 但是当我试图以这种方式进行比较时,它给了我一个错误,我不知道为什么......有人可以帮助我吗?
这是我的代码
const mysql = require('mysql');
const http = require("http");
var user = "test",
pass = "test",
server = "localhost";
//date var
var x = new Date(),
months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
//get year and month
m_y = "`" +
months[x.getMonth()] +
x.getFullYear() +
"`",
//ready for connecting to the server
var serverCon = mysql.createConnection({
host: server,
user: user,
password: pass
});
var con = mysql.createConnection({
host: server,
user: user,
password: pass,
database: "accountantdb"
});
//check var
var q, w, e, r, t, y, u, i, o, p, a, s, d, f, g, h, j, k, l, z, x, c, v, b, n, m;
function createMonthTable() {
sql2 = `
CREATE TABLE ` + m_y + ` (
month_days VARCHAR(500) PRIMARY KEY NOT NULL
)`;
con.query(sql2, function (MT_ERR, MT_RES) {
if (MT_ERR == null) {
w = "table created successfully";
} else {
w = MT_ERR.sqlMessage;
}
if (w == "table created successfully" || w == "Table '" + m_y + "' already exists") {
w = "true";
//code here
} else {
throw w;
}
})
}
function connectToNewDB() {
con.connect(function (error) {
if (error) {
throw error
} else {
createMonthTable();
}
})
}
function createdb() {
sql1 = `
CREATE DATABASE accountantdb
`;
serverCon.query(sql1, function (create_ERR, createRES) {
if (create_ERR == null) {
q = "db created successfully"
} else {
q = create_ERR.sqlMessage;
}
if (q == "db created successfully" || q == "Can't create database 'accountantdb'; database exists") {
connectToNewDB();
} else {
throw q;
}
})
}
serverCon.connect(function (err) {
if (err) {
throw err;
} else {
//when connected - create a new db and connect to it
createdb();
}
});
http.createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/html"
});
res.write(w);
res.end();
}).listen(8000, "hp-pc", function () {
console.log("server running on hp-pc:8000");
})
错误分区
function createMonthTable() {
sql2 = `
CREATE TABLE ` + m_y + ` (
month_days VARCHAR(500) PRIMARY KEY NOT NULL
)`;
con.query(sql2, function (MT_ERR, MT_RES) {
if (MT_ERR == null) {
w = "table created successfully";
} else {
w = MT_ERR.sqlMessage;
}
if (w == "table created successfully" || w == "Table '" + m_y + "' already exists") {
w = "true";
//code here
} else {
throw w;
}
})
}
解决方案
1-你应该用“”替换你已经添加到数据库名称符号限制的 GRAVE ACCENT
2-将月份名称转换为小写,因为 sql 已经将其转换为小写然后比较你的字符串
let str = String("Table '"+m_y.replace(/\`/ig,"").toLowerCase()+"' already exists");
- 代码应该是这样的
function createMonthTable() {
sql2 = `
CREATE TABLE ` + m_y + ` (
month_days VARCHAR(500) PRIMARY KEY NOT NULL
)`;
con.query(sql2, function (MT_ERR, MT_RES) {
if (MT_ERR == null) {
w = "table created successfully";
} else {
w = MT_ERR.sqlMessage;
}
let str = String("Table '"+m_y.replace(/\`/ig,"").toLowerCase()+"' already exists");
if (w == "table created successfully" || w == str) {
w = "true";
//code here
} else {
throw w;
}
})
}
- 为学习而创建的答案
推荐阅读
- javascript - 类型错误:firebase_app__WEBPACK_IMPORTED_MODULE_0___default.a.analytics 不是函数
- java - 为什么Java字符串数组在第一个单元格中将字符串附加到null?
- javascript - 当 Array.map 添加或删除 react.js 中的 div 时,将 css 动画应用于 div
- python - Shell 中的 (10, 10, 10) 和 10 10 10 有什么区别?
- c++ - 如何通过 Windows API 枚举系统的所有知名 SID?
- c - Vim:在终端中编译和运行 C 程序
- java - Java Scanner 阅读领先空间
- python - 在 python 的 asctime 中使用毫秒和日志消息
- java - NoClassDefFoundError 十进制格式
- r - 如何获得R中像素簇的大小