【问题标题】:Insert data into row in PostgreSQL column by column将数据逐列插入PostgreSQL中的行
【发布时间】:2014-08-06 04:24:03
【问题描述】:

我在 PostgreSQL 数据库中创建了一个包含 42 列的表。其中五个是bigint 类型,而其他是character varying 类型。然后我尝试使用一些java代码和spring框架中的jdbc库插入数据。

我遇到了一些需要定位的错误,这很难,因为该表包含太多列。
要插入该表的 jdbc 中的语法:

        String sql = "insert into t_reconcile_stage values("
                + " ?::bigint,"             // 1 id 
                + "?::bigint,"              // 2 m_participant_id
                + "?::bigint,"              // 3 m_status_data_id
                + "?,"                      // 4 reconcile_file
                + "?::date,"                // 5 reconcile_date
                + "?,"                      // 6 transaction_date
                + "?,"                      // 7 transaction_time
                + "?::bigint,"              // 8 stan
                + "?,"                      // 9 rrn
                + "?,"                      // 10 merchant_type
                + "?,"                      // 11 terminal_id
                + "?,"                      // 12 pan
                + "?,"                      // 13 debit_account
                + "?,"                      // 14 credit_account
                + "?::bigint,"              // 15 amount 
                + "?,"                      // 16 customer_id
                + "?,"                      // 17 participant_reference
                + "?,"                      // 18 data_key
                + "?,"                      // 19 dealer_code
                + "?,"                      // 20 biller_code
                + "?,"                      // 21 product_code
                + "?,"                      // 22 feature_code
                + "?::bigint,"              // 23 acquire_fee
                + "?::bigint,"              // 24 issuer_fee
                + "?::bigint,"              // 25 biller_fee
                + "?::bigint,"              // 26 switching_fee
                + "?::bigint,"              // 27 merchant_fee
                + "?,"                      // 28 transaction_type
                + "?,"                      // 29 trfree
                + "?,"                      // 30 free_data1
                + "?,"                      // 31 free_data2
                + "?,"                      // 32 free_data3
                + "?,"                      // 33 free_data4
                + "?,"                      // 34 free_data5
                + "?,"                      // 35 free_data6
                + "?,"                      // 36 free_data7
                + "?,"                      // 37 free_data8
                + "?,"                      // 38 free_data9
                + "?,"                      // 39 free_data10
                + "?::bigint,"              // 40 settlement_amount
                + "?::bigint,"              // 41 charge_amount
                + "?"                       // 42 status
                + ")";                      // 

        simpleJdbcTemplate.update( sql,
                d[0],d[1],d[2],d[3],d4,
                d[5],d[6],d[7],d[8],d[9],
                d[10],d[11],d[12],d[13],d[14],
                d[15],d[16],d[17],d[18],d[19],
                d[20],d[21],d[22],d[23],d[24],
                d[25],d[26],d[27],
                d[28],d[29],d[30],d[31],d[32],d[33],d[34],d[35],d[36],d[37],d[38],
                d[39],d[40],d[41]
        );

我得到的错误:

[2014-08-06 10:48:01,753] [ INFO] - org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
[2014-08-06 10:48:01,769] [ERROR] - dwidasa.reconcile.service.etl.PreReconcileImpl - error in -> etl(6000007777-20140608-gabung.txt) error in inserting data = 101932,0608,00006003110,5136,534110565309,201406,05280698950B312D9FF000,000000078281,00000078281,0000000000,000000000,0020 into tabel t_reconcile_stage
org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [insert into t_reconcile_stage values( ?::bigint,?::bigint,?::bigint,?,?::date,?,?,?::bigint,?,?,?,?,?,?,?::bigint,?,?,?,?,?,?,?,?::bigint,?::bigint,?::bigint,?::bigint,?::bigint,?,?,?,?,?,?,?,?,?,?,?,?,?::bigint,?::bigint,?)]; ERROR: invalid input syntax for integer: ""; nested exception is org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: ""
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
    at org.springframework.jdbc.core.JdbcTe

我不知道错误发生在哪一列。所以我需要在java中使用try-catch语句将数据逐列插入到该表中。是否可以在 Postgres 中进行这样的 INSERT 查询?
或任何其他方法来定位该错误?还是我错过了什么?
(我是处理数据库的初学者。)

【问题讨论】:

    标签: java sql postgresql jdbc


    【解决方案1】:

    INSERT 是一个 all-or-nothing 操作。插入了一个新行,并且需要为每一列分配一个值或 NULL,无论是使用显式输入还是从列默认值。所以不,你所要求的是不可能的。

    来自 Postgres 的错误是:

    错误:整数的输入语法无效:“”

    问题的根源在于您试图将 空字符串 作为bigint 号码出售,这是不可能的。将字符数据转换为整数时,必须将空字符串转换为某种有效形式。 0NULL(不带引号)。

    【讨论】:

    • @Mark:感谢您的关注,但在这种情况下,我实际上是想随便写“sell”。就像“试图将某物作为其他东西出售”。
    • 感觉不对所以改了,现在想知道是不是应该把空字符串卖成a bigint number,但是我的英语水平不足以做出这样的判断。
    【解决方案2】:

    解决此错误的一种方法是为您的列分配默认值(尽管这并不总是一种好的做法)。这样,如果您的插入语句中缺少数据,它应该使用默认值。

    虽然我建议您设计一个更新方法的修订版,但正如上面的海报所指出的,您的整个问题来自于您的数组中的一个值是空的,这可以通过适当的数据验证来解决。

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 2021-04-03
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多