pg bytea

作者: hehehehe | 来源:发表于2022-08-15 16:26 被阅读0次
import psycopg2

database = ""
user = ""
pw = ""
host = ""
port = ""


def get_conn():
    conn = psycopg2.connect(database=database, user=user, password=pw,
                            host=host,
                            port=port)
    print("connected success")
    return conn


def write_pg_bytea():
    with get_conn() as conn_from:
        with conn_from.cursor() as cursor:
            with open("/data/a.zip", "rb") as file:
                file_read = file.read()
            print(file_read)
            print(type(file_read))
            data = psycopg2.Binary(file_read)
            cursor.execute(f"""
                Insert into hn_test(id2,data) values (256,{data});
            """)
    conn_from.close()


def read_pg_bytea():
    with get_conn() as conn_from:
        with conn_from.cursor() as cursor:
            cursor.execute("select data from hn_test where id2 = '256'")
            rcd = cursor.fetchone()
            data = rcd[0]  # get name
            print(data)
            with open("/Users/code/letsgo/utils_path.zip", "wb") as file:
                file.write(data)
    conn_from.close()


if __name__ == '__main__':
    # write_pg_bytea()
    read_pg_bytea()

相关文章

网友评论

      本文标题:pg bytea

      本文链接:https://www.haomeiwen.com/subject/slklgrtx.html