psycopg2

psycopg2 executemany with simple list?

ε祈祈猫儿з 提交于 2019-12-10 22:07:06
问题 I'm trying to use psycopg2 executemany for a simple multi-insert but I can only make it work using dict and not "plain" sequence of values: # given: values = [1, 2, 3] ; cursor = conn.cursor() # this raises TypeError: 'int' object does not support indexing: cursor.executemany('INSERT INTO t (col_a) VALUES ( %s )', values) # I also tried encapsulating my 'values' into a tuple/list but it gives another exception (TypeError: not all arguments converted during string formatting). # while this is

Psycopg2 string formatting with variable names for type creation

Deadly 提交于 2019-12-10 18:25:28
问题 When passing in a variable type name for creation in postgres using psycopg2 using its formatting option, an error was throw over bad string formatting for the name of objects. Are names not allowed to be passed in using %s within the query parameters? The code I wrote that does solve my issues is below (just looking for a better way to resolve this) cursor.execute("CREATE TYPE {0} AS ENUM %s".format(name), (tuple(set([e.upper() for e in elements])),)) 回答1: (2) is completely unrelated to

psycopg error, column does not exist

人盡茶涼 提交于 2019-12-10 16:35:07
问题 I keep getting this error: psycopg2.ProgrammingError: column "someentry" does not exist. The error indicates that the column someentry does not exist when someentry is not a column it's just a value to enter into the db. Here is the code that gives the error: cur.execute('INSERT INTO {0!s} (ip_id, item) VALUES ({1!s}{2!s})'.format('mytable',1,'someentry')) Here is how I create my table: tablename = 'mytable' command = """ CREATE TABLE IF NOT EXISTS {} ( ip_id SERIAL PRIMARY KEY, item VARCHAR

Heroku tutorial with PIP, virtualenv, psycopg2, Django on Windows 7

点点圈 提交于 2019-12-10 16:20:53
问题 I've been doing a lot of reading lately about Heroku and I'm thinking about moving over to it. I thought I would give their little tutorial a try and play around with it a bit. Ok. Time for a confession. While I have virtualenv installed, I don't really use it. I'm not a guy that has to switch between different projects very often (e.g. like a contractor my need to do). I really like the idea of it, but just not that experienced. I know everyone says that it's best practices these days. The

Calculate DATEDIFF in POSTGRES using SQLAlchemy

拜拜、爱过 提交于 2019-12-10 15:07:28
问题 I need to calculate DATEDIFF in minutes between 2 columns of timestamp type. There are so many simple examples on the web, but none of them work really much properly using psycopg2 + sqlalchemy. I've tried: from sqlalchemy import as sa from datetime import datetime # con is a standard pool of connections :class:Connection con.execute( sa.func.datediff( sa.literal_column('minute'), datetime.utcnow(), datetime.utcnow(), ) ) it throws: sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError)

Create a sqlalchemy engine using an existing psycopg2 connection pool

放肆的年华 提交于 2019-12-10 14:58:07
问题 I'm adding a new ORM class using sqlalchemy's declarative mapping system. My codebase has an existing psycopg2 connection pool, which I want to reuse - I don't want code using my orm classes to have its own pool. There's a lot of existing code which directly calls get_conn on the psycopg2 pool, so I don't want to just replace it either. I'm having a problem constructing the engine to connect with. pool_config = {...} POOL = psycopg2.pool.ThreadedConnectionPool(0, 32, **pool_config) [...]

Psycopg2 callproc and sql parameters

拟墨画扇 提交于 2019-12-10 14:57:55
问题 I got some SQL function CREATE OR REPLACE FUNCTION tools.update_company(IN company_id integer, OUT value integer) RETURNS integer AS $BODY$ BEGIN select * into value from function_making_int(company_id) END;$BODY$ and from Psycopg2 (its inside Django if that matters) I do c = connection.cursor() c.callproc('tools.update_company', [1, ]) but function returns exactly the same input sequence as I gave, ignoring results and OUT parameter. Change to IN OUT and passing some foo value changes

High Sierra + Python + Postgresql error: Illegal instruction: 4

走远了吗. 提交于 2019-12-10 14:28:30
问题 I can connect to Postgres via the command line no problem, however if I try to connect via Python using the psycopg2 module I get the error below. Interestingly I have just tried connecting with PSeqal.app and it crashes with the same error. Environment: MacOS: 10.13.3 Xcode 9.2 Python: 3.6.4 Connection module: psycopg2 Postgresql: 10.3 Script: Here is my simple connection script which is trying to connect via Python to Postgresql: def connect(self, params): """ Connect to the PostgreSQL

error inserting values to db with psycopg2 module

只谈情不闲聊 提交于 2019-12-10 13:55:41
问题 I am attempting to insert a dataframe into my postgres database using the pscycopg2 module used with sqlalchemy. The process is loading an excel file into a pandas dataframe and then inserting the dataframe into the database via the predefined table schema. I believe these are the relevant lines of code: post_meta.reflect(schema="users") df = pd.read_excel(path) table = sql.Table(table_name, post_meta, schema="users") dict_items = df.to_dict(orient='records') connection.execute(table.insert()

postgres - cannot drop database using psycopg2

早过忘川 提交于 2019-12-10 13:48:28
问题 So I simply trying to drop and recreate my database using Python's psycopg2. Here is my code: with psycopg2.connect(database="postgres", user="postgres", password="****") as conn: with conn.cursor() as cur: conn.autocommit = True # Explains why we do this - we cannot drop or create from within a DB transaction. http://initd.org/psycopg/docs/connection.html#connection.autocommit cur.execute("DROP DATABASE crowdsurfer;") cur.execute("CREATE DATABASE crowdsurfer;") When I run this code, I get PS