首页 > 解决方案 > mysql模块中的sqlMessage比较错误无法正常工作

问题描述

这是我的代码

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;
    }
  })
}

标签: javascriptmysqlnode.js

解决方案


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;
    }
  })
}
  • 为学习而创建的答案

推荐阅读