1

我在将数据从一个数据库处理到另一个数据库时遇到问题,这里是 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=',')

这是面临的一个错误:

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
4

1 回答 1

1

关闭文件,然后打开读取

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=',')
于 2012-12-21T15:28:37.847 回答