簡體   English   中英

以任何方式進行參數化查詢並將其封裝在 function 中的 python

[英]any way to make parameterized queries and encapsulate it python in function

我想實現一個 python function ,它將使用參數執行SQL查詢。 為此,我開始使用psycopg2來訪問我的本地數據庫。 但是,我寫了一堆非常相似的SQL查詢,而每個SQL語句在取不同值方面略有不同。 My goal is I want to write up parametric SQL so I could wrap it up in python function, ideally, I could make function call with arbitrary parameters so it could replace param values in SQL statement. 我查看了SO帖子並得到了一些想法,但無法完成緊湊的 python function 可以執行SQL具有任意參數的語句。 I know how to write up python function with passing arbitrary parameters by using **kwargs , *args , but not sure how to do this for parametric SQl within python function. 在 python 中是否有任何有效的方法可以輕松做到這一點? 有什么可行的方法來實現這一點?

我的數據庫架構

這是我在 postgresql 中的表模式:

CREATE TABLE mytable(
date_received DATE,
pk_est VARCHAR,
grd_name VARCHAR,
cl_val SMALLINT,
quant_received NUMERIC,
mg_fb_price NUMERIC,
freight NUMERIC,
standard_price NUMERIC,
grd_delv_cost NUMERIC,
order_type VARCHAR,
pk_name VARCHAR,
item_type VARCHAR,
waiting_days NUMERIC,
item_name VARCHAR,
mk_price_variance NUMERIC,
);

我的示例 SQL 查詢

這是需要參數化的示例 SQL 查詢之一:

SELECT
    date_trunc('week', date_received) AS received_week,
    cl_val,
    ROUND(ROUND(SUM(quant_received * standard_price)::numeric,4) / SUM(quant_received),4) AS mk_price_1,
    ROUND(ROUND(SUM(quant_received * mg_fb_price)::numeric,4) / SUM(quant_received),4) AS mg_price_1,
    ROUND(ROUND(SUM(quant_received * mk_price_variance)::numeric,4) / SUM(quant_received),4) AS fb_mk_price_var,
    ROUND(ROUND(SUM(quant_received * freight)::numeric,4) / SUM(quant_received),4) AS freight_new,
    ROUND(ROUND(SUM(quant_received * grd_delv_cost)::numeric,4) / SUM(quant_received),4) AS grd_delv_cost_new,
    TO_CHAR(SUM(quant_received), '999G999G990D') AS Volume_Received
FROM mytable
WHERE date_received >= to_date('2010-10-01','YYYY-MM-DD')
    AND date_received <= to_date('2012-12-31','YYYY-MM-DD')
    AND item_type = 'processed'
    AND cl_val IN ('12.5','6.5','8.1','8.5','9.0')
    AND order_type IN ('formula')
    AND pk_name IN ('target','costco','AFG','KFC')
    AND pk_est NOT IN ('12')
GROUP BY received_week,cl_val
ORDER BY received_week ASC ,cl_val ASC;

我目前的嘗試

import psycopg2

connection = psycopg2.connect(database="myDB", user="postgres", password="passw", host="localhost", port=5432)
cursor = connection.cursor()
cursor.execute(
    """
    select * from mytable where date_received < any(array['2019-01-01'::timestamp, '2020-07-10'::timestamp])
    """)
record = cursor.fetchmany()

另一種嘗試:

cursor.execute("""
    select date_trunc('week', date_received) AS received_week,
        cl_val,
        ROUND(ROUND(SUM(quant_received * standard_price)::numeric,4) / SUM(quant_received),4) AS mk_price_1,
    from (
        select * from mytable 
        where  item_type = %s and order_type IN %s
    ) t;
""", (item_type_value, order_type_value))

results = [r[0] for r in cursor.fetchall()]

但是在我的代碼中,有很多硬編碼的部分需要參數化。 我想知道在 python 中有什么方法可以做到這一點。 誰能指出我如何實現這一目標? 在 python function 中實現參數 SQL 是否可行? 任何想法? 謝謝

目標

我希望像這樣實現 function :

def parameterized_sql(**kwargs, *args):
    connection = psycopg2.connect(database="myDB", user="postgres", password="passw", host="localhost", port=5432)
    cursor = connection.cursor()
    cursor.execute("""SQL statement with parameter""")
    ## maybe more

這只是 python function 的骨架我想實現它但不確定它是否可行。 任何反饋都會有所幫助。 謝謝

更新

I am expecting generic python function which can pass parameter value to SQL body so I can avoid writing up many SQL queries which actually have a lot of overlap from one to another, and it is not parameterized. 目標是使參數化的 SQL 查詢可以在 python function 中執行。

如果你想將命名的 arguments 傳遞給cursor.execute() ,你可以使用%(name)s語法並傳入一個字典。 有關更多詳細信息,請參閱文檔

這是使用您的查詢的示例:

import datetime
import psycopg2

EXAMPLE_QUERY = """
SELECT
    date_trunc('week', date_received) AS received_week,
    cl_val,
    ROUND(ROUND(SUM(quant_received * standard_price)::numeric,4) / SUM(quant_received),4) AS mk_price_1,
    ROUND(ROUND(SUM(quant_received * mg_fb_price)::numeric,4) / SUM(quant_received),4) AS mg_price_1,
    ROUND(ROUND(SUM(quant_received * mk_price_variance)::numeric,4) / SUM(quant_received),4) AS fb_mk_price_var,
    ROUND(ROUND(SUM(quant_received * freight)::numeric,4) / SUM(quant_received),4) AS freight_new,
    ROUND(ROUND(SUM(quant_received * grd_delv_cost)::numeric,4) / SUM(quant_received),4) AS grd_delv_cost_new,
    TO_CHAR(SUM(quant_received), '999G999G990D') AS Volume_Received
FROM mytable
WHERE date_received >= %(min_date_received)s
    AND date_received <= %(max_date_received)s
    AND item_type = %(item_type)s
    AND cl_val IN %(cl_vals)s
    AND order_type IN %(order_types)s
    AND pk_name IN %(pk_names)s
    AND pk_est NOT IN %(pk_ests)s
GROUP BY received_week ,cl_val
ORDER BY received_week ASC, cl_val ASC;
"""


def execute_example_query(cursor, **kwargs):
    """Execute the example query with given parameters."""
    cursor.execute(EXAMPLE_QUERY, kwargs)
    return cursor.fetchall()
            

if __name__ == '__main__':
    connection = psycopg2.connect(database="myDB", user="postgres", password="passw", host="localhost", port=5432)
    cursor = connection.cursor()
    execute_example_query(
        cursor,
        min_date_received = datetime.date(2010, 10, 1),
        max_date_received = datetime.date(2012, 12, 31),
        item_type = 'processed',
        cl_vals = ('12.5', '6.5', '8.1', '8.5', '9.0'),
        order_types = ('formula',),
        pk_names = ('target', 'costco', 'AFG', 'KFC'),
        pk_ests = ('12',)
    )

看一眼:

https://www.psycopg.org/docs/sql.html

“該模塊包含用於以方便和安全的方式動態生成 SQL 的對象和函數。SQL 標識符(例如表和字段的名稱)不能傳遞給 execute() 方法,如查詢 ZDBC11CAA5BDA99F77E6FB4DABD77

那里有多個例子。 如果他們沒有顯示您想要做什么,請修改您的問題以顯示您希望如何更改查詢的具體示例。

這是我在代碼中使用的示例:

insert_list_sql = sql.SQL("""INSERT INTO
        notification_list ({}) VALUES ({}) RETURNING list_id
    """).format(sql.SQL(", ").join(map(sql.Identifier, list_flds)),
                sql.SQL(", ").join(map(sql.Placeholder, list_flds)))

list_flds是我從 attrs 數據類中獲取的字段列表,如果我修改 class,它可能會發生變化。 在這種情況下,我不會修改表名,但沒有什么可以阻止您添加用{}替換表名,然后以另一個 sql.SQL() 的格式提供表名。 只需將上述內容包裝在接受您想要動態化的 arguments 的 function 中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM