簡體   English   中英

Vertx executeBatch 不返回所有行

[英]Vertx executeBatch not returning all rows

我正在使用 vertx JDBC 客戶端池並嘗試向表中插入多條記錄。 插入成功,但插入的記錄不返回,而只返回第一條記錄。

使用batchExecute插入多條記錄的代碼

List<Tuple> batch = new ArrayList<>();
batch.add(Tuple.of(36,"st","st"));
batch.add(Tuple.of(36,"st1","st1"));

pool.preparedQuery("INSERT INTO table (col1,col2,col3) VALUES($1, $2, $3) returning *").executeBatch(batch,rowSetAsyncResult -> {     
            System.out.println("size = " + rowSetAsyncResult.result().size()); // this always return 1
            for(Row row:rowSetAsyncResult.result()){
                System.out.println("id = " + row.getInteger("id"));
            }
        });

Output

size = 1
id = 87

表有四列,其中一列是自動遞增的,這就是為什么上面的代碼有 3 列。

我在這里錯過了什么嗎?

嘗試這個:

pool.preparedQuery("INSERT INTO table (col1,col2,col3) VALUES($1, $2, $3) returning id").executeBatch(batch, res -> {
        if (res.succeeded()) { // Process rows
             RowSet<Row> rows = res.result();
             int[] count = new int[] {0};
             while (rows != null) {
                 count[0]++;
                 rows.iterator().forEachRemaining(row -> {
                    System.out.println("id: " + rows.getInteger("id"));});
                 rows = rows.next();
             }
        } else {
            System.out.println("Batch failed " + res.cause());
        }
    });

我知道已經很晚了。 但我會為任何面臨同樣問題的人回答。 下面的代碼示例將解決問題並獲取所有行。

  client
  .preparedQuery("INSERT INTO color (color_name) VALUES ($1) RETURNING color_id")
  .executeBatch(Arrays.asList(Tuple.of("white"), Tuple.of("red"), Tuple.of("blue")))
  .onSuccess(res -> {
    for (RowSet<Row> rows = res;rows.next() != null;rows = rows.next()) {
      Integer colorId = rows.iterator().next().getInteger("color_id");
      System.out.println("generated key: " + colorId);
    }
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM