使用 Postgres 将值插入 TEXT 列时出现问题

Trouble inserting value into a TEXT column with Postgres

当我尝试将字母数字值插入 Postgres 数据库中的 TEXT 列时出现错误。这是 table 定义

]1

这是我用来将数据插入 table

的查询
INSERT INTO sensor_data
            (
                time,
                i1, i2, i3, i4,
                i5, i6, i7, i8,
                mrt,air_temperature,
                humidity,tvoc, device_id)
        VALUES (to_timestamp(1667595922) AT TIME ZONE 'UTC',
                41.340000,  26.160000, 25.860000, 26.160000,
                25.900000, 25.960000, 26.720000, 25.580000,
                26.085000, 28.065536,
                 55.204773,  40.000000, 1a0032000947363339343638
            );

这是我收到的错误消息

ERROR: syntax error at or near "a0032000947363339343638" LINE 12: 55.204773, 40.000000, 1a00320009473633...

当我输入这个查询时,它工作得很好。

INSERT INTO sensor_data
            (
                time,
                i1, i2, i3, i4,
                i5, i6, i7, i8,
                mrt,air_temperature,
                humidity,tvoc, device_id)
        VALUES (to_timestamp(1667595922) AT TIME ZONE 'UTC',
                41.340000,  26.160000, 25.860000, 26.160000,
                25.900000, 25.960000, 26.720000, 25.580000,
                26.085000, 28.065536,
                 55.204773,  40.000000, 10032000947363339343638
            );

两个查询之间的唯一区别是 device_id 值。当值为“1a0032000947363339343638”时插入失败,当值为“10032000947363339343638”时插入正常。

为什么当我尝试插入“1a0032000947363339343638”时插入失败,即使数据类型是文本?我如何让 table 接受 1a0032000947363339343638' 作为设备 ID?

虽然数字不需要用引号引起来,但字符串必须用引号引起来以区别于其他语法并允许有空格。

在 Postgres 中,这必须是单引号。其他数据库可能允许不同的引用。

INSERT INTO sensor_data
            (
                time,
                i1, i2, i3, i4,
                i5, i6, i7, i8,
                mrt,air_temperature,
                humidity,tvoc, device_id)
        VALUES (to_timestamp(1667595922) AT TIME ZONE 'UTC',
                41.340000,  26.160000, 25.860000, 26.160000,
                25.900000, 25.960000, 26.720000, 25.580000,
                26.085000, 28.065536,
                 55.204773,  40.000000, '1a0032000947363339343638'
            );

您可以在 Postgres documentation on constants 阅读更多内容。