Nodejs MySQL operation

Source: Internet
Author: User

WAMP Create a database

Select phpMyAdmin

Select Users, add Users

Fill in the database details, complete the Select the bottom right corner of the "execute"

User added success

2. Nodejs install MySQL Driver

NPM install MySQL

3 Database Operations Curd

Connecting to a database

Index.js:

var mysql      = require (' mysql '); var connection = Mysql.createconnection ({    host     : ' localhost ',    user     : ' Test ',    password: ' 123456 ',    database: ' Test '}); Connection.connect (); Connection.query (' SELECT 1 + 1 As solution ', function (Error, results, fields) {    if (error) throw error;    Console.log (' The solution is: ', results[0].solution);});

After running, the output results:

Enquiry

The new table account is used for testing:

Index.js:

var mysql      = require (' mysql '); var connection = Mysql.createconnection ({    host     : ' localhost ',    user     : ' Test ',    password: ' 123456 ',    database: ' Test '}); Connection.connect (); var  sql = ' SELECT * from account '; Connection.query (Sql,function (err, result) {    if (err) {        console.log (' [SELECT ERROR]-', err.message);        return;    }    Console.log ('--------------------------SELECT----------------------------');    Console.log (result);    Console.log (Result[0].id, Result[0].name, result[0].age);    Console.log ('------------------------------------------------------------\ n ');}); Connection.end ();

  

Output Result:

Inserting data

Index.js:

var mysql      = require (' mysql '); var connection = Mysql.createconnection ({    host     : ' localhost ',    user     : ' Test ',    password: ' 123456 ',    database: ' Test '}); Connection.connect (); var  addsql = ' INSERT into account ( Id,name,age) VALUES (3,?,?) ';  Insert data var  = [' Baby ', addsqlparams];                                 Fill in the question mark data//Add Connection.query (addsql,addsqlparams,function (err, result) {    if (err) {        console.log (' [INSERT ERROR]-', err.message);        return;    }    Console.log ('--------------------------INSERT----------------------------');    Console.log (' INSERT ID: ', result.insertid);    Console.log (' INSERT ID: ', result);   Insert result    console.log ('-----------------------------------------------------------------\ n ');}); Connection.end ();

Running the results, the database adds a single piece of data:

Update data

var mysql      = require (' mysql '); var connection = Mysql.createconnection ({    host     : ' localhost ',    user     : ' Test ',    password: ' 123456 ',    database: ' Test '}); Connection.connect (); var modsql = ' UPDATE account SET name =?, Age =? WHERE id =? '; var modsqlparams = [' Lee ', 31, 1];//change connection.query (err, result) {    if (err) {        Console.log (' [UPDATE ERROR]-', err.message);        return;    }    Console.log ('--------------------------UPDATE----------------------------');    Console.log (' UPDATE affectedrows ', result.affectedrows);    Console.log ('-----------------------------------------------------------------\ n ');}); Connection.end ();

Operation Result:

Delete data

var mysql      = require (' mysql '); var connection = Mysql.createconnection ({    host     : ' localhost ',    user     : ' Test ',    password: ' 123456 ',    database: ' Test '}); Connection.connect (); var delsql = ' DELETE from account where ID =3 ';//delete Connection.query (delsql,function (err, result) {    if (err) {        console.log (' [DELETE ERROR]-', err.message );        return;    }    Console.log ('--------------------------DELETE----------------------------');    Console.log (' DELETE affectedrows ', result.affectedrows);    Console.log ('-----------------------------------------------------------------\ n ');}); Connection.end ();

Run the result, id=3 data is deleted:

  

Nodejs MySQL operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.