Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres/psycopg2 - Inserting array of strings

I'm using Postgres 9 and Python 2.7.2 along with psycopg2 and am trying to insert an array of string values with properly escaped quotation marks. Sample:

metadata = {"Name": "Guest", "Details": "['One', 'Two', 'Three']"}

cur.execute("insert into meta values ('%s');" % metadata)

which throws the exception:

psycopg2.ProgrammingError: syntax error at or near "One"
LINE 1: "Details": "['One...
                      ^

I've also tried using Postgres' E to escape along with backslashes, but haven't found the correct combination yet. Ideas?

like image 507
Growth Mindset Avatar asked Jul 28 '11 01:07

Growth Mindset


People also ask

Should I use SQLAlchemy or psycopg2?

I've compared the speed of reading form postgresql database in python using SQLAlchemy and psycopg2. The psycopg2 is over 2x faster than SQLAlchemy on small table. This behavior is expected as psycopg2 is a database driver for postgresql while SQLAlchemy is general ORM library.

Does psycopg2 need PostgreSQL?

Prerequisites. The current psycopg2 implementation supports: Python versions from 3.6 to 3.11. PostgreSQL server versions from 7.4 to 15.


3 Answers

You have to let psycopg do parameters binding for you: don't try to quote them yourself.

Psycopg automatically converts a python list of strings into a postgres array. Check http://initd.org/psycopg/docs/usage.html

like image 189
piro Avatar answered Oct 04 '22 12:10

piro


When you want to insert an array into a postgreSQL DB via SQL you do it like this:

INSERT INTO tablename VALUES ('{value1,value2,value3}');

ATTENTION: You need the single quotes to surround the curly braces! So actually you're passing a String/Varchar of a special "array" grammar to the DB

If I enter your code into a python parser I get something like this:

'{'Name': 'Guest', 'Details': "['One', 'Two', 'Three']"}'

But PostgreSQL expects something like this:

'{"Name","Guest","Details",{"One","Two","Three"}}'

Check the manual on Arrays: http://www.postgresql.org/docs/9.0/static/arrays.html

So either you format the String according to the PostgreSQL "array-grammar" by writing a helper function or you use a library which does that for you.

like image 21
das_weezul Avatar answered Oct 04 '22 11:10

das_weezul


def lst2pgarr(alist):
    return '{' + ','.join(alist) + '}'

pyarray = ['pippo', 'minni', 1, 2]

conn = psycopg2.connection (  HERE PUT YOUR CONNECTION STRING  )
c = conn.cursor()

c.execute('select ... where pgarray_attr = %r' % (lst2pgarr(pyarray))
c.execute('insert into tab(pgarray_attr) values (%r)' % (lst2pgarr(pyarray))
like image 33
LittleEaster Avatar answered Oct 04 '22 10:10

LittleEaster