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



你好,朋友,我要在我的代码中以下面的代码显示

在我的代码中显示数据
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;
}

当我运行代码时,它会给我留言,例如

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)

任何想法如何解决?

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

可能是SELECT *而不是SELECT tr_date会起作用。

最新更新