first commit
This commit is contained in:
204
server/comman/db.js
Normal file
204
server/comman/db.js
Normal file
@ -0,0 +1,204 @@
|
||||
const mysql = require('mysql')
|
||||
|
||||
const db = mysql.createPool({
|
||||
host: '127.0.0.1',
|
||||
user: 'root',
|
||||
password: '',
|
||||
database: 'remotemqtt',
|
||||
})
|
||||
let queryacy = function( sql, values ) {
|
||||
// 返回一个 Promise
|
||||
return new Promise(( resolve, reject ) => {
|
||||
db.getConnection(function(err, connection) {
|
||||
if (err) {
|
||||
reject( err )
|
||||
} else {
|
||||
connection.query(sql, values, ( err, rows) => {
|
||||
|
||||
if ( err ) {
|
||||
reject( err )
|
||||
} else {
|
||||
resolve( rows )
|
||||
}
|
||||
// 结束会话
|
||||
//console.log(sql);
|
||||
connection.release()
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// const sqlite3 = require('sqlite3').verbose()
|
||||
// //打开或者创建一个数据库
|
||||
|
||||
|
||||
// var db = new sqlite3.Database(
|
||||
// './db.sqlite3',
|
||||
// sqlite3.OPEN_READWRITE,
|
||||
// function (err) {
|
||||
// if (err) {
|
||||
// return console.log(err.message)
|
||||
// }
|
||||
|
||||
// }
|
||||
// )
|
||||
// resetpath=async function(path)
|
||||
// {
|
||||
|
||||
// db = new sqlite3.Database(
|
||||
// path,
|
||||
// sqlite3.OPEN_READWRITE,
|
||||
// function (err) {
|
||||
// if (err) {
|
||||
// return console.log(err.message)
|
||||
// }
|
||||
// console.log('connect database successfully')
|
||||
// }
|
||||
// )
|
||||
// db.run("CREATE TABLE IF NOT EXISTS remoteswitch (\
|
||||
// `AUTOID` INTEGER PRIMARY KEY AUTOINCREMENT,\
|
||||
// `id` tinytext NOT NULL,\
|
||||
// `type` tinytext NOT NULL,\
|
||||
// `tips` text NOT NULL,\
|
||||
// `other` text NOT NULL,\
|
||||
// `switch1` tinyint(1) NOT NULL,\
|
||||
// `switch2` tinyint(1) NOT NULL,\
|
||||
// `switch3` tinyint(1) NOT NULL,\
|
||||
// `switch4` tinyint(1) NOT NULL,\
|
||||
// `is4G` tinyint(1) NOT NULL,\
|
||||
// `device` tinytext DEFAULT NULL)",(err)=>{console.log(err);
|
||||
// })
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// // db.query=db.run
|
||||
// //如果不存在 remoteswitch 表则创建 并打印创建成功 如果错误则打印错误
|
||||
|
||||
// function ConvertoKeyValue(data)
|
||||
// {
|
||||
|
||||
// let fields = [];
|
||||
// let fields1 = [];
|
||||
// let fields2 = [];
|
||||
// for (let key in data) {
|
||||
// // 如果值是字符串,需要用引号包裹
|
||||
// let value = typeof data[key] === 'string' ? `'${data[key]}'` : data[key];
|
||||
// fields1.push(key);
|
||||
// fields2.push( value);
|
||||
|
||||
// }
|
||||
|
||||
// fields.push(fields1.join(","))
|
||||
// fields.push(fields2.join(","))
|
||||
// return fields
|
||||
// }
|
||||
// function ConvertoKeyEQValue(data)
|
||||
// {
|
||||
|
||||
// let fields = [];
|
||||
|
||||
// for (let key in data) {
|
||||
// // 如果值是字符串,需要用引号包裹
|
||||
// let value = typeof data[key] === 'string' ? `'${data[key]}'` : data[key];
|
||||
// fields.push(key+"="+value);
|
||||
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// return fields.join(",")
|
||||
// }
|
||||
|
||||
db.query("CREATE TABLE IF NOT EXISTS remoteswitch (\
|
||||
AUTOID INT AUTO_INCREMENT PRIMARY KEY,\
|
||||
id TINYTEXT NOT NULL,\
|
||||
type TINYTEXT NOT NULL,\
|
||||
tips TEXT NOT NULL,\
|
||||
other TEXT NOT NULL,\
|
||||
switch1 TINYINT(1) NOT NULL,\
|
||||
switch2 TINYINT(1) NOT NULL,\
|
||||
switch3 TINYINT(1) NOT NULL,\
|
||||
switch4 TINYINT(1) NOT NULL,\
|
||||
is4G TINYINT(1) NOT NULL,\
|
||||
device TINYTEXT DEFAULT NULL\
|
||||
)",(err)=>{;
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
// db.all("CREATE TABLE IF NOT EXISTS remoteswitch1 (\
|
||||
// `AUTOID` INTEGER PRIMARY KEY AUTOINCREMENT,\
|
||||
// `id` tinytext NOT NULL,\
|
||||
// `type` tinytext NOT NULL,\
|
||||
// `tips` text NOT NULL,\
|
||||
// `other` text NOT NULL,\
|
||||
// `switch1` tinyint(1) NOT NULL,\
|
||||
// `switch2` tinyint(1) NOT NULL,\
|
||||
// `switch3` tinyint(1) NOT NULL,\
|
||||
// `switch4` tinyint(1) NOT NULL,\
|
||||
// `is4G` tinyint(1) NOT NULL,\
|
||||
// `device` tinytext DEFAULT NULL)",(err)=>{console.log(err);
|
||||
// })
|
||||
|
||||
|
||||
// let init=async function()
|
||||
// {
|
||||
// db.run("CREATE TABLE IF NOT EXISTS remoteswitch (\
|
||||
// `AUTOID` INTEGER PRIMARY KEY AUTOINCREMENT,\
|
||||
// `id` tinytext NOT NULL,\
|
||||
// `type` tinytext NOT NULL,\
|
||||
// `tips` text NOT NULL,\
|
||||
// `other` text NOT NULL,\
|
||||
// `switch1` tinyint(1) NOT NULL,\
|
||||
// `switch2` tinyint(1) NOT NULL,\
|
||||
// `switch3` tinyint(1) NOT NULL,\
|
||||
// `switch4` tinyint(1) NOT NULL,\
|
||||
// `is4G` tinyint(1) NOT NULL,\
|
||||
// `device` tinytext DEFAULT NULL)",(err)=>{console.log(err);
|
||||
// console.log("remoteswitch table created")})
|
||||
|
||||
// }
|
||||
|
||||
|
||||
// let queryacy = function( sql, values ) {
|
||||
// // 返回一个 Promise
|
||||
// return new Promise(( resolve, reject ) => {
|
||||
// db.getConnection(function(err, connection) {
|
||||
// if (err) {
|
||||
// reject( err )
|
||||
// } else {
|
||||
// connection.query(sql, values, ( err, rows) => {
|
||||
|
||||
// if ( err ) {
|
||||
// reject( err )
|
||||
// } else {
|
||||
// resolve( rows )
|
||||
// }
|
||||
// // 结束会话
|
||||
// //console.log(sql);
|
||||
// connection.release()
|
||||
// })
|
||||
// }
|
||||
// })
|
||||
// })
|
||||
// }
|
||||
|
||||
|
||||
// db.resetpath=resetpath
|
||||
// db.query=db.all
|
||||
db.queryacy= queryacy
|
||||
// db.init=init
|
||||
// db.ConvertoKeyValue=ConvertoKeyValue
|
||||
// db.ConvertoKeyEQValue=ConvertoKeyEQValue
|
||||
module.exports = db
|
Reference in New Issue
Block a user