使用python在红移(或postgresql)中插入dict字符串。逃避问题?

使用python在红移(或postgresql)中插入dict字符串。逃避问题?,python,sql,postgresql,amazon-redshift,Python,Sql,Postgresql,Amazon Redshift,通过Python3.6在我的红移(或postgresql)中插入一个满是单引号的字符串确实有问题 以下是我的意见: In[330]: out Out[330]: "{'hourday': [17, 12, 18, 16], 'domain': ['google.ca', 'allrecipes.com'], 'device_type_id': [20], 'city': [90, 23, 42, 42]}" 这是一个字符串,由于此查询,我尝试了多种方法将其插入数据库: cursor.ex

通过Python3.6在我的红移(或postgresql)中插入一个满是单引号的字符串确实有问题

以下是我的意见:

In[330]: out

Out[330]: 

"{'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,))
我得到一个:

"psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block". 
我尝试了几乎所有的方法来修复它:用双单引号替换单引号(在sql中转义单引号),在python中使用反斜杠转义。但每一次,sql或python都会出现问题。在某个时候,我甚至有一个:

TypeError: not all arguments converted during string formatting
我快死了,请帮帮我

谢谢大家!


编辑:找到问题。与我的数据库的连接实际上被切断了。。。我刷新了它,代码运行正常。谢谢大家!

我无法用本地postgres实例复制它:

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])

工作没有问题。您使用的是哪个驱动程序?

尝试转义您已经尝试过的字符串。没用。