· 6 years ago · Jun 26, 2019, 06:52 AM
1let mysql = require('mysql');
2
3let MysqlPoolBooster = require('mysql-pool-booster');
4 mysql = MysqlPoolBooster(mysql);
5
6let pool = mysql.createPool({
7 host: '...',
8 user: '...',
9 password: '...',
10 database: '...'
11});
12
13
14pool.getConnection(function (err) {
15 if (err) {
16 return console.error('error: ' + err.message);
17 }
18 console.log('Connected to the MySQL server.');
19
20 let createRecs = `CREATE TABLE IF NOT EXISTS recommendations (
21 id INT primary key,
22 recImg VARCHAR (255),
23 recDetails VARCHAR (255),
24 recTitle VARCHAR (255),
25 recCost INT,
26 recRating INT,
27 recratingCount INT,
28 roomId INT,
29 INDEX (id)
30 )
31 ENGINE= MyISAM;`
32
33 pool.query(`USE recs;`)
34
35 pool.query(createRecs, function (err, results, fields) {
36 if (err) {
37 console.log(err.message);
38 }
39 })
40})
41
42 function save() {
43 var start = new Date()
44 pool.query(`SET AUTOCOMMIT = 0;`)
45 pool.query(`SET foreign_key_checks=0`)
46 pool.query(`SET unique_checks=0`)
47 pool.query(`SET sql_log_bin = 0`)
48
49 pool.query(`LOAD DATA LOCAL INFILE 'data1.csv'
50 INTO TABLE recommendations
51 FIELDS TERMINATED BY ','
52 LINES TERMINATED BY '\n'
53 ;`, (err, result, fields) => {
54
55 if (err) {
56 console.error('err on insert', err)
57 }
58 var end = new Date() - start
59 console.log('took: %dms', end)
60 })
61}
62
63
64let find = (id) => {
65 pool.query(`select * from recommendations where id ='${id}'`, (err, res) => {
66 console.log(res)
67
68 })
69}