简体   繁体   English

使用 JDBC 的不明确列,但查询在数据库中工作正常

[英]Ambiguous column using JDBC but query works fine in database

I am connecting to a SQLite database through java using JDBC.我正在使用 JDBC 通过 java 连接到 SQLite 数据库。

Schema:架构:

WorkInfo(id, job, salary)
Person(id, name)

This query below runs fine in my database, but when I try it with JDBC:下面的查询在我的数据库中运行良好,但是当我尝试使用 JDBC 时:

ResultSet rs = statement.executeQuery("select * from Person join workInfo on (Person.id=WorkInfo.id)");
while(rs.next()){ 
    System.out.println("id: " + rs.getInt("Person.id")); //column does not exist
    System.out.println("name: " + rs.getString("name")); //works fine

Output: Output:

If using person.id: no such column: 'person.id'如果使用 person.id: no such column: 'person.id'

Without specifying: ambiguous column name 'id'未指定: ambiguous column name 'id'

I've tried using both WorkInfo and Person and using aliases but it keeps throwing the same ambigious column name (if left as id) or column does not exist.我试过同时使用 WorkInfo 和 Person 并使用别名,但它一直抛出相同的模糊列名(如果保留为 id)或列不存在。

It's always a good practice to explicitly retrieve the columns you want. 显式检索所需的列始终是一个好习惯。 I would change the query to be: 我会将查询更改为:

ResultSet rs = statement.executeQuery("select info.id, info.job, info.salary, "
    + "person.id, person.name from Person person join workInfo info "
    + "on person.id=info.id");
while(rs.next()){ 
    System.out.println("id: " + rs.getInt(4));
    System.out.println("name: " + rs.getString(5));

In this case, you can use the column index instead of the label. 在这种情况下,您可以使用列索引而不是标签。

Or using the AS clause: 或者使用AS子句:

ResultSet rs = statement.executeQuery("select info.id, info.job, info.salary, "
    + "person.id as personId, person.name as personName "
    + "from Person person join workInfo info "
    + "on person.id=info.id");
while(rs.next()){ 
    System.out.println("id: " + rs.getInt("personId"));
    System.out.println("name: " + rs.getString("personName"));

After a day of working on this, I achieved it by using resultSet.getMetaData().经过一天的努力,我通过使用 resultSet.getMetaData() 实现了它。

private int getIndexFromMeta(String column) {
    try {
        ResultSetMetaData meta = resultSet.getMetaData();
        String[] subs = column.split("\\.", -1);
        String tableName = subs[0];
        String columnName = subs[1];

        for (int i = 1; i <= meta.getColumnCount(); i++) {
            if (meta.getTableName(i).equals(tableName) && meta.getColumnName(i).equals(columnName)) {
                return i;
            }
        }
    } catch (SQLException e) {
        Log.trace(e);
    }

    return 0;
}

It seems like the ResultSet you're getting back holds the following columns: 看起来你要回来的ResultSet包含以下列:

  • id ID
  • name 名称
  • id ID
  • job 工作
  • salary 薪水

You have two columns named "id" (none named "Person.id"), so when you try to get its' value you either 你有两个名为“id”的列(没有一个名为“Person.id”),所以当你试图获得它的'值'时

  • Ask for "id" which is ambiguous (which id?) 要求“id”是不明确的(哪个id?)
  • Ask for "Person.id" which does not exist 询问不存在的“Person.id”

Simply try specifying in your query the columns you want and giving them unique aliases. 只需在查询中指定所需的列并为其指定唯一的别名即可。 For example: 例如:

ResultSet rs = statement.executeQuery("select Person.id AS 'personid', name from Person join workInfo on (Person.id=WorkInfo.id)");
while(rs.next()){ 
    System.out.println("id: " + rs.getInt("personid"));
    System.out.println("name: " + rs.getString("name"));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM