简体   繁体   中英

How to add parameter in nodejs with mysql

I am studing nodejs with koa2, and now I want to read & write & delete & update informations in mysql database, I created a file called dbconfig.js for configure the database informations, and another file called mysql.js as the book said like following

    const mysql = require('mysql')
    const config = require('./dbconfig')
    var pool  = mysql.createPool({
        host     : config.database.HOST,
        user     : config.database.USERNAME,
        password : config.database.PASSWORD,
        database : config.database.DATABASE,
        port      : config.database.PORT
    });
    let query = function( sql, values ) {
        return new Promise(( resolve, reject ) => {
            pool.getConnection(function(err, connection) {
                if (err) {
                    resolve( err )
                } else {
                    connection.query(sql, values, ( err, rows) => {
                        if ( err ) {
                            reject( err )
                        } else {
                            resolve( rows )
                        }
                        connection.release()
                    })
                }
            })
        })
    }
    let findAUser = function () {
        let _sql = `
            SELECT * FROM user where id = 
        `
        return query(_sql)
    }

    let deleteAUser = function () {
        let _sql = `
            DELETE FROM user where id = 
        `
        return query(_sql)
    }

    let updateAUser = function () {
        let _sql = `
            UPDATE user set name =  where id = 
        `
        return query(_sql)
    }

    let addAUser = function () {
        let _sql = `
            insert into user () value ()
        `
        return query(_sql)
    }

    module.exports={
        findAUser,
        deleteAUser,
        updateAUser,
        addAUser
    }

In app.js , I call them like this const userModel = require('./mysql');userModel.findAUser() .

But I have no idea to write findAUser , deleteAUser , updateAUser , addAUser , can you help me? Many thanks.

Maybe you should take a look to mysql-json module, which really simplifies basic requests like insert, delete, update, etc...

You also have some exemples to learn how to use.

Hope it helps.

Two things here:

  • In your query function, I would put the connection.release() above the reject/resolve. Otherwise it will probably not being called.
  • In your specific SQL functions you need to provide the parameters as an array of parameters. See example for the findAUser function:

So it could look like this:

let query = function( sql, values ) {
    return new Promise(( resolve, reject ) => {
        pool.getConnection(function(err, connection) {
            if (err) {
                resolve( err )
            } else {
                connection.query(sql, values, ( err, rows) => {
                    connection.release()
                    if ( err ) {
                        reject( err )
                    } else {
                        resolve( rows )
                    }
                })
            }
        })
    })
}
let findAUser = function (id) {
    let _sql = `
        SELECT * FROM user where id = ?
    `
    return query(_sql, [id])
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM