我正在用python3编写程序,并使用psycopg2将程序与数据库连接,但是相同的代码在Windows中不起作用(我使用Ubuntu)。
唯一的区别是我的PostgresSQL版本。在Windows中安装了psycopg2,python3和PostgreSQL。我在两个操作系统中都创建了相同的数据库。
try:
connection = psycopg2.connect(user = "oscar",
password = "postgres", #this password is required in Windows
host = "127.0.0.1",
port = "5432",
database = "Manejador_iva")
cursor = connection.cursor()
# Print PostgreSQL Connection properties
#print ( connection.get_dsn_parameters(),"\n")
#Insert
postgres_insert_query = " INSERT INTO "+ impuesto +" (monto, mes, anho) VALUES (%s,%s, %s)"
record_to_insert = (aCargar, mes, anho)
cursor.execute(postgres_insert_query, record_to_insert)
connection.commit()
count = cursor.rowcount
print (count, "Record inserted successfully into table ")
我也这样处理异常:
except (Exception, psycopg2.Error) as error :
print(error.pgerror)
但不打印任何内容。