Get the App
SLTechnology News&Howtos  ›  Development  › 

How Javascript connects database queries and inserts data

Shulou Source: shulou.com Published: 2022-06-01 07:15:11 10月12日 Update

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!

Tags: Data database file code query terminal input folder article run university compile success data table is in statement preparation test generation value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS MySQL OPPO Reno Xiaomi