AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js MySQL Delete


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js MySQL Delete

Node.js Delete Query helps you to delete one or more rows from a table.

ORDER BY is used to sorts the records in ascending order by default. Sorting records in descending order, you have to use the DESC keyword.

Syntax:

SELECT FROM table_name(s) WHERE column_name = value

Once installed this Node.js module can be used to manipulate the MySQL database:

var mysql = require('mysql');

 

MySQL Delete Records

Delete name from the table Users where name is Chris.

Example: DELETE records
var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
if (err) throw err;
var sql = "DELETE FROM Users WHERE name = 'Chris'";
conn.query(sql, function (err, result) {
if (err) throw err;
console.log("Record deleted: " + result.affectedRows);
});
});

Save the code above in a file named "demo_test_db_delete.js", and run the file:

E:\Users\Your_Name>node demo_test_db_delete.js

After run the file will give you this result:

[
{ id: 3, name: 'Amy', address: 'Apple st 652'},
{ id: 2, name: 'Chris', address: 'Avenida 38'}, This row is DELETE
{ id: 6, name: 'Jack', address: 'Avenida 38'},
{ id: 4, name: 'Jony', address: 'Avenida 38'},
{ id: 1, name: 'Maria', address: 'Avenida 38'},
{ id: 5, name: 'Micha', address: 'Avenida 38'}
]

 

Related subjects:
node.js create database nodejs create table node.js MySQL select from Order By

 

Tags: Node.js MySQL Delete multiple rows, example,
node js mysql delete multiple rows
node js delete file
node js delete request

 

MySQL DELETE Row Dynamic Query

MySQL DELETE Row Dynamic Query name column in table Users.

Example:

var mysql = require('mysql');

var conn = mysql.createConnection({
host: "localhost",
user: "your_username",
password: "your_password",
database: "my_db_name"
});

conn.connect(function(err) {
if (err) throw {
console.log("Database Connection Failed !!!", err);
return;
}
console.log("We are connected to my_db_name");
// notice the ? in below query
var sql = "DELETE FROM Users WHERE name = ?";
// Dynamic Value
let name = 'Chris';
// Notice the name variable below
// It will replace the ? in query
conn.query(query, name, (err, rows) => {
if(err) throw err;

console.log('Row deleted = ' + rows.affectedRows);
});
});

Save the code above in a file called "demo_test_db_delete2.js", and run the file:

Example: : Run "demo_test_db_delete2.js"
E:\Users\Your_Name>node demo_test_db_sort2.js

After run the file will give you this result:

[
{ id: 5, name: 'Micha', address: 'Avenida 38'}
{ id: 1, name: 'Maria', address: 'Avenida 38'},
{ id: 4, name: 'Jony', address: 'Avenida 38'},
{ id: 6, name: 'Jack', address: 'Avenida 38'},
{ id: 2, name: 'Chris', address: 'Avenida 38'}, Row DELETED
{ id: 3, name: 'Amy', address: 'Apple st 652'},
]

 

Result Object

When execute query, the result object returned.

{ fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 34,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}

 



Node.js MySQL Delete multiple rows, example,
node js mysql delete multiple rows
node js delete file
node js delete request
Node.js MySQL Delete - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


CSS background color - opacity, gradient...
CSS flex - display: flex - CSS Flexbox L...
CSS border padding...
HTML text center horizontally and vertic...
HTML text input <input type="text"&g...
HTML link color...
How to Link CSS to HTML, external file n...
HTML link - hyperlink...
jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...