-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
44 lines (36 loc) · 1.08 KB
/
database.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
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'movie_data'
});
connection.connect();
function addMovies(movieArray, callback) {
//use insert as q
var q = 'INSERT INTO movie_list (Title, Year,Description,Rating) VALUES (?,?,?,?)'
for(var i = 0; i < movieArray.length; i++) {
connection.query(q, [movieArray[i].title, movieArray[i].release_date, movieArray[i].overview, movieArray[i].popularity], (error, results) => {
if (error) {
console.log('enters error in query', error)
callback(error, null);
} else {
callback(null, results);
}
})
}
}
function getMoviesFromDB(searchedMovie, callback) {
//use select as q
var q = `SELECT * FROM movie_list WHERE Title LIKE '${searchedMovie}%'`;
connection.query(q, (error, results, fields) => {
console.log(results)
if (error) {
console.log(error);
callback(error, null);
} else {
callback(null, results);
}
})
}
module.exports.addMovies = addMovies;
module.exports.getMoviesFromDB= getMoviesFromDB;