数据表"/>
[使用nodejs和express从查询参数更新mysql数据表
好吧,我想我越来越接近解决这个问题了。现在,我正在端口3000上运行以下脚本。
curl localhost:3000/register?name=bob\&width=13.970000\&time=0
curl localhost:3000/wheels?left=0.000000\&right=0.000000\&time=0 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=100 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=110 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=120 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=130 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=200 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=210 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=220 --cookie "USER=bob"
然后,我使用nodejs并表示将这些查询参数添加到数据库中。一切都在使用“ / register”,因为我能够在数据库中存储“ user”和“ width”的数据,但是当我尝试从“ / wheels”更新相同的表“ left1”和“ right1”属性时“那么,我最能做的就是将数据插入到一个全新的表中,该表的名称和宽度都恢复为默认值。
最后,我想做的是让该应用在cars数据库中创建一个新的数据库条目,并填写名称,宽度,left1,right1,时间,dist,l1,l2,l3,ir数据行然后在所有这些数据都填满后在数据库中创建另一个条目...但是我现在所能做的就是让它输入大量仅填充了名称(bob)和width(14)属性的不同数据集出来..这是我的代码:
const express = require('express');
const session = require('express-session');
const mysql = require('mysql');
let app = express();
var pool = mysql.createPool( {
host: 'localhost',
user: 'root',
password: 'secret',
database: 'cars',
connectionLimit: 10,
multipleStatements : true
});
app.get('/register', function(req, res) {
pool.query("INSERT INTO cars (`name`, `width`, `time`) VALUES (?,?,?)", [req.query.name, req.query.width, req.query.time]);
});
app.get('/wheels', function (req, res) {
pool.query("UPDATE `cars` SET `left1`=?, `right1`=?",
[req.query.left, req.query.right]) });
app.listen(3000, function(req, res) {
console.log('Express JS ready for port 3000');
});
这是数据库的图片:phpMyAdmin database pic
知道我可能要去哪里了吗?有人告诉我尝试使用会话和cookie,但是我一直在网上查找有关“ cookie解析器”和“ cookie会话”的信息,但我一直都没有去尝试。我不知道如何使用它们对mysql数据库做任何事情,也找不到与我要使用它做事有关的任何在线内容...太困惑了。任何帮助将是巨大的!谢谢
回答如下:根据docs,表/列名称周围不应包含引号。尝试使用:
app.get('/wheels', function (req, res) {
pool.query("UPDATE cars SET left1 = ?, right1 = ?",
[req.query.left, req.query.right])
});
更多推荐
[使用nodejs和express从查询参数更新mysql数据表
发布评论