I simple create a table by follow sql:
CREATE TABLE contacts (
id serial PRIMARY KEY,
name VARCHAR (100),
phones TEXT []
);
I am assume you are use pydb and I have create a table like the below show
id | name | phones
----+------+--------------
1 | | {123,222,33}
My python code is just simple insert into one list .
import pgdb
conn = pgdb.connect(database='test')
cur = conn.cursor()
lst = ['123','222','33']
cur.execute('insert into contacts(phones) values (%s)', (lst,))
conn.commit()
It's work for me! I guess you did not commit your cursor or your field type is no right!
Back to your example, i have create a table like yours:
CREATE TABLE tags_table(tags TEXT[]);
Before run my python code, let check table .
test=# select * from tags_table;
tags
------
(0 rows)
and my python code :
#import pgdb
#conn = pgdb.connect(database='test')
#if psycopg2 has used
#try this
import psycopg2
conn = psycopg2.connect(database='test')
cursor = conn.cursor()
tags_list = [
['foo'],
['foo', 'boo', 'goo'],
['boo', 'zoo']
]
for tags in tags_list:
cursor.execute("""INSERT INTO tags_table(tags) VALUES (%s);""", (tags,))
conn.commit()
After run the above code, my table got those result:
test=# select * from tags_table;
tags
---------------
{foo}
{foo,boo,goo}
{boo,zoo}
(3 rows)
I don't really understand why you need to show the result as {} but there are a simple way to do this by declare your own List type.
class MyList(list):
def __str__(self):
items = ','.join(self)
return '{' +'{}'.format(items if items else '') + '}'
def __repr__(self):
return self.__str__()
for i in d:
j = MyList(i[0])
print j
and you will got the result as below show!
{foo}
{foo,boo,goo}
{boo,zoo}
{foo}
{foo,boo,goo}
{boo,zoo}
tagsinstead of(tags,).not all arguments converted during string formatting