简体   繁体   English

如何使用 Scala Java 将 DEFAULT 插入 Postgresql SERIAL 列

[英]How to insert DEFAULT into Postgresql SERIAL column using Scala Java

MySQL will accept NULL to an auto increment column and insert the last value. MySQL 将接受 NULL 到auto increment列并插入最后一个值。 So you could presumably use所以你大概可以使用

(Using the code from this SO answer , where st is a preparedStatement ) (使用这个 SO answer中的代码,其中st是一个preparedStatement

st.setNull(1, java.sql.Types.NULL)

I'm trying to figure out how you do the same with Postgresql, where you cannot use NULL , but must use DEFAULT instead:我想弄清楚你如何对 Postgresql 做同样的事情,你不能使用NULL ,但必须使用DEFAULT

INSERT INTO serial_table (id) VALUES(NULL)

Does something like this exist?这样的东西存在吗?

st.setDefault(1, java.sql.Types.DEFAULT)

Or或者

st.setObject(1, "DEFAULT")

I assume I can't just use the string "DEFAULT".我假设我不能只使用字符串“DEFAULT”。

EDIT:编辑:

To clarify, this is a testing table defined as such:澄清一下,这是一个定义如下的测试表:

CREATE TABLE serial_table (
    id SERIAL NOT NULL PRIMARY KEY
);

So I can't just skip the column to let Postgresql handle it所以我不能跳过专栏让 Postgresql 处理它

INSERT INTO serial_table VALUES()
ERROR:  syntax error at or near ")"
LINE 1: INSERT INTO serial_table VALUES();
                                        ^

To insert a row consisting only of default values you can use:要插入包含默认值的行,您可以使用:

INSERT INTO serial_table DEFAULT VALUES

Just omit the primary key:只需省略主键:

insert into foo (bar) values ("foobar")

Will assign the default values to any columns that are not mentioned explicitly.将默认值分配给任何未明确提及的列。 I am not sure how exactly it is done when you only have one column (the other answer seems to be offering a way to deal with it), but as you said yourself, it's not a sensical use case anyway (as a sidenote, you should not really be writing tests that cover "ridiculous" use cases).我不确定当你只有一列时它是如何完成的(另一个答案似乎提供了一种处理它的方法),但正如你自己所说,无论如何它都不是一个明智的用例(作为旁注,你不应该真正编写涵盖“荒谬”用例的测试)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用@InsertProvider在Postgresql Array(integer [])类型列中插入Java ArrayList - How to insert a java ArrayList in postgresql Array (integer[] ) type column using @InsertProvider 使用Java和MyBatis将NULL长插入PostgreSQL的bigint列中 - Insert long as NULL into bigint column in PostgreSQL using Java and MyBatis 如何使用 JOOQ 在 PostgreSQL 中插入带有 JSON 列的可更新记录? - How to insert a updatable record with JSON column in PostgreSQL using JOOQ? 如何在Java Springboot中使用jdbcTemplate将Integer数组插入到postgresql表中? - How to insert Integer array into postgresql table using jdbcTemplate in Java Springboot? 解析XML值并使用Java插入Postgresql - Parse XML values and insert into Postgresql using Java 如何使用 Java 将 JPEG 图像插入 PostgreSQL bytea 字段,然后使用 PHP 在网站上显示? - How do I insert a JPEG image into a PostgreSQL bytea field using Java and then display on a website using PHP? 如何在Java中使用DefaultTableCellRenderer将图像插入列表 - how to insert an image into a column table using DefaultTableCellRenderer in java 如何使用java / astyanax将值插入列族? - How to insert value into Column Family using java/astyanax? 如何使用Java将多个复选框值插入单个列 - how to insert multiple checkbox values into a single column using java 如何使用Java创建虚拟串行端口 - How to Create virtual serial port using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM