From: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> |
---|---|
To: | Hans Ginzel <hans(at)matfyz(dot)cz>, psycopg(at)postgresql(dot)org |
Subject: | Re: insert DEFAULT value |
Date: | 2021-03-03 23:23:28 |
Message-ID: | 05f8e563-162a-3a86-d939-7fb7f03811e6@aklaver.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | psycopg |
On 3/3/21 1:51 PM, Hans Ginzel wrote:
> How to insert DEFAULT value, please?
>
> import psycopg2
> from psycopg2.sql import DEFAULT #
> https://www.postgresql-archive.org/Inserting-default-values-into-execute-values-td6130148.html
>
>
> db = psycopg2.connect(host='host', dbname='db')
> cursor = db.cursor()
> cursor.execute("DROP TABLE IF EXISTS test_default")
> cursor.execute("CREATE TABLE test_default(i int NOT NULL DEFAULT 1)")
> cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,))
>
> cursor.execute("DROP TABLE IF EXISTS test_default")
> cursor.execute("CREATE TABLE test_default(j jsonb NOT NULL DEFAULT
> '{}'::jsonb)")
> cursor.execute("INSERT INTO test_default VALUES (%s)", (DEFAULT,))
>
> For both cases I get
> psycopg2.ProgrammingError: can't adapt type 'SQL'
You need to do something like:
cur.execute(sql.SQL("INSERT INTO test_default VALUES ({})").format(DEFAULT))
>
> Thank you in advance,
> HG
>
>
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | Hans Ginzel | 2021-03-04 05:43:28 | Re: insert DEFAULT value |
Previous Message | Hans Ginzel | 2021-03-03 21:51:37 | insert DEFAULT value |