Simple MySQL operations (1): mysql operations
I recently started to learn mysql. It takes a long time to clarify my ideas with my head because I have learned a little complicated ....
Now, I just thought about it. I 'd like to write it down for your reference. Actually, it's only me ~~~
I am using the express module in nodeJS. I suggest you download it from the official website when installing mysql;
Create a folder named test first.
Create a js file in the folder and name it express. js.
1) installation: Open the command window in the current folder and enter: npm install express to install the express module.
2) Use: Open express. js and enter:
Var express = require ('express '); var app = express (); // process get/post/put/delete requests: app. get/post/put/delete ('/', function (req, res) {// process get/post/put/delete requests}); app. get ('/login.html', function (req, res) {// process get requests
Fs.readFile('login.html ', function (err, data ){
If (err ){
Res. end ("404 page ...");
} Else {
Res. setHeader ('content-type', 'text/html ');
Res. end (data );
}
});
}); App. post ('/user', function (req, res) {// process post requests });
When you jump to the 'login.html 'page, if there are referenced server resources (images, css, etc.), you will find that these resources are not loaded... This requires
Server File loading:
app.use(express.static('public'));
Create a new public folder in the test folder and put all the images, css, and js to be loaded by the server into the public folder so that the server resources can be loaded.
Note: although it is stored in the public folder, the path remains the same
Html file:
// Html file: // assets/css/style.css <link href = "css/style.css"> // public/images/1.jpg< img src = "images/1.jpg">
Start the mysql operation as follows:
1) installation: Download from mysql.com
2) Use: ① open the workbench graphical interface to view the mysql running status:
You can see that the database is running normally and you can proceed.
② Create a database
Click the image
Enter the Database Name (not Chinese) → click apply at the bottom → click apply → the database is created successfully
Create a table
Right-click tables: create table → name of the input column → click apply → click finish
Table created
③ Add, delete, modify, and query Databases
For example, right-click and Select rows to open the sign table.
After opening the image below, you can directly add/delete data in the table (after operating the data, remember to click apply to save the operation), or enter the command to operate on the sign
If you forget the command, there is a small method: to operate the table below (for example, I added a row of data: id = 1, name = javencs, password = 123), click apply, A Selection box is displayed:
The statement in it is the operation that I just added data, = copy this statement to the code box, and save it with apply to achieve the same effect;
Add data:
INTO `hshs`.`sign` (`id`, `name`, `password`) VALUES ('1', 'javencs', '123');
Delete data:
DELETE FROM `hshs`.`sign` WHERE `id`='1';
Search data:
SELECT * FROM hshs. sign; // query all data SELECT * FROM hshs. sign WHERE id = 1; // condition Query
Modify data:
UPDATE 'hsps '. 'sign' SET 'Password' = '000000' WHERE 'id' = '1'; // modify some data to UPDATE 'hsps '. 'sign' SET 'id' = '3', 'name' = 'jack', 'Password' = '000000' WHERE 'id' = '1 '; // modify a data record