-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
63 lines (58 loc) · 1.64 KB
/
test.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
var fs = require("fs");
var mysql = require('mysql');
var config = require('./config.js')
var time = '1H'
// mysql 参数
var connection = mysql.createConnection({
host : config.config[0].host,
user : config.config[0].user,
password : config.config[0].password,
port: config.config[0].port,
database: config.config[0].database
});
// 读取Json文件
var data = fs.readFileSync('test.json');
// 转为json格式
data = JSON.parse(data)
// 新建连接
connection.connect();
// 插入数据
var addSql = 'INSERT INTO bitstamp_candles(exchange,start_at,end_at,open,high,low,close,volume_traded,trades_count,interval_at)' +
'VALUES("BitStamp",?,?,?,?,?,?,?,?,"'+ time +'")';
//执行
var l = data.length
for (var i = 0; i < l; ++i){
var addSqlParams = [
data[i].time_period_start,
data[i].time_period_end,
data[i].price_open,
data[i].price_high,
data[i].price_low,
data[i].price_close,
data[i].volume_traded,
data[i].trades_count
]
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
execute(WirteLog, '[INSERT ERROR] - ',err.message)
return;
}
// 写入历史文件
// execute(WirteLog, JSON.stringify(result)+"\n")
});
console.log('写入成功')
}
connection.end();
function execute (someFunction, value) {
someFunction(value);
}
function WirteLog (result) {
var fs = require("fs");
result.WriteTime = new Date()
fs.appendFile('insert.log', result, function(err) {
if (err) {
return console.error(err);
}
})
}