if my table has 4 colums and i want to fetch the 3rd column what do i do.

安稳与你 提交于 2019-12-24 08:25:31

问题


here is colums in my table

   Song song = new Song();
    song.setMsong_id(cursor.getLong(0));
    song.setmSong_name(cursor.getString(1));
    song.setmSong_year(cursor.getLong(2));
    song.setmSong_path(cursor.getString(3));

now i want to see the 3rd column so should i write this code

 song.setmSong_name(cursor.getString(0));

or

song.setmSong_name(cursor.getString(3));

with getString(0); it is returning the the size of list as this method

 private Song cursorToSelectSong(Cursor cursor) {
    Song song = new Song();
    song.setmSong_name(cursor.getString(0));
    return song;

but it is not displaying it in other activity.it shows the empty string, i've checked it by using the Toast.code of that activity is

 mSongDao = new SongDAO(this);
    mListSongs = mSongDao.getSelectedSongs(artist_id);
    if (mAdapter == null) {
        mAdapter = new ListSongsAdapter(this, mListSongs);
        mListviewSongs.setAdapter(mAdapter);
        Toast.makeText(this, mListSongs+"", Toast.LENGTH_SHORT).show();

    }

}

回答1:


The important aspect is not the table or tables but the query itself as that determines the column order.

For example if the query were based upon SELECT * FROM your_table (and the columns in the table were defined as id, songname, songyear, songpath) then the columnd is the cursor would be as per the definition.

However, if you did SELECT songname, songpath, songid, songyear FROM your_table;

The the order would be as per the SELECT statement i.e. songname (0), songpath (1), songid (2), songyear (3).

However, that is one of the issues with using offsets you are tied to the order as per the SELECT.

Now if you used the Cursor's getColumnIndex method then that returns the column offset according to it's name.

So cursor.getString(cursor.getColumnIndex("songpath")); would get the songpath column irrespective of it's offset/position in the Cursor (IF THE CURSOR INCLUDES THAT COLUMN).

Recalling your previous question you basically had SELECT songpath FROM your_table, As such there is just a single column in the resultant Cursor so you could only use get either :-

cursor.getString(0); 

or :-

cursor.getString(cursor.getColumnIndex("songpath"));

The latter is the recommended method (BUT ideally have the column names defined as constants)

Matters can get more complicated though, for example consider

SELECT songpath||songname AS myconfusingcolumn FROM yourtable;

This would return a single column named myconfusingcolumn that consists of the songpath concatenated with the songname. That is the AS keyword is followed by an alias for the column (without the AS the column name would be even more confusing/difficult as it would be songpath||songname) (this example is relatively simple).

Another thing to be wary of is, are ambiguous columns (duplicated column names) for example, if you had two tables song and artist and song the the additional column artist_id so you have :-

The song table with columns id, songname, songyear, songpath, artist_id

The artists table with columns id and artist_name

and you then used

SELECT * FROM song JOIN artist ON song.artist_id = artist.id;
  • Note that as the id column of the artist table, if used in the statement, it has to be prefixed with the respective table name otherwise an AMBIGUOUS column error would be raised i.e. the SQL parser wouldn't know which id column you mean.

Furthermore you would end up with a Cursor having columns :-

id, songname, songyear, songpath, artist_id, id, artist_name

csr.getLong(csr.getColumnIndex("id")); could get confused (I believe it actually gets the last AMBIGUOUS column)

long songid = csr.getLong(0); would get the id column from the song table.
long artistid = csr.getLong(5); would get the id column from the artist table.
long artistid = csr.getLong(4); would also get the artist_id same as (5).

To recap/summarise :-

The columns, order and name, in a Cursor are wholly dependant upon the SELECT query. They will have an offset and name as per the query. When accessing the Cursor the underlying table name(s) are not usable just the column names or offsets.

It is more flexible to access columns by their names rather than by their offstes. That is make use of the Cursor's getColumnIndex method as it negates the need to calculate offsets and more especially missing re-calculation should a query be changed.

Using CONSTANTS for column names will likely result in reduced issues such as typing errors.

Additional

Using Toast.makeText(this, mListSongs+"", Toast.LENGTH_SHORT).show();

Will get the unusual result [{}] because mListSongs is the entire container for the Songs. You need to loop though each element and then get the properties/values for each member/variable from the element(Song object).



来源:https://stackoverflow.com/questions/53541336/if-my-table-has-4-colums-and-i-want-to-fetch-the-3rd-column-what-do-i-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!