mysql从一个数据库向另一个数据库插数据

var mysqlr = require('mysql');
var readConnection = mysqlr.createConnection({//创建第一个连接
    host: '112.74.82.181',
    user: 'root', 
    password: 'root.',
    database: 'ce',
    dateStrings: true,
    charset: 'utf8mb4',
    pagesize: 30,
    connectionLimit: 10,
    reuseDB: true
});
readConnection.connect();

var mysqlw = require('mysql');
var writeConnection = mysqlw.createConnection({//创建第二个连接
    host: '112.73.65.191',
    user: 'root',    
    password: 'root',  
    database: 'ce2',  
    dateStrings: true,
    charset: 'utf8mb4',
    pagesize: 30,
    connectionLimit: 10,
    reuseDB: true
});
writeConnection.connect();

//查
var oldUserLoginInfo = `select * from table_a`;

//增
var writeOldUserInfo = "INSERT INTO table_a (p_name,price,imgs,p_asin,p_name_en) VALUES (?,?,?,?,?)";

//执行查
readConnection.query(oldUserLoginInfo,function(err,rows,fields){
    if (err) throw err;
    
    for(var item of rows){//处理查结果
        var addItem = [item.goods_name,item.goods_price,(!item.image) ? '' : `https://aa/piao/public/upload/${item.image}`,item.number,item.goods_name];
        //执行增
        writeConnection.query(writeOldUserInfo,addItem,function(err,result){
            if(err)
            console.log(err);
            console.log('插入数据======================>');
        });
    }

    writeConnection.end();//关闭连接2
    readConnection.end();//关闭连接1
});

 

上一篇:Promise的理解


下一篇:NamespaceController