-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const express = require('express')
const mysql = require('mysql')
const app = express();
//create and verify the link of database
const db = mysql.createConnection({
host: 'localhost',
user: "root",
password: "123456",
database: "node_js"
})
//connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log("Database Connected")
})
//create table
app.get("/createtable", (req, res) => {
let sql = 'CREATE TABLE post2(id int AUTO_INCREMENT, title VARCHAR(255), body VARCHAR(255), PRIMARY KEY (id))';
db.query(sql, (err, result) => {
if (err) {
throw err;
}
console.log(result)
res.send('posts table is created...')
})
})
//add data in table
app.get("/up", (req, res) => {
let post = { title: 'vijay1', body: 'vijay text' }
let sql = 'INSERT INTO posts SET ?';
let query = db.query(sql, post, (err, result) => {
if (err) {
throw err;
}
console.log(result)
res.send("data added to table");
})
})
//update table
app.get('/updatetable', (req, res) => {
// let colname = {'title' 'username' VARCHAR(255)}
let sql = "ALTER TABLE post2 CHANGE 'title' TO 'username' VARCHAR(255)"
db.query(sql, (err, result) => {
if (err) {
throw err;
}
console.log(result);
res.send("table column is updated");
})
})
//server get listen on port address
app.listen('3000', () => {
console.log("Server Started on 3000")
})