SQLite database query returns only one row

5.8k views Asked by At

Below is the code I'm using to grab all results stored in an SQLite table. The problem I am having is that it is only returning a single 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 made within a fragment and look 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

There are 3 answers

0
jampez77 On BEST ANSWER

Thanks to all who answered. I have found a function that works for perfectly.

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
Cole Gordon On

I am unsure on what the cursor.movetonext() method is because I have never used it, but I would just been using 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;
       }

With the result set you can just do this:

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

You are only retrieving one single row there:

// 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 have to iterate the cursor to get all 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 would list all the column values of every row. If you want to have a table with row-entries, define a class for storing the column values.