Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

nodejs 数据查询插入

情形:查询数据是否存在数据库,不存在则插入.

我的方法:通过循环查询,如果查询不存在则插入该条数据。

想要的结果:想要查询后把需要插入的数据存放在一个数组中,查询结束后再一次性插入不存在的数据。

遇到的问题:
1.查询结果是异步返回,导致数组undefined
2.部分代码如下:
insert.js

let connection = require('./dbconect');
function insert(addSql, addSqlParams) {
    let promise = new Promise(function (resolve, reject) {
        connection.query(addSql, addSqlParams, function (err, result) {
            if (err) {
   console.log('[INSERT ERROR] - ', err.message);
                return;
            }
            resolve(result);
            // connection.end();

        });
    });
    return promise;
}
module.exports = {
    insert,
    connection
};

query.js

let connection = require('./dbconect');
function query(sql, sql_params) {
    let promise = new Promise(function (resolve, reject) {
        connection.query(sql, sql_params, (err, result) => {
            if (err) {
   console.log('[SELECT ERROR] -', err.message);
                return;
            }
            resolve(result);
        });
    });
    return promise;
}
module.exports = {
    query,
    connection
}

index.js

const select = require('./db/query');
const add = require('./db/insert');

   // 存放查询数据(promise)
    let select_result, insert_result, send_mail_data = [];
    
 // 插入sql语句
    let add_sql = "insert into detail (id,pub_date,title,url,insert_date) values ? ";

    function selectInsertDB(news_data) {
        let mail_content, subject;
        // 循环取到的数据
        console.log('news_data的数据是????', news_data);
        // 存放插入数据
        let add_sql_params, add_sql_params_temp = [];
        let add_sql_params_promise;
        for (let i = 0; i < news_data.length; i++) {

            // 查询mysql语句 及 所需条件
            let select_sql = "select * from detail where pub_date= ? and title = ?";
            let select_sql_params = [news_data[i].pub_date, news_data[i].title];
            try {
                select_result = select.query(select_sql, select_sql_params);
                select_result.then(function (result) {
                    console.log('查询数据:', select_sql_params, '查询结果', result)
                    console.log('判断指标' + !result.length);
                    if (!result.length) {
                        // 数据库无此数据则将数据放入待插入变量
                        for (item in news_data[i]) {
                            add_sql_params_temp.push(news_data[i][item]);
                        }
                        add_sql_params_temp.push(showDateTime());
                        add_sql_params.push(add_sql_params_temp);
                        console.log('需要插入的记录为:', add_sql_params);
                    } else {
                        console.log('数据已存在', result);

                    }
                    if (i == news_data.length - 1) {
                        add_sql_params_promise = new Promise(function (resolve) {
                            resolve(add_sql_params);
                        })
                    }
                    // })
                }, function (err) {
                    console.log('查询失败', err)
                })
            } catch (e) {
                console.log('查询语句有问题??');
            }

        }
        console.log('需要插入的数据记录', add_sql_params);

        add_sql_params_promise.then(function () {
            if (!add_sql_params.length) {
                // 没有数据插入         
       
                // 发送邮件
                // sendMail(nodemailer, mailOptions);

            } else {
                // 有数据插入
               
                        // 发送数据
                        // sendMail(nodemailer, mailOptions);

                    }, function (err) {
                        console.log('插入失败', err)
                    })
                } catch (e) {
                    console.log('插入数据出错', e);
                }
            }
        })


    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

嗯?为什么不设置一个主键,然后疯狂插入呢?

因为有主键,所以数据不会重复


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...