Can't get any data from sqlite db on Android

前端 未结 1 1603
萌比男神i
萌比男神i 2021-01-29 08:03

I have a DB helper that does this function:

public Cursor getCourseNames() throws SQLException {
    mDb = mDbHelper.getReadableDatabase();

    return mDb.query         


        
1条回答
  •  温柔的废话
    2021-01-29 08:36

    It should be this

    public Cursor getCourseNames() throws SQLException {
        String[] values = {COURSE_NAME};
        mDb = mDbHelper.getReadableDatabase();
    
        return mDb.query("Course",values,COURSE_ROWID, null, null, null, null, null); 
    }
    

    Explanation :

    the medthod in the api has been defined as

    public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

    So you need to pass the strings accordingly.

    User my example as a reference it works for me

        private String name;
    private String Events_Table = "events";
    private String[] Columns = {"_id", "Name", "Date", "Time_Slot", "Venue", "Details", "EHName", "EHNumber"} ;
    private String WhereClause = Columns[1]+"=?" ;
    
        Cursor cursor = db.query(Events_Table, Columns, WhereClause, new String[] {name}, null, null, null);
    

    Consider Reading this

    Parameters

    table The table name to compile the query against.

    columns A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.

    selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.

    selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

    groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used.

    orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

    0 讨论(0)
提交回复
热议问题