Home > Web Front-end > JS Tutorial > body text

How to implement node connection to mysql

php中世界最好的语言
Release: 2018-03-13 18:19:10
Original
1937 people have browsed it

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 commands

Change root password 123 to 1234

mysqladmin -u root -p 123 password 1234;
Copy after login

Display database

show databases;
Copy after login

Create database, for example:

create database database_test
create database [database];
Copy after login

Use the database, for example:

use database_test
use [database];
Copy after login

Delete the database, for example:

drop database database_test
drop database [database];
Copy after login

Create a table, for example:

create table_test(name char(10) not  null,age int(4))
create table <table>(<field><type>);
Copy after login

Insert data, For example:

insert into table_test (name,age) values  (&#39;vist&#39;,24)
insert into <table>(<field>) values (<value>);
Copy after login

Query data, for example:

select * from table_test
select * from <table>;
Copy after login

Modify data, for example:

update table_test set age=18 where  name=&#39;vist&#39;
update <table> set <fidld>=<value> where <if>;
Copy after login

Delete data, for example:

delete from table_test where name=&#39;vist&#39;
delete from <table> where <if>;
node配置
Copy after login

Need to install the mysql package

npm install mysql
Copy after login

node

Connect to the database

var mysql=require(&#39;mysql&#39;);var database=&#39;database_test&#39;;var table=&#39;table_test&#39;;
Copy after login

Configure user password

var client=mysql.createConnection({  user: &#39;root&#39;,  passowrd: &#39;1234&#39;
  });
Copy after login

Connect to the database

client.connect();console.log(&#39;连接数据库&#39;);
Copy after login

Use the database

client.query(&#39;use &#39;+ database);
Copy after login

Query operation

client.query(&#39;select * from &#39;+ table , function(err, results, fields){  if(err){    throw err;
  }  if(results){    console.log(&#39;查询&#39;);
    results.map(function(item){      console.log(&#39;%s\t%d&#39;, item.name, item.age);
      })
  }
  });
Copy after login

Insertion operation

client.query(&#39;insert into &#39;+ table + &#39;(name, age) values (?, ?)&#39;, [&#39;bestvist&#39;,20], function(err, results, fields){    if(err){      throw err;
    }    if(results){      console.log(&#39;插入&#39;);      console.log(results);
    }
  });
Copy after login

Update operation

client.query(&#39;update &#39;+ table + &#39; set age=? where name=?&#39;,[18, &#39;bestvist&#39;],function(err, results, fields){  if(err){    throw err;
  }  if(results){    console.log(&#39;更新&#39;);    console.log(results);
  }
  });
Copy after login

Delete operation

client.query(&#39;delete from &#39;+ table +&#39; where name=?&#39;, [&#39;bestvist&#39;], function(err, results, fields){  if(err){    throw err;
  }  if(results){    console.log(&#39;删除&#39;);    console.log(results);
  }
  })
Copy after login

Close database

client.end();
Copy after login

I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!