我使用python执行基本ETL,将记录从mysql数据库传输到postgres数据库。我正在使用python开始传输:
source_cursor = source_cnx.cursor()
source_cursor.execute(query.extract_query)
data = source_cursor.fetchall()
source_cursor.close()
# load data into warehouse db
if data:
target_cursor = target_cnx.cursor()
#target_cursor.execute("USE {};".format(datawarehouse_name))
target_cursor.executemany(query.load_query, data)
print('data loaded to warehouse db')
target_cursor.close()
else:
print('data is empty')
SELECT `tbl_rrc`.`id`,
`tbl_rrc`.`col_filing_operator`,
`tbl_rrc`.`col_medium`,
`tbl_rrc`.`col_district`,
`tbl_rrc`.`col_type`,
DATE_FORMAT(`tbl_rrc`.`col_timestamp`, '%Y-%m-%d %T.%f') as `col_timestamp`
from `tbl_rrc`
INSERT INTO geo_data_staging.tbl_rrc
(id,
col_filing_operator,
col_medium,
col_district,
col_type,
col_timestamp)
VALUES
(%s,%s,%s,%s,%s);
注意,Id上有一个PK约束
问题是,虽然我没有错误,但在目标表中看不到任何记录。我通过手动插入一条记录,然后再次运行来测试这一点。代码出错,违反了PK约束。所以我知道它在找桌子
如果你知道我可能会错过什么,我将不胜感激
使用
psycopg2
,必须在游标上调用commit()
,才能提交事务。如果只调用close()
,事务将隐式回滚这有几个例外。您可以将连接设置为“自动提交”。您还可以use your cursors inside a ^{} block ,如果块没有抛出任何异常,它将自动提交
相关问题 更多 >
编程相关推荐