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