admin管理员组文章数量:1309939
I am studying the psycopg3 library for working with postgresql, I wanted to know if I applied the correct approach to working with psycopg3. I recently learned what pool connections are, so I wrote the following code:
from flask import Flask
from psycopg_pool import ConnectionPool
app = Flask(__name__)
pool = ConnectionPool(conninfo='dbname=test user=test password=test',min_size=4, max_size=10)
@app.route("/")
def index():
with pool.connection() as conn:
ans = conn.execute("SELECT * FROM users;").fetchall()
return {"result": ans}
Before this I just created a connection using the old approach but I didn't know about the pool:
from flask import Flask
import psycopg2
app = Flask(__name__)
def db_connect():
conn = psycopg2.connect(database="test", user="test",
password="test.", host="localhost", port=5432)
return conn
@app.route("/")
def index():
with db_connect() as conn:
with conn.cursor() as cur:
cur.execute("SELECT * FROM users;")
ans = cur.fetchall()
return {"result": ans}
I would like to know if I am using and programming psycopg3 correctly.
本文标签: pythonCorrect use of psycopg3Stack Overflow
版权声明:本文标题:python - Correct use of psycopg3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741861234a2401634.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论