如果我的表有4个列,并且我想获取第3列,该怎么办. [英] if my table has 4 colums and i want to fetch the 3rd column what do i do.

查看:101
本文介绍了如果我的表有4个列,并且我想获取第3列,该怎么办.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我桌子上的列子

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

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

with getString(0);它以此方法返回列表的大小

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;

但是它没有在其他活动中显示它.它显示了空字符串,我已经使用Toast对其进行了检查.该活动的代码是

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();

    }

}

推荐答案

重要的方面不是表,而是查询本身,它决定了列的顺序.

例如,如果查询基于SELECT * FROM your_table(并且表中的列定义为id,songname,songyear,songpath),则按定义的列就是光标.

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.

但是,如果您做了SELECT songname, songpath, songid, songyear FROM your_table;

顺序将按照 SELECT 语句进行,即歌曲名称(0),歌曲路径(1),歌曲ID(2),歌曲年份(3).

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

但是,这是使用偏移量的问题之一,按照SELECT,您将偏移量与订单联系在一起.

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

现在,如果您使用Cursor的 getColumnIndex 方法,则根据其名称返回列偏移.

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

因此cursor.getString(cursor.getColumnIndex("songpath"));将获得songpath列,而不管其在游标中的偏移量/位置(如果光标包含该列).

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).

回顾上一个问题,您基本上有SELECT songpath FROM your_table,因此结果游标中只有一列,因此您只能使用get之一:-

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

或:-

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;

这将返回一个名为myconfusingcolumn的单列,该列由与歌曲名串联的歌曲路径组成.也就是说,AS关键字后面是该列的别名(没有AS时,列名将更加混乱/困难,因为它是songpath || songname)(此示例相对简单).

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).

要警惕的另一件事是,例如,歧义列(重复的列名),如果您有两个表song和artist,并且在另外的列artist_id上添加了歌曲,那么您就有:-

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 :-

歌曲表,其中包含 id 歌曲名称歌曲年份歌曲路径 artist_id

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

艺术家表,其中包含 id 列和 artist_name

然后您使用

SELECT * FROM song JOIN artist ON song.artist_id = artist.id;

  • 请注意,作为 artist 表的 id 列,如果在语句中使用该表,则必须在其前面加上相应的表名,否则将出现AMBIGUOUS列错误被引发,即SQL解析器将不知道您的意思是 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 ,歌曲名称,歌曲年份,歌曲路径,artist_id, id ,artist_name

      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).
      

      回顾/总结:-

      游标中的列,顺序和名称完全取决于SELECT查询.根据查询,它们将具有偏移量和名称.当访问游标时,仅表名或偏移量不能使用基础表名.

      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.

      按列名而不是按对象名访问列更灵活.那是因为使用了Cursor的 getColumnIndex 方法,因为它不需要计算偏移量,尤其是在更改查询时缺少重新计算的情况.

      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.

      对列名称使用常量可能会减少诸如键入错误之类的问题.

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

      将得到异常结果[{}],因为mListSongs是Songs的整个容器.您需要遍历每个元素,然后从元素(Song对象)获取每个成员/变量的属性/值.

      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).

      这篇关于如果我的表有4个列,并且我想获取第3列,该怎么办.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆