Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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
Python3 psycopg2承诺问题使用变量vs函数进行连接_Python_Python 3.x_Psycopg2 - Fatal编程技术网

Python3 psycopg2承诺问题使用变量vs函数进行连接

Python3 psycopg2承诺问题使用变量vs函数进行连接,python,python-3.x,psycopg2,Python,Python 3.x,Psycopg2,有人能给我解释一下为什么定义的test().commit()不能像varcon.commit()那样工作吗?其他一切似乎都很好。(使用ubuntu-trusty-32的虚拟漫游程序) 这可能是因为函数块(def)中的return语句不等于= psycopg2连接函数返回一个连接对象,该对象被分配给conn或varcon conn=psycopg2.connect(“dbname=test user=postgres password=secret”) test()函数还返回psycopg2连接

有人能给我解释一下为什么定义的test().commit()不能像varcon.commit()那样工作吗?其他一切似乎都很好。(使用ubuntu-trusty-32的虚拟漫游程序)


这可能是因为函数块(
def
)中的
return
语句不等于
=
psycopg2连接函数返回一个连接对象,该对象被分配给
conn
varcon

conn=psycopg2.connect(“dbname=test user=postgres password=secret”)

test()
函数还返回psycopg2连接对象,但在writer2中,它没有分配给变量(内存位置),这意味着没有引用

这也解释了为什么建立了数据库连接(在测试函数中初始化),但提交不起作用(断开引用)

()

也许试试

ami=test()

ami.commit()


要建立引用,这可能是因为功能块(
def
)中的
return
语句不等于像
=
这样的赋值 psycopg2连接函数返回一个连接对象,该对象被分配给
conn
varcon

conn=psycopg2.connect(“dbname=test user=postgres password=secret”)

test()
函数还返回psycopg2连接对象,但在writer2中,它没有分配给变量(内存位置),这意味着没有引用

这也解释了为什么建立了数据库连接(在测试函数中初始化),但提交不起作用(断开引用)

()

也许试试

ami=test()

ami.commit()


要在每次调用
psycopg2.connect()
时建立引用,请打开到数据库的新连接

因此,您的代码在一个连接中有效地执行SQL,提交另一个连接,然后关闭第三个连接。即使在
test()
函数中,也会打开两个不同的连接

我使用以下模式访问PostgreSQL:

conn = psycopg2.connect(DSN)

with conn:
    with conn.cursor() as curs:
        ...
        curs.execute(SQL1)

with conn:
    with conn.cursor() as curs:
        ...
        curs.execute(SQL2)

conn.close()
with
语句确保围绕SQL打开并正确提交事务。它还自动回滚事务,以防
中的代码引发异常


参考资料:

每次调用
psycopg2.connect()
都会打开到数据库的新连接

因此,您的代码在一个连接中有效地执行SQL,提交另一个连接,然后关闭第三个连接。即使在
test()
函数中,也会打开两个不同的连接

我使用以下模式访问PostgreSQL:

conn = psycopg2.connect(DSN)

with conn:
    with conn.cursor() as curs:
        ...
        curs.execute(SQL1)

with conn:
    with conn.cursor() as curs:
        ...
        curs.execute(SQL2)

conn.close()
with
语句确保围绕SQL打开并正确提交事务。它还自动回滚事务,以防
中的代码引发异常


参考资料:

感谢您的宝贵回复!你能编辑你的第二个链接到正确的地址()吗?因为project.org是另外一个网站。你的建议奏效了。另外,我还找到了另一种方法,但不确定这是否是一种好的做法:
test():try:psycopg2.connect('dbname=tourbank'),除了:print('Connection to tourbanking Database failed')else:conn2=psycopg2.connect('dbname=tourbanking'))conn2.autocommit=True返回conn2
in是python中建立和测试数据库连接的标准方法。你可以想怎么做就怎么做……请不要投票,因为我试图获得无名英雄徽章:)谢谢你非常有用的回复!你能编辑你的第二个链接到正确的地址()吗?因为project.org是另外一个网站。你的建议奏效了。另外,我还找到了另一种方法,但不确定这是否是一种好的做法:
test():try:psycopg2.connect('dbname=tourbank'),除了:print('Connection to tourbanking Database failed')else:conn2=psycopg2.connect('dbname=tourbanking'))conn2.autocommit=True返回conn2
in是python中建立和测试数据库连接的标准方法。你可以想怎么做就怎么做……请不要投票,因为我试图获得无名英雄徽章:)谢谢你的回复。很高兴知道这一点。我对python和PostgreSQL还是新手。我正在使用Udacity的程序进行研究,有趣的是,他们既没有提到也没有使用这种方法。我想知道为什么。。。我也不明白为什么简单地声明var=psycopg2.connect(DSN)会自动生成SQL连接?这与调用函数不同。我假设它被execute方法激活。我不确定是否应该建立“conn”的全局值,然后在各种定义的sql查询函数中使用它。
connect()
是一个函数。PostgreSQL在单独的进程(甚至服务器)中运行,在执行SQL之前,您需要建立到它的连接。然后可以多次重复使用同一连接。如果这样做,您应该准备好重新建立丢失的连接(一段时间后它们可能会断开)。现实世界的程序从连接池获取连接,连接池负责管理连接生命周期。感谢您的回复。很高兴知道这一点。我对python和PostgreSQL还是新手。我正在使用Udacity的程序进行研究,有趣的是,他们既没有提到也没有使用这种方法。我想知道为什么。。。我也不明白为什么简单地声明var=psycopg2.connect(DSN)会自动生成SQL连接?这与调用函数不同。我假设它被execute方法激活。我不确定是否应该建立“conn”的全局值,然后在各种定义的sql查询函数中使用它。
connect()
是一个函数