我只需要一种在数据库中插入简单数据的方法。
这是我的代码:
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
const mysql = require('mysql')
var connection = mysql.createConnection({
host : 'xxx',
user : 'xxx',
password : 'xxx',
database : 'xxx'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world')
})
app.get('/adduser', function (req, res) {
var queryaggiungi = 'INSERT INTO users (psid, name, surname, gender, timezone, locale ) VALUES (11111111111111111, "aaa", "bbb", "male", 3, "IT_en");';
connection.query(queryadd, function (err, result) {
//if (err) throw err;
console.log("user added");
connection.end();
});
res.send('query: ' + queryadd);
})
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})
我第一次运行xxx.com/adduser,然后添加了行,输出为:
2017-10-19T21:22:37.062633+00:00 heroku[router]: at=info method=GET path="/adduser" host=xxx-xxx-xxx.herokuapp.com request_id=384b7896-16b6-42de-9963-79ffc181ad12 fwd="151.70.34.90" dyno=web.1 connect=0ms service=18ms status=304 bytes=150 protocol=https
2017-10-19T21:22:37.062148+00:00 app[web.1]: user added
之后,每次运行xxx.com/adduser时,都不再添加该行,我得到了这个:
2017-10-19T21:23:17.948224+00:00 app[web.1]: user added
2017-10-19T21:23:17.950545+00:00 app[web.1]: events.js:161
2017-10-19T21:23:17.950547+00:00 app[web.1]: throw er; // Unhandled 'error' event
2017-10-19T21:23:17.950547+00:00 app[web.1]: ^
2017-10-19T21:23:17.950547+00:00 app[web.1]:
2017-10-19T21:23:17.950548+00:00 app[web.1]: Error: Cannot enqueue Quit after invoking quit.
2017-10-19T21:23:17.950549+00:00 app[web.1]: at Protocol._validateEnqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:204:16)
2017-10-19T21:23:17.950549+00:00 app[web.1]: at Protocol._enqueue (/app/node_modules/mysql/lib/protocol/Protocol.js:139:13)
2017-10-19T21:23:17.950550+00:00 app[web.1]: at Protocol.quit (/app/node_modules/mysql/lib/protocol/Protocol.js:92:23)
2017-10-19T21:23:17.950550+00:00 app[web.1]: at Connection.end (/app/node_modules/mysql/lib/Connection.js:249:18)
2017-10-19T21:23:17.950551+00:00 app[web.1]: at Query._callback (/app/index.js:128:18)
2017-10-19T21:23:17.950551+00:00 app[web.1]: at Query.Sequence.end (/app/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
2017-10-19T21:23:17.950552+00:00 app[web.1]: at /app/node_modules/mysql/lib/protocol/Protocol.js:225:14
2017-10-19T21:23:17.950553+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:67:7)
2017-10-19T21:23:17.950553+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:98:9)
2017-10-19T21:23:17.952807+00:00 heroku[router]: at=info method=GET path="/adduser" host=tranquil-woodland-21158.herokuapp.com request_id=cb522acc-a134-4fb2-bf21-64ce97e9584a fwd="11.10.34.30" dyno=web.1 connect=0ms service=7ms status=304 bytes=150 protocol=https
2017-10-19T21:23:18.034002+00:00 heroku[web.1]: State changed from up to crashed
2017-10-19T21:23:18.020653+00:00 heroku[web.1]: Process exited with status 1
有什么问题?
我在JawsDB Mysql和ClearDB heroku插件中遇到了这个问题。
全部谢谢:)
修改
CREATE TABLE IF NOT EXISTS users (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
psid BIGINT,
name VARCHAR(20),
surname VARCHAR(20),
gender VARCHAR(6),
timezone INT,
locale VARCHAR(6),
inserted TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);