子查询结果不成功

时间:2013-04-19 07:20:15

标签: java sqlite jdbc subquery

我在sqlite查询结果中遇到子查询问题。执行以下查询成功时,会在Sqlite数据库中返回结果,但在运行程序时,它不会返回任何值。我在下面的程序中做错了什么。请指教。

查询:

select * from (SELECT count(*), col2,col9,col4 FROM tbl_data_1  WHERE col4=6  AND col3 BETWEEN '2012-11-21' AND '2013-04-19' GROUP BY col2,col9,col4) as inr_qry where col2= '20016' and col9='3'

java中的程序:

Vector<Vector> dataList = new Vector();
for (int i =0; i < listRows.size(); i++){
    Vector cols = new Vector();             
    cols.add(listRows.get(i));
    for (int j = 1; j < listColumns.size(); j++){
        // below query will be dynamic
        String inner_query = "select * from (SELECT count(*), col2,col9,col4 FROM tbl_data_1  WHERE col4=6  AND col3 BETWEEN '2012-11-21'               AND '2013-04-19' GROUP BY col2,col9,col4) as inr_qry where col2= '20016' and col9='3'";

        PreparedStatement data_st = database.getConnection().prepareStatement(inner_query);
        ResultSet data_rs = data_st.executeQuery();
        while (data_rs.next())  { // result not successful, result set not having records
            SLogger.printD("data_rs.getString(1): " +data_rs.getString(1));
            cols.add(data_rs.getString(1));
        }
    }
    dataList.add(cols);
}

2 个答案:

答案 0 :(得分:1)

你不需要PreparedStatement。而是使用它:

Statement stmt = database.getConnection().createStatement();

答案 1 :(得分:0)

听起来很奇怪但是作为一个int预期,你可以尝试使用

getInt(1)

而不是

getString(1)