This time I will bring you how to connect node to mysql. What are the precautions to connect node to mysql? The following is a practical case, let’s take a look.
Mysql basic commandsChange root password 123 to 1234mysqladmin -u root -p 123 password 1234;
show databases;
Create database, for example:
create database database_test create database [database];
use database_test use [database];
drop database database_test drop database [database];
create table_test(name char(10) not null,age int(4)) create table <table>(<field><type>);
Insert data, For example:
insert into table_test (name,age) values ('vist',24) insert into <table>(<field>) values (<value>);
select * from table_test select * from <table>;
update table_test set age=18 where name='vist' update <table> set <fidld>=<value> where <if>;
delete from table_test where name='vist' delete from <table> where <if>; node配置
npm install mysql
var mysql=require('mysql');var database='database_test';var table='table_test';
var client=mysql.createConnection({ user: 'root', passowrd: '1234' });
client.connect();console.log('连接数据库');
client.query('use '+ database);
client.query('select * from '+ table , function(err, results, fields){ if(err){ throw err; } if(results){ console.log('查询'); results.map(function(item){ console.log('%s\t%d', item.name, item.age); }) } });
client.query('insert into '+ table + '(name, age) values (?, ?)', ['bestvist',20], function(err, results, fields){ if(err){ throw err; } if(results){ console.log('插入'); console.log(results); } });
client.query('update '+ table + ' set age=? where name=?',[18, 'bestvist'],function(err, results, fields){ if(err){ throw err; } if(results){ console.log('更新'); console.log(results); } });
client.query('delete from '+ table +' where name=?', ['bestvist'], function(err, results, fields){ if(err){ throw err; } if(results){ console.log('删除'); console.log(results); } })
client.end();
Use JS to implement sorting algorithm
JS regular expressionHow to use
The above is the detailed content of How to implement node connection to mysql. For more information, please follow other related articles on the PHP Chinese website!