Nodejs Operation MySQL-Adding and deleting changes

Source: Internet
Author: User

Install the NPM module project first
npm init
Install MySQL
--save
Nodejs Connection MSYQL
//import MySQLConstMysql= require(' MySQL ');//Connect MySQLConstConnection= MySQL.createconnection({    Host: ' 127.0.0.1 ',    User: ' Root ',    Password: ' Password ',    Port: ' 3306 ',    Database: ' Test '});Connection.Connect();//End ConnectionConnection.End();
Increase
//Introduce MySQLConstMysql= require(' MySQL ');//Connection MYQLConstConnection= MySQL.createconnection({    Host: ' 127.0.0.1 ',    User: ' Root ',    Password: ' Password ',    Port: ' 3306 ',    Database: ' Test ',});Connection.Connect();//INSERT statement LetAddsql= "INSERT into article (title, author, date) VALUES (?,?, now ())"; LetAddsqlparams=[' Today is Noce ', ' Bob '];//Execute INSERT statementConnection.Query(Addsql,Addsqlparams,(Err,Result= {    if(ERR){        ThrowErr;    }    //Insert Successful output    Console.Log(' Insert Success ');    Console.Log(Result);});//Disconnect MSYQLConnection.End();
By deleting
//Introduce MySQLConstMysql= require(' MySQL ');//Connect MySQLConstConnection= MySQL.createconnection({    Host: ' 127.0.0.1 ',    User: ' Root ',    Password: ' Password ',    Port: ' 3306 ',    Database: ' Test '});Connection.Connect();//DELETE statement LetSql= "Delete from article where id = ten";//Execute DELETE statementConnection.Query(SQL,(Err,Data= {    if(ERR){        ThrowErr;    }    //Successful execution    Console.Log(' Delete success! ');    Console.Log(data);});//Disconnect MSYQLConnection.End();
Change
//import MySQLConstMysql= require(' MySQL ');//Connect MySQLConstConnection= MySQL.createconnection({    Host: ' 127.0.0.1 ',    User: ' Root ',    Password: ' Password ',    Port: ' 3306 ',    Database: ' Test '});Connection.Connect();//UPDATE statement LetModsql= "Update article set title =?, Author =?" Where id like? "; LetModsqlparams=[' Learn Nodejs tonight ', ' a wave of Wanbo ',  A];//Execute UPDATE statementConnection.Query(Modsql,Modsqlparams,(Err,Data= {    if(ERR){        ThrowErr;    }    Console.Log(' upload success! ');    Console.Log(data)});Connection.End();
Check
//import MySQLConstMysql= require(' MySQL ');//Connect MySQLConstConnection= MySQL.createconnection({    Host: ' 127.0.0.1 ',    User: ' Root ',    Password: ' Password ',    Port: ' 3306 ',    Database: ' Test ',});Connection.Connect();//Query Statements LetSql= ' SELECT * from article ';//Execute Query statementConnection.Query(SQL,(Err,Data= {    if(ERR){        Console.Log(' [SELECT ERROR]-', Err.message);        return;    }    //Query Success    Console.Log(data);});Connection.End();

Nodejs Operation MySQL-Adding and deleting changes

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.