简体   繁体   中英

Node.js MySQL Delete Query

I'm trying to create a delete function in nodejs from MySQL database so I put this

update

dataToDB('TesT','1')
getDataFromDB('1')

function getDataFromDB(version){
  let select_query = `SELECT * from files where version = '${version}' ORDER by File_ID desc LIMIT 1`;
  modifieDB(version)
  connection.query(select_query,(err, result) => {
      if (err){
          throw(err)
      }else{
          console.log(result)
      }
  })
}

function dataToDB(File_Name,version){
  let insert_query = `INSERT INTO files(File_Name, version) VALUES ('${File_Name}','${version}')`;
  connection.query(insert_query,(err, result) => {
      if (err){
          throw(err)
      }else{
          console.log(result)
      }
  })
}

function modifieDB(version){
    let select_query = `DELETE FROM files WHERE version != ${version}`;
    connection.query(select_query,(err, result) => {
        if (err){
            throw(err)
        }else{
            console.log(result)
        }
    })
  }

but it's didn't work and give this error:

Error: Error: Truncated incorrect DOUBLE value: 'undefined'

Any Idea?

Firstly, as others already pointed out (this is important):

Please use parametrized queries, they make your life easier and keep you safe of SQL Injections .

Then, back to your question:

Given that you aren't using parametrized queries here, I'd guess that you are missing the ' around ${version} here:

DELETE FROM files WHERE version != ${version}

...which means whatever you enter as version won't be treated as a string, but as part of the query, which can cause all kinds of errors.

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