mysql select, insert and delete works from java program, but update not working

空扰寡人 提交于 2019-12-04 05:06:49

问题


I have a table with primary key id, select, insert and delete queries all work from java program, but update query not working, so as to 'insert on duplicate update'( only works when record doesn't exist, when record exists, the updating won't work).

All queries committed, and my mariadb version is 10.1.14.

Thanks in advance for any help!

All queries works well in mysql-cli.

table schema

+------------------+----------------------+------+-----+---------------------+-----------------------------+
| Field            | Type                 | Null | Key | Default             | Extra                       |
+------------------+----------------------+------+-----+---------------------+-----------------------------+
| id               | int(10) unsigned     | NO   | PRI | NULL                |                             |
| posng_valid_type | tinyint(3) unsigned  | YES  |     | NULL                |                             |
| longitude        | double(9,6)          | NO   |     | 0.000000            |                             |
| latitude         | double(9,6)          | NO   |     | 0.000000            |                             |
| heading          | smallint(6)          | NO   |     | 0                   |                             |
| altitude         | float(7,3)           | NO   |     | 0.000               |                             |
| gps_speed        | smallint(5) unsigned | NO   |     | 0                   |                             |
| sample_time      | timestamp            | NO   |     | 0000-00-00 00:00:00 |                             |
| update_time      | timestamp            | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
+------------------+----------------------+------+-----+---------------------+-----------------------------+

1. update query

update `status_position` 
set `status_position`.`id` = 3007,  
    `status_position`.`posng_valid_type` = 0, 
    `status_position`.`longitude` = 121.1921, 
    `status_position`.`latitude` = 31.2797, 
    `status_position`.`heading` = -1, 
    `status_position`.`altitude` = 0.0, 
    `status_position`.`gps_speed` = 0, 
    `status_position`.`sample_time` = timestamp '2017-02-15 03:52:23.0'   
where `status_position`.`id` = 3007;

2. update on duplicate query

insert into `status_position` (
    `id`, 
    `posng_valid_type`, 
    `longitude`, 
    `latitude`, 
    `heading`, 
    `altitude`, 
    `gps_speed`, 
    `sample_time`
 ) values (
    2001, 
    0, 
    121.1921, 
    31.2797, 
    -1, 
    0.0, 
    0, 
    timestamp '2017-02-15 03:52:23.0'
) on duplicate key update 
    `status_position`.`id` = 2001, 
    `status_position`.`posng_valid_type` = 0, 
    `status_position`.`longitude` = 121.1921, 
    `status_position`.`latitude` = 31.2797, 
    `status_position`.`heading` = -1, 
    `status_position`.`altitude` = 0.0, 
    `status_position`.`gps_speed` = 0, 
    `status_position`.`sample_time` = timestamp '2017-02-15 03:52:23.0';

java code with JOOQ which generates the query 2

public <R extends Record> void batchUpsertRecord(Table<R> table, List<R> records) throws PersistenceException {
        Connection conn = ConnectionPoolManager.INSTANCE.getConnection();

            try (DSLContext dslContext = DSL.using(conn, SQLDialect.MARIADB)) {
                List<InsertQuery> insertQueryList = new ArrayList<>();
                for (R record : records) {
                    InsertQuery<R> insert = dslContext.insertQuery(table);
                    insert.addRecord(record);
                    insert.onDuplicateKeyUpdate(true);
                    insert.addValuesForUpdate(mapOfChangedValues(record));
                    insertQueryList.add(insert);
                }
                dslContext.batch(insertQueryList).execute();
                conn.commit();
            } catch (SQLException e) {
                logger.error("Failed to upsert record into table({}).", table.getName(), e);
            } finally {
                ConnectionPoolManager.INSTANCE.closeConnection(conn, logger);
            }
    }

来源:https://stackoverflow.com/questions/42268739/mysql-select-insert-and-delete-works-from-java-program-but-update-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!