什么是 Postgres _text 类型?
What is the Postgres _text type?
我有一个带有 _text
类型(注意下划线)的 Postgres table,我无法确定如何将字符串 []
插入 table。
这是我的 table 定义:
CREATE TABLE public.newtable (
column1 _text NULL
);
我启用了 postgis
扩展程序:
CREATE EXTENSION IF NOT EXISTS postgis;
还有我的 python 代码:
conn = psycopg2.connect()
conn.autocommit = True
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
rows = [("[]",)]
insert_query = f"INSERT INTO newtable (column1) values %s"
psycopg2.extras.execute_values(cur, insert_query, rows, template=None, page_size=100)
此returns以下错误:
psycopg2.errors.InvalidTextRepresentation: malformed array literal: "[]"
LINE 1: INSERT INTO newtable (column1) values ('[]')
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
如何插入这些数据?这个错误是什么意思? Postgres 中的 _text
类型是什么?
汇总我的评论:
CREATE TABLE public.newtable (
column1 _text NULL
);
--_text gets transformed into text[]
\d newtable
Table "public.newtable"
Column | Type | Collation | Nullable | Default
---------+--------+-----------+----------+---------
column1 | text[] | | |
insert into newtable values ('{}');
select * from newtable ;
column1
---------
{}
在Python中:
import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
cur.execute("insert into newtable values ('{}')")
con.commit()
cur.execute("select * from newtable")
cur.fetchone()
([],)
cur.execute("truncate newtable")
con.commit()
cur.execute("insert into newtable values (%s)", [[]])
con.commit()
cur.execute("select * from newtable")
cur.fetchone()
([],)
来自 psycopg2
文档 Type adaption Postgres 数组适用于 Python 列表,反之亦然。
更新
在 Postgres 系统目录 pg_type 中找到 _text
类型。在 psql
:
\x
Expanded display is on.
select * from pg_type where typname = '_text';
-[ RECORD 1 ]--+-----------------
oid | 1009
typname | _text
typnamespace | 11
typowner | 10
typlen | -1
typbyval | f
typtype | b
typcategory | A
typispreferred | f
typisdefined | t
typdelim | ,
typrelid | 0
typelem | 25
typarray | 0
typinput | array_in
typoutput | array_out
typreceive | array_recv
typsend | array_send
typmodin | -
typmodout | -
typanalyze | array_typanalyze
typalign | i
typstorage | x
typnotnull | f
typbasetype | 0
typtypmod | -1
typndims | 0
typcollation | 100
typdefaultbin | NULL
typdefault | NULL
typacl | NULL
请参阅上面的 pg_type
link 以获取有关列所指内容的信息。在 link 的“Table 52.63.typcategory 代码代码类别 A 数组类型”中映射的 A
的 typcategory
是一个线索。以及 typinput
、typoutput
等值。
我有一个带有 _text
类型(注意下划线)的 Postgres table,我无法确定如何将字符串 []
插入 table。
这是我的 table 定义:
CREATE TABLE public.newtable (
column1 _text NULL
);
我启用了 postgis
扩展程序:
CREATE EXTENSION IF NOT EXISTS postgis;
还有我的 python 代码:
conn = psycopg2.connect()
conn.autocommit = True
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
rows = [("[]",)]
insert_query = f"INSERT INTO newtable (column1) values %s"
psycopg2.extras.execute_values(cur, insert_query, rows, template=None, page_size=100)
此returns以下错误:
psycopg2.errors.InvalidTextRepresentation: malformed array literal: "[]"
LINE 1: INSERT INTO newtable (column1) values ('[]')
^
DETAIL: "[" must introduce explicitly-specified array dimensions.
如何插入这些数据?这个错误是什么意思? Postgres 中的 _text
类型是什么?
汇总我的评论:
CREATE TABLE public.newtable (
column1 _text NULL
);
--_text gets transformed into text[]
\d newtable
Table "public.newtable"
Column | Type | Collation | Nullable | Default
---------+--------+-----------+----------+---------
column1 | text[] | | |
insert into newtable values ('{}');
select * from newtable ;
column1
---------
{}
在Python中:
import psycopg2
con = psycopg2.connect(dbname="test", host='localhost', user='postgres')
cur = con.cursor()
cur.execute("insert into newtable values ('{}')")
con.commit()
cur.execute("select * from newtable")
cur.fetchone()
([],)
cur.execute("truncate newtable")
con.commit()
cur.execute("insert into newtable values (%s)", [[]])
con.commit()
cur.execute("select * from newtable")
cur.fetchone()
([],)
来自 psycopg2
文档 Type adaption Postgres 数组适用于 Python 列表,反之亦然。
更新
在 Postgres 系统目录 pg_type 中找到 _text
类型。在 psql
:
\x
Expanded display is on.
select * from pg_type where typname = '_text';
-[ RECORD 1 ]--+-----------------
oid | 1009
typname | _text
typnamespace | 11
typowner | 10
typlen | -1
typbyval | f
typtype | b
typcategory | A
typispreferred | f
typisdefined | t
typdelim | ,
typrelid | 0
typelem | 25
typarray | 0
typinput | array_in
typoutput | array_out
typreceive | array_recv
typsend | array_send
typmodin | -
typmodout | -
typanalyze | array_typanalyze
typalign | i
typstorage | x
typnotnull | f
typbasetype | 0
typtypmod | -1
typndims | 0
typcollation | 100
typdefaultbin | NULL
typdefault | NULL
typacl | NULL
请参阅上面的 pg_type
link 以获取有关列所指内容的信息。在 link 的“Table 52.63.typcategory 代码代码类别 A 数组类型”中映射的 A
的 typcategory
是一个线索。以及 typinput
、typoutput
等值。