【问题标题】:Why does this insert give error: There is a column named "column" in table "table", but it cannot be referenced from this part of the query为什么这个插入给出错误:表“table”中有一个名为“column”的列,但是不能从这部分查询中引用它
【发布时间】:2021-12-25 17:12:20
【问题描述】:

我正在尝试学习使用 JDBC 来更改 PostgreSQL 数据库。我想尝试将数据插入到表中。

private static void createDatabase(Connection connection) throws SQLException {
    Statement stmt = connection.createStatement();
    stmt.executeUpdate("DROP TABLE IF EXISTS " + TABLE_NAME);
    stmt.executeUpdate("CREATE TABLE my_table (Column1 Text)");
    stmt.executeUpdate("INSERT INTO my_table VALUES (column1)");        
}   

运行这个,我得到一个错误:

错误:列“column1”不存在
提示:在“my_table”表中有一个名为“column1”的列,但不能从这部分查询中引用。

如果表中存在该列,为什么会出现错误?

【问题讨论】:

  • 如果您尝试在文本类型的列collumn1 中插入值,则必须将要插入的文本括在单引号中。 "INSERT INTO my_table VALUES ('some text for collumn1')"

标签: postgresql jdbc


【解决方案1】:

问题是您的语句INSERT INTO my_table VALUES (Column1) 要求插入(到column1column1 的当前值。鉴于该行尚不存在,这无法完成:没有column1 的当前值。顺便说一句,明确列出要插入的列是一种很好的做法。

values 子句需要一个值、表达式或参数的列表。例如,您可以使用:

INSERT INTO my_table (Column1) VALUES ('Value for Column1')

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2014-08-08
    • 2018-11-17
    • 2019-11-04
    • 1970-01-01
    • 2015-07-13
    • 2019-01-14
    • 1970-01-01
    • 2016-03-22
    相关资源
    最近更新 更多