Return all columns of a SQLite table in Android

前端 未结 3 1587
天命终不由人
天命终不由人 2020-12-28 13:33

The SQLite database table I\'m using is subjected to adding columns whenever I need to. I don\'t want to hard-code the columns at the risk of updating the database and forge

3条回答
  •  清歌不尽
    2020-12-28 13:53

    You may not need a list of column names.

    It seems that you want the list of column names so that you can build up a comma-separated list of columns to select. If this is the case, then where you would normally place the list of column names in your SELECT query, you could instead use an asterisk:

    SELECT * FROM table WHERE ...
    

    Or if the table is aliased:

    SELECT t.* FROM table AS t ...
    

    An asterisk means "all columns".

    EDIT: If you really do want a list of column names of a table, then you can use the following code:

    Cursor c = db.rawQuery("SELECT * FROM table WHERE 0", null);
    try {
        String[] columnNames = c.columnNames();
    } finally {
        c.close();
    }
    

    Note that you must not modify the returned array of Strings in any way: https://code.google.com/p/android/issues/detail?id=3731

提交回复
热议问题