我在客户端构造了一个包含 SQL 语句的变量
var sql = "SELECT * FROM restaurant WHERE (areaCategory" + " " + area + ")" + "AND (cuisineCategory" + " " + cuisine + ") AND (priceCategory" + " " + price +")"
如何将这条 SQL 语句从客户端导出到服务器端以便发送这条语句? SQL 语句因情况而异,因此我必须创建一个变量。
答案 0 :(得分:2)
好吧,正如@Aley 所提到的,您真的不希望客户能够完全访问您的数据库!
相反,我会使用 AJAX 调用或表单将参数发送到服务器,然后在服务器端使用准备好的语句
您可能想使用像 axios 这样的库并使用 post 方法进行 Ajax 调用:
//client side
axios.post('/restaurant', {
area: areaCategory,
cuisine: cuisineCategory
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
表格应该是不言自明的
<!--client side-->
<form method="post" action=/restaurant">
<input type="text" name="area" placeholde="Area…">
<input type="text" name="cuisine" placeholde="Cuisine……">
<input type="submit">
</form>
由于有许多不同接口的不同数据库,这里有一些链接:
Does SQLite3 have prepared statements in Node.js?