SQLite database query only returns one row

Below is the code I am using to get all the results stored in a SQLite table. The problem I'm running into is that it only returns one row when I know there are 21 rows in the DB.

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;
}

      

The db call is done inside a fragment and looks like this.

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


source to share


3 answers


Thanks to all who responded. I found a function that works great.



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;
}

      

0


source


You are only fetching one line:

// 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++;
    }
}

      

You need to iterate over the cursor to get all the results:



// 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));
        }
    }
}

      

But your table will list all the column values ​​for each row. If you want to have a table with rows, define a class to store the column values.

+4


source


I'm not sure what the cursor.movetonext () method is because I never used it, but I would just use a method like this:

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;
       }

      

In the result set, you can simply do this:

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

      

0


source







All Articles