請不要關閉這個問題。它不是重復的
我對 fastapi 和 python 完全陌生。我正在使用pgcrypto模塊來加密客戶的個人資訊。我的原始查詢是
select pgp_sym_decrypt(cast(email as bytea), 'secret_key') as email
from customers
并且查詢作業正常。如何產生類似的東西SqlAlchemy?我試過這樣的東西
from sqlalchemy import select, func, cast, LargeBinary
from sqlalchemy.dialects.postgresql import BYTEA
customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, 'bytea'), 'secret_key'))
也試過
customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, BYTEA), 'secret_key'))
也試過
customer = select(func.pgp_sym_decrypt(cast(Customer.c.email, LargeBinary), 'secret_key'))
但在任何地方都沒有運氣。那么,如何解決這個問題呢?任何幫助,將不勝感激。非常感謝你。
uj5u.com熱心網友回復:
該pgp_sym_decypt函式將 aBYTEA作為其引數,但pgp_sym_encrypt采用 a VARCHAR,因此不需要強制轉換。
import sqlalchemy as sa
engine = sa.create_engine('postgresql psycopg2:///test', echo=True, future=True)
tbl = sa.Table('t70770085', sa.MetaData(),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('name', sa.String))
tbl.drop(engine, checkfirst=True)
tbl.create(engine)
SECRET = 'secret_key'
with engine.begin() as conn:
conn.execute(tbl.insert().values(name=sa.func.pgp_sym_encrypt('Alice', SECRET)))
with engine.connect() as conn:
query = sa.select(sa.func.pgp_sym_decrypt(sa.cast(tbl.c.name, sa.LargeBinary), SECRET))
result = conn.execute(query)
print(result.scalar_one())
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/420242.html
標籤:
