0

I am getting an error when I try to insert an alphanumeric value into a TEXT column in a Postgres DB. This is the table definition

![enter image description here]1

This is the query I am using to insert data into the 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
            );

This is the error message I get

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

When I enter this query, it works just fine.

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

The only difference between the 2 queries is the device_id value. The insert fails when the value is '1a0032000947363339343638' and work fine when the value is '10032000947363339343638'.

Why would the insert fail when I try to insert '1a0032000947363339343638' even though the data type is a TEXT? And how do I get the table to accept 1a0032000947363339343638' for a device Id?

4

1 回答 1

0

虽然不需要引用数字,但必须引用字符串以将它们与其他语法区分开来并允许空格。

在 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 文档中阅读有关常量的更多信息。

于 2019-10-06T16:46:26.227 回答