-
Notifications
You must be signed in to change notification settings - Fork 0
/
scouting.js
48 lines (40 loc) · 1.33 KB
/
scouting.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
var mysql = require('mysql');
function get_connection() {
return mysql.createConnection({
host : 'localhost',
database : 'strategy',
user : 'strategy',
password : 'foo',
});
}
//open a connection to the database, then call function c with the connection object
//c can send queries to the connection object
//c is expected to close connection when done
function with_connection(c) {
var connection = get_connection();
connection.connect(function(err) {
if (err) {
console.error('Error connecting: ' + err.stack);
return;
}
});
c(connection);
}
//this is test or demo code that's run only at top level
if (require.main === module) {
//get a connection to the database
//and then insert a random team
//this is an example of node sql
with_connection(connection =>
connection.query('INSERT INTO team (team_number, name) VALUES(?, ?) ON DUPLICATE KEY UPDATE team_number = team_number',
//the ON DUPLICATE KEY UPDATE is a mysql 'trick' to prevent errors in the database if reentering same team
[7889, 'OSAT'],
function (error, results, fields) {
if (error) {
throw error;
}
console.log(results.affectedRows + " updated");
})
);
}
module.exports.with_connection = with_connection;