列存在时,Java SQLite无法找到列

时间:2016-07-10 21:01:52

标签: java database sqlite

我正在开发一个项目(完整源代码here),作为项目的一部分,我创建了一个Database类,以便更轻松,更清晰地与SQLite数据库连接。我目前正在尝试编写一个方法,它将使用SELECT和给定的参数来返回包含结果的字符串数组。我遇到的问题是,当我运行程序进行测试时,Eclipse会抛出java.sql.SQLException: no such column: 'MOVES'

但是,当我在GUI中查看数据库时,它清楚地显示了我正在尝试访问的列,当我在同一程序中执行sql时,它能够返回数据。

这是我到目前为止编写的方法:

public String[] get(String what, String table, String[] conds) {        
    try {
        if (what.equals("*")) {
            throw new Exception("'*' selector not supported");
        }
        c.setAutoCommit(false);
        stmt = c.createStatement();
        String sql = "SELECT " + what.toUpperCase() + " FROM " + table.toUpperCase();
        if (conds.length > 0) {
            sql += " where ";
            for (int i = 0; i < conds.length; i++) {
                if (i == conds.length - 1) {
                    sql += conds[i];
                    break;
                }
                sql += conds[i] + " AND ";
            }
        }
        sql += ";";
        System.out.println(sql);
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            if (table.toUpperCase().equals("DEX")) {
                String id = "";//rs.getInt("id") + "";
                String species = rs.getString("species");
                String type1 = rs.getString("type1");
                String type2 = rs.getString("type2");
                String hp = rs.getInt("hp") + "";
                String atk = rs.getInt("atk") + "";
                String def = rs.getInt("def") + "";
                String spa = rs.getInt("spa") + "";
                String spd = rs.getInt("spd") + "";
                String spe = rs.getInt("spe") + "";
                String ab1 = rs.getString("ab1");
                String ab2 = rs.getString("ab2");
                String hab = rs.getString("hab");
                String weight = rs.getString("weight");
                return new String[] { id, species, type1, type2, hp, atk, def, spa, spd, spe, ab1, ab2, hab,
                        weight };
            } else if (table.toUpperCase().equals("MOVES")) {
                String name = rs.getString("NAME");
                String flags = rs.getString("FLAGS");
                String type = rs.getString("TYPE");
                String full = rs.getString("LONG");
                String abbr = rs.getString("SHORT");
                String acc = rs.getInt("ACCURACY") + "";
                String base = rs.getInt("BASE") + "";
                String category = rs.getInt("CATEGORY") + "";
                String pp = rs.getInt("PP") + "";
                String priority = rs.getInt("PRIORITY") + "";
                String viable = rs.getInt("VIABLE") + "";
                return new String[] { name, acc, base, category, pp, priority, flags, type, full, abbr, viable };
            } else if (table.toUpperCase().equals("LEARNSETS")) {
                String species = rs.getString("SPECIES");
                String moves = rs.getString("MOVES");
                return new String[] { species, moves };
            } else {
                throw new Exception("Table not found");
            }
        }
        rs.close();
        stmt.close();
        c.close();
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    return null;
}

Screencaps:

Database structure Data from learnsets table SQL query

更新: 我想仔细检查我使用的数据库查看器是不是搞砸了,所以我打开了终端并运行

sqlite3 git/Pokemon/data.db
pragma table_info(MOVES);

收到此回复:

0|SPECIES|TEXT|0||0
1|MOVES|TEXT|0||0

1 个答案:

答案 0 :(得分:0)

最后想出来,对于遇到此问题的其他人,请确保您尝试从结果集中获取的数据实际包含在其中。例如,如果我调用SELECT SPECIES FROM DEX;,结果集不会包含其他内容,例如id,type或任何其他列,则它只包含species列。我不知道为什么花了这么长时间才弄清楚这一点,但是你有它。