Home >Web Front-end >JS Tutorial >How to implement node connection to mysql

How to implement node connection to mysql

php中世界最好的语言
php中世界最好的语言Original
2018-03-13 18:19:101997browse

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;

Display database

show databases;

Create database, for example:

create database database_test
create database [database];

Use the database, for example:

use database_test
use [database];

Delete the database, for example:

drop database database_test
drop database [database];

Create a table, for example:

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  (&#39;vist&#39;,24)
insert into <table>(<field>) values (<value>);

Query data, for example:

select * from table_test
select * from <table>;

Modify data, for example:

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

Delete data, for example:

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

Need to install the mysql package

npm install mysql

node

Connect to the database

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

Configure user password

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

Connect to the database

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

Use the database

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

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);
      })
  }
  });

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);
    }
  });

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);
  }
  });

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);
  }
  })

Close database

client.end();

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!

Statement:
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