Python和PG Cop​​y

时间:2017-10-24 15:17:25

标签: python django postgresql

我有下面的代码,使用sqangchemy在Django应用程序中将CSV下载到内存。

array24 = 1:00,2:00,3:00 to 00:00
while [[ ! -f /home/22/job_stop.txt ]]
do
  #TIME=`date +%H%M|bc`
  currentHour=date '+%H:00'
    for loop array24 
    if  [$currentHour -eq $array24 ] || 

    then
            /folder/1/script.sh
    fi
    end for
  sleep 900
done
exit

如何使用pg_copy将其保存到数据库,而不是将其加载到数据框中。我试过了:

engine = create_engine('postgres://.....')

response = requests.get(STOCK_DATA_URL)

zip_file = ZipFile(BytesIO(response.content))
data_file_name = zip_file.namelist()[0]
new_prices = zip_file.open(data_file_name)

df = pd.read_csv(new_prices, names=['ticker', 'name'])

错误:

connection = engine.connect()
  sql = "copy assets_temp (ticker, name) FROM 'EOD-datasets-codes.csv' DELIMITER ','  CSV;"
  result = connection.execute()
  for row in result:
      print(row)
  connection.close()

1 个答案:

答案 0 :(得分:0)

在这里,您需要将raw_connection与copy_expert命令一起使用。将您的代码更改为

sql = "copy assets_temp (ticker, name) FROM 'EOD-datasets-codes.csv' DELIMITER ','  CSV;"
connection = engine.raw_connection()
cursor = connection.cursor()
cursor.copy_expert(sql)