Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Javascript connects database queries and inserts data

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces Javascript how to connect the database query and insert data, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

First, prepare the database

Download MySQL and edit the code with the help of database graphics tools or on cmd

-- create a database CREATE DATABASE IF NOT EXISTS SchoolTownDB;-- and use the database USE SchoolTownDB;-- to add data tables CREATE TABLE school (id INT, NAME VARCHAR (10), X DOUBLE, Y DOUBLE);-- add data INSERT INTO school VALUE (1jue 'Guangzhou University', NULL,NULL); INSERT INTO school VALUE ('Sun Yat-sen University', NULL,NULL);-- display data tables SELECT * FROM school

Download node:

Download from the official website and click next to install by default.

Prepare a folder:

Create a new folder and place the js file in it later. Name it Demo.

Copy the node_modules folder in the node compiled file to the Demo folder, and it will play a role in connecting to the database.

2. Compile package.json file in Vscode

Create a new terminal in the menu bar

Enter the following code on the terminal, and then enter .npm init

Then enter until it appears (yes), then type yes in the terminal, and then enter enter.

So a json file package.json is generated.

Then continue to enter npm install mysql

Run the compilation to generate the package-lock.json file

At this point, the package.json file is roughly as follows:

Third, realize the connection of database with Javascript code.

Create an index.js file and edit the code in it:

/ / introduce mysql module let mysql = require ("mysql"); / / create connection object let connection = mysql.createConnection ({host: "localhost", / / connect local computer port:3306,// port user: "root", / / database account password: "qq124519", / / password database: "SchoolTownDB" / / connected database name}) / / call the connect method to create a connection connection.connect ((err) = > {/ / callback function). If an error is reported, the err will be populated with if (err) {console.error ("connection failure" + err.stack); / / print stack information return;} console.log ("connection successful");}); / / close database connection connection.end ()

Test code, enter at the terminal

Node.\ index.js

Enter and run

Query and insert data

Query for SQL statement:

Query connection.query of / / SQL statement ("SELECT * FROM school", (err, result) = > {if (err) {console.error ("query failure" + err.message); result;} console.log (result);})

The running code is also typed node.\ index.js at the terminal.

Insert data for the database:

Let sql = "insert into school values (?)"; / /? Is the placeholder / / setting parameter let params= [3, "South China University of Technology", null,null]; connection.query (sql,params, (err,result) = > {if (err) {console.error ("insert failure" + err.message); result;} console.log ("insert successful");})

The running code is also typed node.\ index.js at the terminal.

The test results are as follows:

Thank you for reading this article carefully. I hope the article "how to connect database queries and insert data in Javascript" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report