1
這是我的代碼:Psycopg2不commiting改變
conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s' port='%s'" % (db, user, server, password, port))
cursor = conn.cursor()
cursor.execute("UPDATE bazos_ads SET price = replace(price, 'Zadarmo', '-5')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, 'Nerozhoduje', '-4')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, 'Ponúknite', '-3')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, 'Dohodou', '-2')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, 'V texte', '-1')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, '€', '')")
cursor.execute("UPDATE bazos_ads SET price = replace(price, ' ', '')")
cursor.execute("TRUNCATE infinity_ads")
conn.commit()
cursor.execute('INSERT INTO infinity_ads (site,category,link,number,image,title,info,price,date,locality) SELECT site,category,link,number::integer,image,title,info,price::integer,date::date,locality FROM bazos_ads')
cursor.execute("TRUNCATE bazos_ads")
conn.commit()
conn.close()
我需要修改與TextField的數據庫列,以便我能夠將它們複製到第二個數據庫,指定列類型(Integrer在這種情況下)。如果我複製這些SQL並在PSQL中逐一運行,它就可以工作。但是,如果我嘗試運行在python這個代碼,我得到這個錯誤messagge:
psycopg2.DataError: invalid input syntax for integer: "6€"
所以我想psycopg2不commiting變化或者沒有與替換€符號另一個問題,但我無法弄清楚,因爲在PSQL它工作正常。
編輯:我仍然無法找到解決辦法,但至少這項工作:
#!/bin/bash
psql -U name-d pass-c "UPDATE bazos_ads SET price = replace(price, 'Zadarmo', '-5')"
...