psycopg2

psycopg2 does not insert unicode data

假装没事ソ 提交于 2019-12-11 04:37:40
问题 I have a script that takes data from one database and according to tables' names and fields copies it into another database. The issue is about unicode data, I need to insert some words in Russian but every time psycopg2 writes it as if it were default string. import psycopg2 import psycopg2.extensions conn_two = psycopg2.connect(user="postgres", password="password", host = "localhost", port= "5432", dbname = "base2") cur_2 = conn_two.cursor() sql = 'INSERT INTO {} ({}) VALUES {};'.format('

Insert scraped table data directly into PostgreSQL db

核能气质少年 提交于 2019-12-11 04:34:34
问题 I want to insert my scraped data directly into PostgreSQL db, I'm struggling with writing query for this, any help would be appreciated. The code I've came up with so far: import csv import urllib.request from bs4 import BeautifulSoup conn = psycopg2.connect(database='--',user='--', password='--', port=--) cursor = conn.cursor() soup = BeautifulSoup(urllib.request.urlopen("http://tis.nhai.gov.in/TollInformation?TollPlazaID=236").read(),'lxml') tbody = soup('table' ,{"class":"tollinfotbl"})[0]

Can't install psycopg2

戏子无情 提交于 2019-12-11 03:43:24
问题 I'm trying to install psycopg2 so I can use postgresQL in python and Django. I have run into multiple errors in the last few hours trying to install it, and my latest one I can't seem to solve. When I run setup.py install on the psycopg2 in the command console it attempts to install it, but then gives me an error when trying to execute link.exe in the Visual Studio 8 folder. This doesn't make sense to me, considering I got that executable to work when I launch it from Windows Explorer, and

copy data from MSSQL database to Postgresql database with Python

删除回忆录丶 提交于 2019-12-11 03:09:52
问题 I have two 2 databases. One with MSSQL and another with Postgresql. I want that my python script is running (for this I use a cron-job on linux), every day. The data from the MSSQL database should copied to the Postgresql database. I have an idea but it doesn't works. Could you help me??? Maybe my solution is totally wrong... That's my code: import pymssql, psycopg2 class DatabaseRequest: def __init__(self): self.conn1 = pymssql.connect(host='****', user='****', password='****', database='***

Escape input data for postgres

北城余情 提交于 2019-12-11 02:36:01
问题 I writing a python script for inserting of data in my postgres db. Is in postgres a escape function how I can escape the inserted data? 回答1: Just pass query parameters as a second argument to execute , like: >>> cur.execute( ... """INSERT INTO some_table (an_int, a_date, a_string) ... VALUES (%s, %s, %s);""", ... (10, datetime.date(2005, 11, 18), "O'Reilly")) Then, all of the parameters will be properly escaped. This is because psycopg2 follows Python Database API Specification v2.0 and

PostgreSQL 'Sever closed the connection unexpectedly'

依然范特西╮ 提交于 2019-12-11 02:29:33
问题 I am using Psycopg2 to insert into a local PostgreSQL database, however since upgrading my PC to windows 10 I keep getting the following error: psycopg2.OperationalError: server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. Sometimes the insert is successful, but can take a few attempts to do it. Also when accessing the database through pgadmin III I also get the same error messages when navigating through

Use python and psycopg2 to execute a sql file that contains a DROP DATABASE statement

女生的网名这么多〃 提交于 2019-12-11 02:27:26
问题 I am trying to use a python function to execute a .sql file. The sql file begins with a DROP DATABASE statement. The first lines of the .sql file look like this: DROP DATABASE IF EXISTS myDB; CREATE DATABASE myDB; The rest of the .sql file defines all the tables and views for 'myDB' Python Code: def connect(): conn = psycopg2.connect(dbname='template1', user='user01') conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) cursor = conn.cursor() sqlfile = open('/path/to/myDB

Psycopg2: 'module' object has no attribute 'connect'

风流意气都作罢 提交于 2019-12-11 02:23:51
问题 I'm trying to connect to a postgres database with psycopg2: import psycopg2 try: conn = psycopg2.connect("dbname='puppetdb' user='puppetdb' host='172.17.0.1' port='5432' password='puppetdb'") except Exception, e: print "I am unable to connect to the database" print e Which returns: I am unable to connect to the database 'module' object has no attribute 'connect' I've made sure that psycopg2 is installed with pip install psycopg2 and it seems like this should work according to the

How to have data structure with both tuple and dictionary characteristic

牧云@^-^@ 提交于 2019-12-11 01:56:11
问题 By refering code at http://initd.org/psycopg/docs/extras.html#dictionary-like-cursor >>> rec['data'] "abc'def" >>> rec[2] "abc'def" I was wondering how they manage to make a data structure having both tuple and dictionary characteristic? 回答1: In Python the [] lookups are handled by the __getitem__ magic method; in other words, when you index a custom class Python will call instance.__getitem__(...) and return you the value. This lets you do e.g. >>> class Foo: ... def __getitem__(self, value)

Python/Oracle/cx_Oracle date behavior with year=0 issues

蓝咒 提交于 2019-12-10 22:32:37
问题 I'm currently working on some ETL code to incrementally load data from a source Oracle system to a Postgresql analysis environment with Python as the glue. I am leveraging cx_Oracle as the driver to pull the Oracle data, but I am running into some strange issues with Oracle Date and Python datetime and year 0. As far as I understand, neither Oracle nor Python support year 0. I've isolated a particular row and field with an offending Date: SELECT CAR_RENTAL_DATE, EXTRACT(YEAR FROM CAR_RENTAL