Node js MySQL delete data

DELETE Command is used to delete records from MySql table. Once the data is deleted, you can not recover them. So always be careful while deleting the existing table.

Syntax

DELETE FROM Tablename [WHERE Clause];

In the given example, we have deleted record of id #2.

Example

var mysql = require('mysql');
var con = mysql.createConnection({
    host: "hostname",
    user: "username",
    password: "password",
    database: "database"
});
con.connect(function(error){
    if(error) {
        console.log('Error establishing connection to db');
        return;
    }
    console.log('Connection Established'); 
    var deletedata = "DELETE from department WHERE id = ?";
    con.query(deletedata, [2], function (error) {  
    if(error) { 
        throw(error);
    }  
    console.log("Data Deleted");  
    });  
});

Open cmd and run the above file as -

nodejs mysql delete data

Read more articles


General Knowledge



Learn Popular Language