node 2个mysql 数据库之间迁移数据
console.log(new Date());
function getdata(){
var p = new Promise((resolve,reject) => {
const mysql = require('mysql')
// 数据库配置对象---from
const databaseConfig = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
port: '3306',
database: 'qqs'
})
databaseConfig.connect() // 开始连接
// 执行 sql 语句
const sql = `select TicketNo from ticket limit 30;`
databaseConfig.query(sql, (err, result) => {
if (err) {
console.error(err)
return
}
//console.log(result)
if(result.length == 0) return console.log('----无需处理数据');
//setTimeout(()=>{
resolve(result);
databaseConfig.end() // 关闭连接
//},500)
})
})
return p;
}
function dealdata(data){
var p = new Promise((resolve,reject) => {
const mysql = require('mysql')
const to_databaseConfig = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
port: '3306',
database: 'queuesystem'
})
to_databaseConfig.connect() // 开始连接
data.forEach((item,key) => {
let insert_sql = `insert into base_test(name,type) values ('${item.TicketNo}','mysql');`;
console.log(insert_sql);
to_databaseConfig.query(insert_sql,(insert_err,insert_res) => {
if(insert_err){
console.log(insert_err);
//return;
} else {
console.log(key,'----insert-ok');
}
})
})
setTimeout(()=>{
resolve(to_databaseConfig);
},200)
})
return p;
}
getdata()
.then(function(data){
return dealdata(data);
})
.then(function(to_databaseConfig){
to_databaseConfig.end() // 关闭连接
console.log('----关闭数据库queuesystem---');
})
相信坚持的力量,日复一日的习惯.