strftime(('%Y-%m',tr_date)在SQlite android中不起作用

时间:2014-01-29 09:05:57

标签: android sqlite datetime

您好朋友,我希望在我的应用程序中按月显示数据,如下所示

public class IncomeActivity extends ListActivity {

Button mbutoonmonth;
 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_income);
    DatabaseConnectionAPI mDatabaseConnectionAPI=new DatabaseConnectionAPI(getApplicationContext());
mTextViewMonth.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String StartDate="";


            int mYear = 0;
            int mMonth;
            int mDay;
            int dayOFWeek = 0;
            String mondayDate="";
            Calendar mCalendar = Calendar.getInstance();
            mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

            mCalendar.add(Calendar.DATE, 0);

            mYear = mCalendar.get(Calendar.YEAR);
            mMonth = mCalendar.get(Calendar.MONTH) + 1;
            mDay = mCalendar.get(Calendar.DAY_OF_MONTH);

            if (mMonth==10|mMonth==11|mMonth==12) {
                 StartDate=String.valueOf(mYear)+"-"+String.valueOf(mMonth);
                System.out.println("Start DAte "+StartDate);
            }
            else {
                 StartDate=String.valueOf(mYear)+"-0"+String.valueOf(mMonth);
                System.out.println("Start DAte "+StartDate);
            }

            mArrayListTransactions=mDatabaseConnectionAPI.getIncomeMonthData(mStringTrMode,StartDate);
            mCustomListviewAdapter=new CustomListviewAdapter(IncomeActivity.this, mArrayListTransactions);
            mListView.setAdapter(mCustomListviewAdapter);
        }
    });
   }
}

DatabaseConnectionAPI.java

 public ArrayList<ParsesrTransaction> getIncomeMonthData(String mode ,String date) {

    ArrayList<ParsesrTransaction> mGetCategoryList = new ArrayList<ParsesrTransaction>();
    try {
        String sqlQuery = "select tr_date  from Transaction_Master where strftime('%Y-%m', tr_date) = "+ "'"+date+"'" ;
        Cursor mCursorCategory = Query(sqlQuery);
        if (mCursorCategory != null) {
            mCursorCategory.moveToFirst();
            while (!mCursorCategory.isAfterLast()) {

                ParsesrTransaction mParserCategory = new ParsesrTransaction();
                mParserCategory
                .setCatID(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("cat_id")));
                mParserCategory
                .setCatName(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("cat_name")));
                mParserCategory
                .setTrDate(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("tr_date")));
                mParserCategory
                .setTrDetail(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("tr_detail")));
                mParserCategory
                .setTrMode(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("tr_mode")));
                mParserCategory
                .setTrPrice(mCursorCategory.getString(mCursorCategory
                        .getColumnIndex("tr_price")));
                mGetCategoryList.add(mParserCategory);
                mCursorCategory.moveToNext();
            }
        }
        mCursorCategory.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mGetCategoryList;
}

当我运行abouve代码时,它会给我一些消息,如

01-29 14:27:11.890: E/CursorWindow(9965): Bad request for field slot 0,-1. numRows = 3, numColumns = 1
01-29 14:27:11.890: W/System.err(9965): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
01-29 14:27:11.890: W/System.err(9965):     at android.database.CursorWindow.getString_native(Native Method)
01-29 14:27:11.900: W/System.err(9965):     at android.database.CursorWindow.getString(CursorWindow.java:329)
01-29 14:27:11.900: W/System.err(9965):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
01-29 14:27:11.900: W/System.err(9965):     at pkg.android.rootways.rootmoney.helper.DatabaseConnectionAPI.getIncomeMonthData(DatabaseConnectionAPI.java:646)
01-29 14:27:11.900: W/System.err(9965):     at pkg.android.rootways.rootmoney.IncomeActivity$9.onClick(IncomeActivity.java:268)

我知道如何解决它?

1 个答案:

答案 0 :(得分:2)

您只是将tr_date列投影到光标,但也尝试访问其他五个列。对于光标中不存在的列,getColumnIndex()返回-1。

可能SELECT *代替SELECT tr_date