SQLite数据库查询只返回一行

时间:2015-06-09 15:36:35

标签: java android database sqlite cursor

以下是我用来获取存储在SQLite表中的所有结果的代码。我遇到的问题是,当我知道数据库中有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;
}

db调用是在片段内进行的,看起来像这样。

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 个答案:

答案 0 :(得分: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));
        }
    }
}

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

答案 1 :(得分: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();
    }

答案 2 :(得分: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;
}