简体   繁体   English

java sql error列的类型为整数,但表达式的类型为字符变化

[英]java sql error column is of type integer but expression is of type character varying

I have prepared sql statement: 我已经准备好sql语句:

PreparedStatement insert_query = db.prepareStatement(String.format(INSERT_QUERY, table, 
                              columns.toString(), placeholder));
System.out.println(insert_query);

Result: 结果:

INSERT INTO "article" (article_text, article_id, article_title) VALUES (?, ?, ?) RETURNING article_id

Next I create values list, set parameters and execure insert query: 接下来,我创建值列表,设置参数并执行插入查询:

ArrayList<String> values = new ArrayList<String>(); -- > is [New CONTENT, 4, New TITLE]
for (int i = 1; i <= values.size(); i++) {
            insert_query.setString(i, values.get(i-1));
        }
System.out.println(insert_query);
insert_query.executeUpdate();

I got an error: 我收到一个错误:

INSERT INTO "article" (article_text, article_id, article_title) VALUES ('New CONTENT', '4', 'New TITLE') RETURNING article_id

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: column "article_id" is of type integer but expression is of type character varying

Something is wrong with article_id. article_id出了点问题。 I put it as string here. 我把它放在这里作为字符串。 However when I manually try this query in PostgreSQL everything works((( 但是,当我在PostgreSQL中手动尝试此查询时,一切正常(((

For the article_id, you must do insert_query.setInteger instead of insert_query.setString . 对于article_id,您必须执行insert_query.setInteger而不是insert_query.setString When I check your exception, the article_id is sent to the DB as a String. 当我检查您的异常时,article_id将作为字符串发送到数据库。 That's why you have this exception. 这就是为什么您有此异常。

Better solution I found is initiate values list as: 我发现更好的解决方案是初始化值列表为:

ArrayList<Object> values = new ArrayList<Object>();

And insert into statement object: 并插入语句对象:

insert_query.setObject(i, values.get(i-1));

暂无
暂无

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

相关问题 列是 uuid 类型,但表达式是 Spark Scala 中不同的字符类型 - Column is of type uuid but expression is of type character varying in Spark Scala JOOQ-列“ id”的类型为uuid,但表达式的类型为字符变化 - JOOQ - column “id” is of type uuid but expression is of type character varying org.postgresql.util.PSQLException:错误:列“标签”是 jsonb 类型,但表达式的类型是字符变化 - org.postgresql.util.PSQLException: ERROR: column "tags" is of type jsonb but expression is of type character varying 错误:列“DOJ”的类型是没有时区的时间戳,但表达式的类型是字符变化 - ERROR: column "DOJ" is of type timestamp without time zone but expression is of type character varying 错误: <column-name> 是没有时区的时间,但表达的类型字符是变化的 - Error: the <column-name> is of time without time zone but expression is of type character varying 当我在 Jooq 中插入日期时,出现此错误:column creation_date is of type timestamp with time zone but expression is of type character varying - When I insert a date in Jooq, I get this error: column creation_date is of type timestamp with time zone but expression is of type character varying PostgreSQL 提示:您需要重写或转换表达式。 “状态”列的类型是状态,但表达式的类型是字符变化 - PostgreSQL Hint: You will need to rewrite or cast the expression. column “state” is of type status but expression is of type character varying 错误:递归查询“t”第 3 列在非递归项中有类型字符变化(255),但类型字符整体变化 - ERROR: recursive query “t” column 3 has type character varying(255) in non-recursive term but type character varying overall 在字符类型变化的列上使用聚合函数 - Using aggregate function on a column of type character varying org.postgresql.util.PSQLException:错误:列的类型为日期,但表达式的类型为 integer:无法使用 Z93F725A07423FE1C889F448B33D21F4 将日期写入 postgres - org.postgresql.util.PSQLException: ERROR: column is of type date but expression is of type integer: cannot write date into postgres using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM