我有课程可以连接到我的数据库。
import psycopg2, psycopg2.extensions
from parseini import config
import pandas as pd, pandas.io.sql as sqlio
class MyDatabase:
def __init__(self, name='mydb.ini'):
self.params = config(filename=name)
self.my_connection = psycopg2.connect(**self.params)
self.my_cursor = self.my_connection.cursor()
def fetch_all_as_df(self, sql_statement):
return sqlio.read_sql_query(sql_statement, self.my_connection)
def df_to_sql(self, df):
table = 'sometable'
return sqlio.to_sql(df, table, self.my_connection)
def __del__(self):
self.my_cursor.close()
self.my_connection.close()
Run Code Online (Sandbox Code Playgroud)
在我的情况下,我如何重新连接到数据库并处理 psycopg2.OperationalError ?
我有一张这样的桌子
create table public.incominglog
(
envelopeid varchar(36) default ''::character varying not null,
messagetype text default NULL::character varying,
initialenvelopeid varchar(36) default NULL::character varying,
processid varchar(36) default NULL::character varying,
logtimestamp timestamp,
customscode varchar(8),
exchtypeid integer
);
create unique index incominglog_envelopeid_uindex
on public.incominglog (envelopeid);
create unique index incominglog_initialenvelopeid_uindex
on public.incominglog (initialenvelopeid);
Run Code Online (Sandbox Code Playgroud)
我正在尝试插入这样的值
INSERT INTO
incominglog (
envelopeid,
messagetype,
initialenvelopeid,
processid,
logtimestamp,
customscode,
exchtypeid
)
VALUES
(
'ae2a2b46-ae4f-42a1-ada3-1f8f0aff7361',
'CMN.00001',
'aad06a96-667f-42c9-9196-8e0fec103d8b',
'4fed3854-e1de-42eb-b2c7-3ad714b58a9e',
'2019-04-17 14:57:54.195588',
'10210130',
'19200'
)
ON CONFLICT (initialenvelopeid)
DO update set
envelopeid = …Run Code Online (Sandbox Code Playgroud)