我该如何插入新的新Android SQLite数据以及上一行的行数据并更改一个?

时间:2018-11-20 19:53:02

标签: android sql

我正在开发一个利用SQLite数据库的Android应用程序。这是支票簿计划者,我可以在其中输入购买,存款等交易。我正在计划重复收费,并将其与所有当前输入数据一起显示在列表视图中。新数据将获取当月和支票帐户的所有交易记录,并以用户输入所提供的月份为单位,以递增的日期插入数据库。

这是我当前在DBHelper calss中拥有的代码

 public void getTransRepeater(int repeat, int id){
    SQLiteDatabase db = this.getWritableDatabase();
    String whereclause =  TRANSACTION_CHECKING_ID + " = " + id + " AND " + TRANSACTION_REPEAT + " != 'NONE'";
    String[] dateargs = new String[1];
    Cursor res = db.rawQuery(
            "SELECT * FROM " + TABLE_TRANSACTIONS + " WHERE " + TRANSACTION_DATE + " >= " + getCurrentMonth() + " ORDER BY " + TRANSACTION_DATE + " ASC, " + TRANSACTION_ID, null);
    db.beginTransaction();
    res.moveToFirst();

    System.out.println("Paolo before " + getCurrentMonth());
    while (res.moveToNext()){



        if (res.getString(res.getColumnIndex(TRANSACTION_REPEAT)).equals("MONTH")){
            SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyyMMdd");
            String date = res.getString(res.getColumnIndex(TRANSACTION_DATE));
            Date dDate = null;


            try {
                dDate = dateFormat.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar cal = Calendar.getInstance();
            cal.setTime(dDate);
            cal.add(Calendar.MONTH, 1);
            String newDate = dateFormat.format(cal.getTime());
            for (int i = 0; i < repeat; i++){



                ContentValues cv = new ContentValues();
                cv.put(TRANSACTION_NAME, res.getString(res.getColumnIndex(TRANSACTION_NAME)));
                cv.put(TRANSACTION_TYPE, res.getString(res.getColumnIndex(TRANSACTION_TYPE)));
                cv.put(TRANSACTION_DATE, newDate);
                cv.put(TRANSACTION_AMOUNT, res.getInt(res.getColumnIndex(TRANSACTION_AMOUNT)));
                cv.put(TRANSACTION_NOTES, res.getString(res.getColumnIndex(TRANSACTION_NOTES)));
                cv.put(TRANSACTION_REPEAT, res.getString(res.getColumnIndex(TRANSACTION_REPEAT)));
                cv.put(TRANSACTION_CHECKING_ID, res.getInt(res.getColumnIndex(TRANSACTION_CHECKING_ID)));
                cv.put(TRANSACTION_NEW_BALANCE, res.getInt(res.getColumnIndex(TRANSACTION_NEW_BALANCE)));
                cv.put(TRANSACTION_CREDIT_ID, res.getString(res.getColumnIndex(TRANSACTION_CREDIT_ID)));
                db.insert(TABLE_TRANSACTIONS, null, cv);


            }

        }
    }
    res.close();
    db.setTransactionSuccessful();
    db.endTransaction();

}

目前,我可以使交易重复进行,但是对于每次迭代而言,日期仅相差1个月,并且它适用于数据库中的所有交易,不适用于指定的帐号。

0 个答案:

没有答案