cursor.copy_from fallito in postgres

cursor postgresql psycopg2 python sqlbulkcopy

Domanda

Sto affrontando problemi nel gestire i dati da un db ad un altro db qui è il codice Python.

import psycopg2

db = psycopg2.connect("dbname='db' user='xx' password='xx' host='127.0.0.2'")
st = db.cursor();

db_l = psycopg2.connect("dbname='xx' user='xx' password='xx' host='127.0.0.1'")
st_l = db_l.cursor();


q = "create table tarf as select i_rate, i_tariff As id, prefix, price_1, price_n, interval_1, interval_n, '00:00' AS start_time, '23:59:59' AS end_time, activation_dat
e, '3049-1-1 00:00' as exp  from rates JOIN tariffs USING (i_tariff) JOIN billing_plans USING(i_tariff) JOIN accounts using (i_billing_plan)"

st_l.execute(q)

f = open('tariff.sql','w')
st_l.copy_to(f,'tarf',sep=',')

st.copy_from(f,'tariff',sep=',')

Ecco un errore affrontato:

Traceback (most recent call last):
  File "sync_tariff.py", line 23, in <module>
    st.copy_from(f,'tariff',sep=',')
psycopg2.extensions.QueryCanceledError: COPY from stdin failed: error in .read() call
CONTEXT:  COPY tariff, line 1

Risposta accettata

Chiudere il file e quindi aprirlo per la lettura

f = open('tariff.sql', 'w')
st_l.copy_to(f, 'tarf', sep=',')

f.close()

f = open('tariff.sql', 'r')
st.copy_from(f, 'tariff', sep=',')


Autorizzato sotto: CC-BY-SA with attribution
Non affiliato con Stack Overflow
Autorizzato sotto: CC-BY-SA with attribution
Non affiliato con Stack Overflow