定義されたtest()。commit()がvarcon.commit()として機能しない理由を説明してもらえますか?他のすべては正常に動作するようです。 (の放浪VirtualBoxのUbuntuで、信頼できる-32を使用して)Python3 psycopg2コミットメントの問題(接続の変数と関数を使用)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import psycopg2
varcon = psycopg2.connect('dbname=tournament')
def test():
try:
psycopg2.connect("dbname=tournament")
except:
print("Connection to Tournament Database Failed")
else:
return psycopg2.connect('dbname=tournament')
def writer():
#db = psycopg2.connect('dbname=tournament')
c =varcon.cursor()
c.execute('select * from players')
data = c.fetchall()
c.execute("insert into players (name) values ('Joe Smith')")
varcon.commit()
varcon.close
print(data)
def writer2():
#db = psycopg2.connect('dbname=tournament')
c =test().cursor()
c.execute('select * from players')
data = c.fetchall()
c.execute("insert into players (name) values ('Joe Smith')")
test().commit()
test().close
print(data)
writer2() #this seem not commited, but database registers the insert by observing the serial promotion
#writer() # this works as expected
はあなたの非常に有用回答いただきありがとうございます! project.orgが別のものであるため、アドレスを修正するための2番目のリンクを編集してください(http://www.icu-project.org)。あなたの提案はうまくいった。また、別の方法を見つけましたが、それが良いのかどうかは分かりません: '' 'test(): try: psycopg2.connect( 'dbname = tournament') を除くpsycopg2.connect( 'トーナメントデータベースへの接続に失敗しました'他) : conn2 = psycopg2.connect( 'DBNAME =大会') conn2.autocommit = Trueの 戻りconn2''' –
でhttp://stackoverflow.com/questions/11735371/how-to-test-database -connectivity-in-pythonは、Pythonでデータベース接続を確立してテストする標準的な方法です。あなたは好きなようにこれを行うことができます... –
私は知られていないヒーローバッジを取得しようとするので、upvoteをしないでください:) –