Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用;插入“;在psycopg2连接池中?_Python_Connection Pooling_Psycopg2_Psycopg - Fatal编程技术网

Python 如何使用;插入“;在psycopg2连接池中?

Python 如何使用;插入“;在psycopg2连接池中?,python,connection-pooling,psycopg2,psycopg,Python,Connection Pooling,Psycopg2,Psycopg,我使用psycopg2在Python上连接到PostgreSQL,我想使用连接池 我不知道在执行INSERT查询时应该做什么来代替commit()和rollback() db = pool.SimpleConnectionPool(1, 10,host=conf_hostname,database=conf_dbname,user=conf_dbuser,password=conf_dbpass,port=conf_dbport) # Get Cursor @contextmanager d

我使用psycopg2在Python上连接到PostgreSQL,我想使用连接池

我不知道在执行INSERT查询时应该做什么来代替commit()和rollback()

db = pool.SimpleConnectionPool(1, 10,host=conf_hostname,database=conf_dbname,user=conf_dbuser,password=conf_dbpass,port=conf_dbport)


# Get Cursor
@contextmanager
def get_cursor():
    con = db.getconn()
    try:
        yield con.cursor()
    finally:
        db.putconn(con)


with get_cursor() as cursor:
    cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") 
    id = cursor.fetchone()

没有commit(),我无法获取插入记录的id。

更新我无法测试代码,但我给您一些想法: 您在连接中而不是在数据库中进行提交

# Get Cursor
@contextmanager
def get_cursor():
    con = db.getconn()
    try:
        yield con
    finally:
        db.putconn(con)

with get_cursor() as cursor:
    con.cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") 
    con.commit()
    id = cursor.fetchone()



连接池的存在是因为创建到数据库的新连接可能会很昂贵,而且不会避免提交或回滚。因此,您可以毫无问题地提交数据,提交数据不会破坏连接。

以下是我的工作示例:

db = pool.SimpleConnectionPool(1, 10,host=conf_hostname,database=conf_dbname,user=conf_dbuser,password=conf_dbpass,port=conf_dbport)


@contextmanager
def get_connection():
    con = db.getconn()
    try:
        yield con
    finally:
        db.putconn(con)

def write_to_db():
    with get_connection() as conn:
        try:
            cursor = conn.cursor()
            cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") 
            id = cursor.fetchone()
            cursor.close()
            conn.commit()
        except:
            conn.rollback()

我认为这将是一个更具pythonic的

db_pool=pool.SimpleConnectionPool(1,10,,
host=CONF.db\u主机,
database=CONF.db_name,
user=CONF.db_user,
password=CONF.db\u用户,
端口=CONF.db_端口)
@上下文管理器
def db():
con=db_pool.getconn()
cur=con.cursor()
尝试:
屈服con,cur
最后:
当前关闭()
布特康(康涅狄格州)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
使用db()作为(连接,光标):
尝试:
cursor.execute(“”)插入表(字段)
值(返回id“”的值)
my_id=cursor.fetchone()
rowcount=cursor.rowcount
如果rowcount==1:
commit()连接
其他:
连接。回滚()
除了psycopg2。错误为错误:
打印('数据库错误:',错误)
例外情况除外,例如:
打印('一般错误:',例如)

我找到了提交的配置:“con.autocommit=True”,我能为回滚做些什么()谢谢。但是如何提交数据呢?db.commit()不存在!或者我应该使用“con.commit()”和“con.rollback()”,是吗?是的,“con.commit()”和“con.rollback()”谢谢你的回答!我还有一个关于连接池的问题。“如果我在我的程序中打开一个连接而不关闭它会发生什么?而是连接池”。我的程序作为服务运行,并侦听套接字中的数据包。我想每秒保存1000条以上的记录!连接池用于共享同一个连接,而不是打开100个到db的连接。我只打开一个,使用100次。不监听任何套接字,它不是一个服务,它只是一个客户端。
db = pool.SimpleConnectionPool(1, 10,host=conf_hostname,database=conf_dbname,user=conf_dbuser,password=conf_dbpass,port=conf_dbport)


@contextmanager
def get_connection():
    con = db.getconn()
    try:
        yield con
    finally:
        db.putconn(con)

def write_to_db():
    with get_connection() as conn:
        try:
            cursor = conn.cursor()
            cursor.execute("INSERT INTO table (fields) VALUES (values) RETURNING id") 
            id = cursor.fetchone()
            cursor.close()
            conn.commit()
        except:
            conn.rollback()