python - How to efficiently submit multiple SQL query strings in Python?
问题描述
I have a python function that takes in a list of people (first name, last name, and date of birth stored in a dict), loops through this list and queries the database person by person, and returns a list containing the results found.
So for example if the user would want to query 3 people using the list [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Charlie'}]
, the function would query the database 3 times, each time getting a list of potential matches that gets put into another list. The could return a list of 3 lists each containing matching results for Alice, Bob and Charlie. The function looks like the following.
query_string = "SELECT * FROM some_db"
def improved_batch_client_check(user_requests, db_connection):
responses = []
for one_request in user_requests:
first_name = one_request.get('FirstName')
last_name = one_request.get('LastName')
dob = one_request.get('BirthDate')
query_string_end = " WHERE db.NAME LIKE '%{}%' AND db.NAME LIKE '%{}%'".format(first_name, last_name)
if dob is None:
pass
else:
query_string_end += " AND db.DOB = '{}'".format(str(dob))
df_candidates = pandas.read_sql(query_string + query_string_end, db_connection)
responses.append(df_candidates.to_dict('records'))
return jsonify(responses)
What I want to do is to improve efficiency by having to call the function pandas.read_sql()
only once by somehow submitting a list of different queries that returns dataframes in a similar manner to what is used below. What is the easiest way to do it in Python?
解决方案
为了使其成为一个查询,我将创建一个或的查询,即 -
select * from table where (request1) or (request2) ...
request
如果没有给出 dob 或-
db.NAME LIKE '%{}%' AND db.NAME LIKE '%{}%'".format(first_name, last_name)
如果给出 dob -
db.NAME LIKE '%{}%' AND db.NAME LIKE '%{}%' AND db.DOB = '{}'".format(first_name, last_name, str(dob))
把它们放在一起——
def improved_batch_client_check(user_requests, db_connection):
requests = []
for one_request in user_requests:
first_name = one_request.get('FirstName')
last_name = one_request.get('LastName')
dob = one_request.get('BirthDate')
curr_request = "db.NAME LIKE '%{}%' AND db.NAME LIKE '%{}%'".format(first_name, last_name)
if dob is not None:
curr_request += " AND db.DOB = '{}'".format(str(dob))
requests.append(curr_request)
query_string = "SELECT * FROM some_db WHERE " + 'or '.join([f'({x})' for x in requests])
df = pandas.read_sql(query_string, db_connection)
return jsonify (df.to_dict('records'))
推荐阅读
- flutter - 我已经编写了这段代码,但是它不允许我在 ChangeNotifierProvider 中使用构建器方法?我不知道,如何摆脱这个?
- angular - 组件和模块提供者声明中使用的 Angular ValueProvider 和 Injector 字面量
- angular - 如何在 Angular 中使用 sass-loader?
- php - 如何在 php/laravel 中获取过去 5 周的范围及其开始和结束日期
- java - 在 Spring 集成测试中执行删除时如何测试约束冲突?
- google-apps-script - Google Script - 将文件名与文件夹名进行比较,如果名称匹配,则将文件移入文件夹
- python - 为什么创建 Keras 模型时无法添加图层(NoneType 错误)
- javascript - 如何删除 forEach 函数变量上的“未定义”状态
- javascript - Regx 验证不适用于 Google Chrome Android 应用程序,但适用于 Chrome 浏览器 - PC
- java - 相同类和相同实现但不同对象类型作为参数的 JAVA 方法