-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql-db.js
47 lines (43 loc) · 1.25 KB
/
mysql-db.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
const geheim = require('./geheim/geheim.json');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: geheim.mysql_connection,
user: geheim.mysql_username,
password: geheim.mysql_password,
database : geheim.mysql_database
});
connection.connect((error) => {
if(error){
console.log('Error connecting to the MySQL Database');
return;
}
console.log('Connection established sucessfully');
});
var getAllUrls = function(callback) {
connection.query(`SELECT * FROM urls`, (err, result) =>{
if(err){
callback(err, null)
}
callback(null, result);
});
};
var insertUrl = function(id, url, callback) {
connection.query(`INSERT INTO urls (id, url) VALUES ('${id}', '${url}');`, (err, result) =>{
console.log(id +" : "+url);
if(err){
callback(err, null)
}
callback(null, result);
});
};
var getURL = function (url, callback) {
connection.query(`SELECT * FROM urls WHERE id = '${url}'`, (err, result) =>{
if(err){
callback(err, null)
}
callback(null, result);
});
};
module.exports.getAllUrls = getAllUrls;
module.exports.insertUrl = insertUrl;
module.exports.getURL = getURL;