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