使用 fetch javascript 执行多个 sql 查询
我试图弄清楚如何通过获取并从SQL执行中返回数据多次查询。
该解决方案不一定是承诺,我只想执行多个获取,并在下一次获取之前每次获得结果。
我在一个函数中,单击一个按钮,javascript/hmtl/python/python/flask/postgresql,
Promise.all([
fetch("/query_village/village/1277"),
fetch("/query_fathers/fathers/18")
]).then(values => {
return Promise.all(values.map(r => r.json()));
}).then(([village, fathers]) =>{
console.log(village)
console.log(fathers)
})
我想要村庄的第1277行和18个父亲。
结果我得到的是:
在后台,我在Python中有此代码:
> @app.route("/query_village/<collection_village>/<code_id_village>")
> def query(collection_village, code_id_village):
> data_village = Database.fetchall_where(collection_village, 'code_id', code_id_village, return_dict=True)
> if data_village:
> return data_village[0]
> return {}
>
> @app.route("/query_fathers/<collection_fathers>/<code_id_fathers>")
> def query(collection_faters, code_id_faters):
> data_faters = Database.fetchall_where(collection_faters, 'code_id', code_id_faters, return_dict=True)
> if data_faters:
> return data_faters[0]
> return {}
在数据库中,我有:
def fetchall_where(table: str, column: str, value: Any, return_dict=False) -> List:
Database.CURSOR.execute(f"SELECT * FROM public.\"{table}\" WHERE \"{column}\"='{value}'")
fetch_all = Database.CURSOR.fetchall()
# Return as a dict
if return_dict:
columns = [col[0] for col in Database.CURSOR.description]
rows = [dict(zip(columns, row)) for row in fetch_all]
return rows
# Return as a list
return fetch_all
请放心,数据库内的乡村表中有数据。但是,似乎在父亲返回之前没有发生村庄的返回,就好像处决正在覆盖上一个。
我在执行中添加了一些印刷品,我得到了此版本(我还尝试了fetchone()
):
here village
SELECT * FROM public."village" WHERE "code_id"='1277'
here fathers
SELECT * FROM public."fathers" WHERE "code_id"='18'
village (1277, 'some village', 2)
fathers None
您可以看到它进入村庄的路线,执行村庄,然后与父亲一起进入,执行父亲,然后以价值返回村庄,现在父亲没有……
为什么在执行SQL声明后显示的村庄结果不显示村庄的印刷?
让我知道该示例中是否不清楚。
I am trying to figure out how to query multiple times with fetch and return data from SQL execution.
The solution doesn't have to be a Promise, I just want to execute multiple fetches and get the result each time before the next fetch is done.
I'm inside a function called by a click of a button, javascript/hmtl/python/flask/postgresql
Promise.all([
fetch("/query_village/village/1277"),
fetch("/query_fathers/fathers/18")
]).then(values => {
return Promise.all(values.map(r => r.json()));
}).then(([village, fathers]) =>{
console.log(village)
console.log(fathers)
})
I want the row 1277 of village and 18 of fathers.
What I get as a result is:
In the background, I have this code in python:
> @app.route("/query_village/<collection_village>/<code_id_village>")
> def query(collection_village, code_id_village):
> data_village = Database.fetchall_where(collection_village, 'code_id', code_id_village, return_dict=True)
> if data_village:
> return data_village[0]
> return {}
>
> @app.route("/query_fathers/<collection_fathers>/<code_id_fathers>")
> def query(collection_faters, code_id_faters):
> data_faters = Database.fetchall_where(collection_faters, 'code_id', code_id_faters, return_dict=True)
> if data_faters:
> return data_faters[0]
> return {}
and in the database I have:
def fetchall_where(table: str, column: str, value: Any, return_dict=False) -> List:
Database.CURSOR.execute(f"SELECT * FROM public.\"{table}\" WHERE \"{column}\"='{value}'")
fetch_all = Database.CURSOR.fetchall()
# Return as a dict
if return_dict:
columns = [col[0] for col in Database.CURSOR.description]
rows = [dict(zip(columns, row)) for row in fetch_all]
return rows
# Return as a list
return fetch_all
Rest assured there is data in the village table inside the database. But it seems that the return of the village does not happen before the return of the fathers, as if the execution is overwriting the previous one.
I've added a few prints in the execution and I got this (I also tried with fetchone()
):
here village
SELECT * FROM public."village" WHERE "code_id"='1277'
here fathers
SELECT * FROM public."fathers" WHERE "code_id"='18'
village (1277, 'some village', 2)
fathers None
As you can see it enters the route for village, execute the village, then enter with fathers, execute the fathers, then return the village with the value and now fathers is None...
Why isn't the print of the result of Village shown after the execution of the SQL statement?
Let me know if something is not clear in the example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论