使用cast为int的日期格式在sqlite中进行命令查询

时间:2017-07-22 07:41:27

标签: android android-sqlite

我想更改我的查询以应用ASC订单。这是我的查询,它会更改日期格式并在DESC模式下正确排序。

   public Cursor get(String Filter1, String Order1)
{
String Sort="data"; 
if (Order1=="data")
{
    Sort="substr(data, 7, 4), substr(data, 4, 2), substr(data, 1, 2)";
}
else {Sort="players";}

try

{
   String sql3 ="SELECT rowid _id, * FROM players"
        + " WHERE players like ('"+ Filter1 +"')"
        //+ " ORDER BY "+ Sort +"";
        + " ORDER BY "+ Sort +"";

    Cursor mCur = mDb.rawQuery(sql3, null);
     if (mCur!=null)
     {
        mCur.moveToNext();
     }
     return mCur;
 }
 catch (SQLException mSQLException) 
 {
     Log.e(TAG, "getGiocatoriTutti >>"+ mSQLException.toString());
     throw mSQLException;
}

最后添加ASCDESC不起作用。

ORDER BY "+ Sort +" ASC";

更改为:

   Sort="cast (substr(date,7,4) as int) ASC"

仅按年份正确排序。

我试试

Sort="cast (substr(date,7,4), substr(date 4,2), substr(date 1,2) as int) ASC"

但不是正确的sintax。

1 个答案:

答案 0 :(得分:1)

解决了,这是我的解决方案:

Sort="substr(data,7,4)||substr(data,4,2)||substr(data,1,2) DESC";