-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAzureMySQLConnection.js
65 lines (59 loc) · 2.03 KB
/
AzureMySQLConnection.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
65
const mysql = require('mysql');
function mysqlDB(){
var config =
{
host: '*.mysql.database.azure.com',
user: ' user name',
password: 'user password ',
database: 'database name',
port: 3306,
ssl: false
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else
{
console.log("Connection established.");
queryDatabase();
}
});
function queryDatabase(){
//drop table if exists
conn.query('DROP TABLE IF EXISTS inventory;', function (err, results, fields) {
if (err) throw err;
console.log('Dropped inventory table if existed.');
})
//create new table
conn.query('CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);',
function (err, results, fields) {
if (err) throw err;
console.log('Created inventory table.');
})
//insert data into table
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['Mobile', 150],
function (err, results, fields) {
if (err) throw err;
else console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['Desktop', 154],
function (err, results, fields) {
if (err) throw err;
console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.query('INSERT INTO inventory (name, quantity) VALUES (?, ?);', ['Laptop', 100],
function (err, results, fields) {
if (err) throw err;
console.log('Inserted ' + results.affectedRows + ' row(s).');
})
conn.end(function (err) {
if (err) throw err;
else console.log('Done.')
});
};
};
mysqlDB();