SQLite数据库查询仅返回一行

3

以下是我用来抓取存储在SQLite表中的所有结果的代码。我的问题是,它只返回单个行,而我知道DB中有21行。

public HashMap<String, String> fetchResults(String TABLE_NAME, String sql) {
    HashMap<String, String> table = new HashMap<String, String>();
    if(sql == null)
        sql = "";
    String selectQuery = "SELECT * FROM " + TABLE_NAME + " " + sql;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    String[] columns = null;
    switch(TABLE_NAME ){
    case "projects":
        columns = dbTables.projectsColumns;
        break;
    }



    // Move to first row
    cursor.moveToFirst();
    if (cursor.getCount() > 0) {

        int n=1;
        for(int i=0; i<columns.length; i++){
            table.put(columns[i], cursor.getString(n));
            n++;
        }

    }
    //move to the next row 
    cursor.moveToNext();

    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching " + TABLE_NAME + " from Sqlite: " + table.toString());

    return table;
}

数据库调用在一个片段中进行,代码如下。

HashMap<String, String> projects = db.fetchResults(dbTables.TABLE_PROJECTS, null);


    for (String key : projects.keySet()) {

        if(key == "projectname"){
            System.out.println("Key: " + key + ", Value: " + projects.get(key));
            menuList.add(projects.get(key));
        }
    }
3个回答

4

您在那里只检索了一行数据:

// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
    int n=1;
    for(int i=0; i<columns.length; i++){
        table.put(columns[i], cursor.getString(n));
        n++;
    }
}

您需要迭代游标以获取所有结果:

// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
    while(cursor.moveToNext()) {
        for (int i = 0; i < columns.length; i++) {
            table.put(columns[i], cursor.getString(i));
        }
    }
}

但是你的表格将列出每一行的所有列值。如果你想要一个带有行条目的表格,请定义一个用于存储列值的类。


很抱歉,那似乎没有产生任何影响。 - jampez77
你尝试过在rawQuery之后调试和检查cursor的值吗? - mrtn
是的,我使用了 dumpCursorToString() 方法,它显示了多个结果。 - jampez77
然后尝试检查table,它应该存储了您表中的所有值。 - mrtn

0

我不确定cursor.movetonext()方法是什么,因为我从未使用过它,但我会使用类似这样的方法:

public static ResultSet createRS(String Query,String Server) {


          // Create a variable for the connection string.
          String connectionUrl = "jdbc:sqlserver://"+Server+"\\SQLExpress;databaseName=Schedule;" +
         "integratedSecurity=true;";

          // Declare the JDBC objects.
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;

          try {


             // Establish the connection.
             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
             con = DriverManager.getConnection(connectionUrl);

             // Create and execute an SQL statement that returns a
             // set of data and then display it.
             String SQL = Query;
             stmt = con.createStatement();
             rs = stmt.executeQuery(SQL);
          }

          // Handle any errors that may have occurred.
          catch (Exception e) {
             e.printStackTrace();
          }

          return rs;
       }

有了结果集,您只需要这样做:

try {
        while (rs.next()) {
            current=Double.parseDouble(rs.getString(currentColumn));
        }
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

0
感谢所有回答的人。我已经找到了一个完美运作的函数。
public Cursor getAllRows(String TABLE_NAME, String sql){
    String selectQuery = "SELECT * FROM " + TABLE_NAME + " " + sql;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if(cursor != null){
        cursor.moveToFirst();
    }
    return cursor;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接