psycopg2

Python psycopg2 check row exists

不问归期 提交于 2019-12-23 07:10:08
问题 In Python psycopg2 how can I check if a row exists? def track_exists(self, track_id): cur = self.conn.cursor() cur.execute("SELECT fma_track_id FROM tracks WHERE fma_track_id = %s", (track_id,)) if cur.fetchall() > 0: return true else: return false Currently I am getting Traceback (most recent call last): File "<stdin>", line 1, in <module> File "mumu.py", line 38, in track_exists if cur.fetchall() > 0: TypeError: 'NoneType' object has no attribute '__getitem__' 回答1: Don't use fetchall()

sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver

ぃ、小莉子 提交于 2019-12-23 06:47:11
问题 I am trying to run alembic migration and when I run alembic revision --autogenerate -m "Added initial tables" It fails saying sqlalchemy.exc.ArgumentError: Can't load plugin: sqlalchemy.dialects:driver the database url is postgresql+psycopg2://dev:passwd@localhost/db and I even have psycopg2 installed in my virtualenv $yolk -l Flask-Login - 0.1.3 - active Flask-SQLAlchemy - 0.16 - active Flask - 0.9 - active Jinja2 - 2.6 - active Mako - 0.7.3 - active MarkupSafe - 0.15 - active Python - 2.7.2

Cast a PostgreSQL row to text, but keep columns separate

时光总嘲笑我的痴心妄想 提交于 2019-12-23 04:06:05
问题 I am working on a Postgres viewer using Python. I need to convert all columns to text so I can display them in my GUI. I don't know how many columns there are per table or the names of them as this is supposed to be a generic viewer. Google got me this code: SELECT t.*::text FROM table AS t; However, this concatenates the row like this: t ----------------|------- (712,982,dfdfv) What I need is this (with type text of course), just like a normal SELECT * does: id | vendor_id | vendor_barcode -

Cast a PostgreSQL row to text, but keep columns separate

守給你的承諾、 提交于 2019-12-23 04:06:00
问题 I am working on a Postgres viewer using Python. I need to convert all columns to text so I can display them in my GUI. I don't know how many columns there are per table or the names of them as this is supposed to be a generic viewer. Google got me this code: SELECT t.*::text FROM table AS t; However, this concatenates the row like this: t ----------------|------- (712,982,dfdfv) What I need is this (with type text of course), just like a normal SELECT * does: id | vendor_id | vendor_barcode -

Why can't I install psycopg2? (Python 2.6.4, PostgreSQL 8.4, OS X 10.6.3)

眉间皱痕 提交于 2019-12-22 10:49:39
问题 ORIGINAL MESSAGE (now outdated): After running python setup.py install I get the following: Warning: Unable to find 'pg_config' filebuilding 'psycopg2._psycopg' extension gcc-4.0 -arch ppc -arch i386 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -O3 - DPSYCOPG_DEFAULT_PYDATETIME=1 -DPSYCOPG_VERSION="2.2.1 (dt dec ext pq3)" -DPSYCOPG_EXTENSIONS=1 -DPSYCOPG_NEW_BOOLEAN=1 -DHAVE_PQFREEMEM=1 -DHAVE_PQPROTOCOL3=1 -I/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -I. -c

Get warning messages through psycopg2

北慕城南 提交于 2019-12-22 03:52:09
问题 I want to call a plpgsql function through psycopg2 and see the warning messages. I.e, I have this function: create or replace function test_warning() returns void as $$ begin raise warning 'this is only a test'; end; $$ language plpgsql; and call it so in python: import psycopg2 conn = psycopg2.connect(conn_string) cursor = conn.cursor() cursor.callproc("test_warning") # or so: cursor.execute('SELECT test_warning()') Unfortunately the warning message as defined in plpgsql does not appear

There is no South database module 'south.db.postgresql_psycopg2' for your database django

倾然丶 夕夏残阳落幕 提交于 2019-12-22 03:35:48
问题 I have a django app with version as 1.6.5 , i am trying to upgrade it to 1.8 , but on the way i got the below error right after the django version was increased to 1.8 There is no South database module 'south.db.postgresql_psycopg2' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS. Code INSTALLED_APPS = [ 'django_messages', 'avatar', 'tinymce', 'south', 'tracking', ...... ] DATABASES = { 'default':

psycopg2 installation problem on OSX Lion

回眸只為那壹抹淺笑 提交于 2019-12-21 05:34:10
问题 I used PIP to install psycopg2 on OSX Lion. But when I try to import from python I get the following errors: Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import psycopg2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Python/2.7/site-packages/psycopg2/__init__.py", line 67, in <module> from psycopg2.

copy data from csv to postgresql using python

邮差的信 提交于 2019-12-20 12:34:31
问题 I am on windows 7 64 bit. I have a csv file 'data.csv'. I want to import data to a postgresql table 'temp_unicommerce_status' via a python script. My Script is: import psycopg2 conn = psycopg2.connect("host='localhost' port='5432' dbname='Ekodev' user='bn_openerp' password='fa05844d'") cur = conn.cursor() cur.execute("""truncate table "meta".temp_unicommerce_status;""") cur.execute("""Copy temp_unicommerce_status from 'C:\Users\n\Desktop\data.csv';""") conn.commit() conn.close() I am getting

How can I speed up update/replace operations in PostgreSQL?

ぐ巨炮叔叔 提交于 2019-12-20 09:17:09
问题 We have a rather specific application that uses PostgreSQL 8.3 as a storage backend (using Python and psycopg2). The operations we perform to the important tables are in the majority of cases inserts or updates (rarely deletes or selects). For sanity reasons we have created our own Data Mapper-like layer that works reasonably well, but it has one big bottleneck, the update performance. Of course, I'm not expecting the update/replace scenario to be as speedy as the 'insert to an empty table'