0

I do have a problem with inserting a string full of single quotes in my redshift (or postgresql) through python 3.6.

Here's my input:

In[330]: out

Out[330]: 

"{'hourday': [17, 12, 18, 16], 'domain': ['google.ca', 'allrecipes.com'], 'device_type_id': [20], 'city': [90, 23, 42, 42]}"

It's a string and I tried by many means to insert it in my database thanks to this query:

cursor.execute("insert into table1 (message) VALUES (%s)",(out,))

I get a :

"psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block". 

I tried almost everything to fix it : replacing single quotes with double single quotes (to escape single quotes in sql), putting backslash to escape in python. But every single time, there was a problem or with sql or with python. At some point I even had a :

TypeError: not all arguments converted during string formatting

It's killing me, please help me !

Thank you !

EDIT : Found the problem. The connection with my database was actually cut... I refreshed it and the code worked. Thank you !

1 Answers1

1

I can't reproduce it with a local postgres instance:

conn = psycopg2.connect(host=DB_HOST, port=DB_PORT, user=DB_USER, password=DB_PASSWORD)
cursor = conn.cursor()
cursor.execute('CREATE TEMPORARY TABLE table1(message VARCHAR(1000)) ON COMMIT DELETE ROWS')
out = "\"{'hourday': [17, 12, 18, 16], 'domain': ['google.ca', 'allrecipes.com'], 'device_type_id': [20], 'city': [90, 23, 42, 42]}\""
cursor.execute('INSERT INTO table1 (message) VALUES (%s)', (out,))
cursor.execute('SELECT * from table1')
row = cursor.fetchone()
print(row[0])

Works without problems. Which driver are you using?

Dietmar
  • 506
  • 3
  • 10