列索引超出范围:1,列数:0
Posted
技术标签:
【中文标题】列索引超出范围:1,列数:0【英文标题】:The column index is out of range: 1, number of columns: 0 【发布时间】:2016-08-02 12:40:20 【问题描述】:我更新了 UPSERT_DELETE_JOB_COUNTERS。 现在我认为“upsert”适用于新的查询 =) 或者你怎么看?
问候, 彼得
`private static final String UPSERT_DELETE_JOB_COUNTERS =
"UPDATE rating.delete_job_counters SET delete_count = delete_count + ? WHERE fee_group_id = ? AND delete_job_id = ?; "+
"INSERT INTO rating.delete_job_counters(delete_job_id, fee_group_id, delete_count ) "+
"SELECT ?, ? , ? " +
"WHERE NOT EXISTS (SELECT 1 FROM rating.delete_job_counters WHERE fee_group_id = ? AND delete_job_id = ?); ";
@Override
public Boolean insertIntoDeleteStatistic(final CachedRowSet deletedEntries,
Long deleteJobId,
Transaction transaction) throws SQLException
boolean finish = true;
if (deletedEntries.size() == 0)
return finish;
final Connection connection = transaction.getConnection();
final Timer executeTimer = Timer.start("Insert took ");
try
PreparedStatement pstmt = connection.prepareStatement(UPSERT_DELETE_JOB_COUNTERS);
while (deletedEntries.next())
pstmt.setInt(1, deletedEntries.getInt(2));
pstmt.setInt(2, deletedEntries.getInt(1));
pstmt.setLong(3, deleteJobId);
pstmt.setLong(4, deleteJobId);
pstmt.setInt(5, deletedEntries.getInt(1));
pstmt.setInt(6, deletedEntries.getInt(2));
pstmt.setInt(7, deletedEntries.getInt(1));
pstmt.setInt(8, deletedEntries.getInt(2));
pstmt.execute();
...
`
【问题讨论】:
我相信deleteCdr.getInt(2)
是问题的原因。你确定这个缓存的结果集实际上有两列可用吗?
与您的问题没有直接关系,但您需要关闭Statement
和Connection
。考虑try-with-resources。
` 更新 rating.delete_job_counters SET delete_count = delete_count + ?在哪里 fee_group_id = ?和 delete_job_id = ?;插入到 rating.delete_job_counters(delete_job_id, fee_group_id, delete_count) SELECT ?, ? , ?不存在的地方(从 rating.delete_job_counters 中选择 1,其中 fee_group_id = ?和 delete_job_id = ?); ` 这个查询好吗?我的第一次测试是阳性的。
【参考方案1】:
您不能将参数化的java.sql.PreparedStatement
与DO
语句一起使用。
为什么不使用INSERT ... ON CONFLICT DO UPDATE
(从 PostgreSQL 9.5 开始可用)?
对于旧版本的 PostgreSQL,您可能必须编写一个“无限循环”,如以下伪代码示例:
while (1)
INSERT ...
if (insert went ok) break;
UPDATE ...
if (update changed one row) break;
【讨论】:
我们有 PostgresSql 9.4 =( 你有别的想法吗? 我已经编辑了答案;我想这是你唯一的选择。以上是关于列索引超出范围:1,列数:0的主要内容,如果未能解决你的问题,请参考以下文章
列索引超出范围,3 > 2。嵌套异常是 java.sql.SQLException: