Flutter 请求数据库
Flutter put request for Database
我正在开发 Flutter 应用程序。我们有一个 PSQL 数据库,后台有 Node 服务器。在 Flutter 应用程序上,我显示了一些我成功从数据库中获取的几何图形。现在,在对几何图形(例如线条)进行修改后,我希望能够通过放置请求更新数据库。
服务器是这样的:
app.put('/api/shape/:id', async (req,res) =>{
let answer;
if( req.body.shape_type == "line"){
answer = await db.db.modify_line(req.params.id, req.body.info_shape);
}
res.send(answer);
});
而 db.js 文件如下:
modify_line : async function(id_shape, info_shape){
console.log(info_shape);
const result = await send_query("UPDATE line SET line = WHERE id_shape = ", [id_shape, info_shape]);
return(result);
},
在 Flutter 应用程序上,我这样做:
_makeUpdateRequest() async {
var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();
Map data;
if (globals.selectedType == globals.Type.line) {
String lseg = "(" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dy.toString() + ")";
data = {
'shape_type': 'line',
'info_shape': {
'id_shape': globals.selectedShapeID.toString(),
'line': lseg,
}
};
}
http.Response response;
try {
//encode Map to JSON
print("encode Map to JSON");
var body = json.encode(data);
print(body);
response =
await http.put(url,
headers: {
"Content-Type": "application/json"
},
body: body
).catchError((error) => print(error.toString()));
} catch (e) {
print(e);
}
return response;
}
数据库 "line" table 每行包含 "shapeID" 和 "lseg" 信息。
目前我在尝试此代码时遇到错误:
{ id_shape: '619',
line: '(19.5,100.6,20.5,50.9)' }
fail____error: invalid input syntax for type lseg: "{"id_shape":"619","line":"(-19.5,100.6,20.5,50.9)"}"
我该如何塑造我的 lseg json?
谢谢
好吧,在我看来你正在将整个 input_shape
对象传递给 SQL 查询,根据你的 console.log
:
{
id_shape: '619',
line: '(19.5,100.6,20.5,50.9)'
}
显然,这对 Postgre 无效SQL。
我会说你的后端代码应该更像这样:
modify_line : async function(id_shape, info_shape){
console.log(info_shape);
const result = await send_query(
"UPDATE line SET line = WHERE id_shape = ",
// Reference "line" sub-object
[id_shape, info_shape.line],
);
return(result);
},
您还应注意行的 Geometric types 格式:
[ ( x1 , y1 ) , ( x2 , y2 ) ]
( ( x1 , y1 ) , ( x2 , y2 ) )
( x1 , y1 ) , ( x2 , y2 )
x1 , y1 , x2 , y2
阅读本文后我不能 100% 确定您的格式(带前导和尾随括号)是正确的。
问题已解决,现回复如下:
DB.js 就像:
modify_line : async function(id_shape, info_shape){
const result = await send_query("UPDATE line SET line = WHERE id_shape = ", [info_shape['id_shape'], info_shape['line']]);
return(result);
},
Flutter 应用就像:
_makeUpdateRequest() async {
var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();
Map data;
if (globals.selectedType == globals.Type.line) {
String lseg =
"[" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dy.toString() + "]";
data = {
'shape_type': 'line',
'info_shape': {
'id_shape': globals.selectedShapeID.toString(),
'line': lseg,
}
};
}
http.Response response;
try {
//encode Map to JSON
print("encode Map to JSON");
var body = json.encode(data);
print(body);
response =
await http.put(url,
headers: {
"Content-Type": "application/json"
},
body: body
).catchError((error) => print(error.toString()));
} catch (e) {
print(e);
}
return response;
}
我正在开发 Flutter 应用程序。我们有一个 PSQL 数据库,后台有 Node 服务器。在 Flutter 应用程序上,我显示了一些我成功从数据库中获取的几何图形。现在,在对几何图形(例如线条)进行修改后,我希望能够通过放置请求更新数据库。
服务器是这样的:
app.put('/api/shape/:id', async (req,res) =>{
let answer;
if( req.body.shape_type == "line"){
answer = await db.db.modify_line(req.params.id, req.body.info_shape);
}
res.send(answer);
});
而 db.js 文件如下:
modify_line : async function(id_shape, info_shape){
console.log(info_shape);
const result = await send_query("UPDATE line SET line = WHERE id_shape = ", [id_shape, info_shape]);
return(result);
},
在 Flutter 应用程序上,我这样做:
_makeUpdateRequest() async {
var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();
Map data;
if (globals.selectedType == globals.Type.line) {
String lseg = "(" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dy.toString() + ")";
data = {
'shape_type': 'line',
'info_shape': {
'id_shape': globals.selectedShapeID.toString(),
'line': lseg,
}
};
}
http.Response response;
try {
//encode Map to JSON
print("encode Map to JSON");
var body = json.encode(data);
print(body);
response =
await http.put(url,
headers: {
"Content-Type": "application/json"
},
body: body
).catchError((error) => print(error.toString()));
} catch (e) {
print(e);
}
return response;
}
数据库 "line" table 每行包含 "shapeID" 和 "lseg" 信息。
目前我在尝试此代码时遇到错误:
{ id_shape: '619',
line: '(19.5,100.6,20.5,50.9)' }
fail____error: invalid input syntax for type lseg: "{"id_shape":"619","line":"(-19.5,100.6,20.5,50.9)"}"
我该如何塑造我的 lseg json? 谢谢
好吧,在我看来你正在将整个 input_shape
对象传递给 SQL 查询,根据你的 console.log
:
{
id_shape: '619',
line: '(19.5,100.6,20.5,50.9)'
}
显然,这对 Postgre 无效SQL。
我会说你的后端代码应该更像这样:
modify_line : async function(id_shape, info_shape){
console.log(info_shape);
const result = await send_query(
"UPDATE line SET line = WHERE id_shape = ",
// Reference "line" sub-object
[id_shape, info_shape.line],
);
return(result);
},
您还应注意行的 Geometric types 格式:
[ ( x1 , y1 ) , ( x2 , y2 ) ]
( ( x1 , y1 ) , ( x2 , y2 ) )
( x1 , y1 ) , ( x2 , y2 )
x1 , y1 , x2 , y2
阅读本文后我不能 100% 确定您的格式(带前导和尾随括号)是正确的。
问题已解决,现回复如下: DB.js 就像:
modify_line : async function(id_shape, info_shape){
const result = await send_query("UPDATE line SET line = WHERE id_shape = ", [info_shape['id_shape'], info_shape['line']]);
return(result);
},
Flutter 应用就像:
_makeUpdateRequest() async {
var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();
Map data;
if (globals.selectedType == globals.Type.line) {
String lseg =
"[" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
globals.pLines[globals.selectedLineIndex].p2.dy.toString() + "]";
data = {
'shape_type': 'line',
'info_shape': {
'id_shape': globals.selectedShapeID.toString(),
'line': lseg,
}
};
}
http.Response response;
try {
//encode Map to JSON
print("encode Map to JSON");
var body = json.encode(data);
print(body);
response =
await http.put(url,
headers: {
"Content-Type": "application/json"
},
body: body
).catchError((error) => print(error.toString()));
} catch (e) {
print(e);
}
return response;
}