我试图使用我创建的3种不同的DatabaseHelper方法来从2个表->“支出”和“交易”表中检索不同的数据。我必须在Main类的某个方法中使用所有这3种Cursor方法,并且我不知道覆盖Cursor cuz的正确方法,因为我已经使用了这些数据库方法,例如myDb.getDeletedCategory,myDb.getLatestAmount等。因此,我认为主要的问题是如何在“ deletedcategory”之后定义我的游标,以及在使用一个:-
之后“覆盖”或重新定义游标的正确方法是什么?Cursor deletedcategory = myDb.getDeletedCategory(transaction_id, position);
if (deletedcategory.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No category found !", Toast.LENGTH_LONG).show();
} else {
deletedcategory.moveToFirst();
deletedcategoryis = deletedcategory.getString(0);
}
Cursor latestamount = myDb.getLatestAmount(position, deletedcategoryis);
if (latestamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "No amount found in specific category !", Toast.LENGTH_LONG).show();
} else {
latestamount.moveToFirst();
amountfromdb_ofcategory = latestamount.getDouble(0);
}
Cursor deletedamount = myDb.getDeletedAmount(transaction_id, position);
if (deletedamount.getCount() == 0) {
Toast.makeText(AddExpenses.this, "Deleted amount not found !", Toast.LENGTH_LONG).show();
} else {
deletedamount.moveToFirst();
amount_deleted = deletedamount.getDouble(0);
}
答案 0 :(得分:0)
您可以从字面上覆盖现有游标。
例如,考虑以下数据库助手(带有3个返回游标的方法):-
public class DBHelper01 extends SQLiteOpenHelper {
public static final String DBNAME = "db01";
public static final int DBVERSION = 1;
public static final String TABLE_SPENDING = "spending";
public static final String TABLE_TRANSACTIONS = "transactions";
public static final String SPENDING_COL_ID = BaseColumns._ID;
public static final String SPENDING_COL_AMOUNT = "amount";
public static final String SPENDING_COL_CATEGORY = "catetgory";
public static final String TRANSACTIONS_COL_ID = BaseColumns._ID;
public static final String TRANSACTIONS_COl_DATE = "date";
public static final String TRANSACTION_COL_AMOUNT = "amount";
public static final String TRANSACTION_TYPE = "type";
public static final int TRANSACTION_TYPE_ADD = 0;
public static final int TRANSACTION_TYPE_DELETE = 1;
public static final int TRANSACTION_TYPE_UPDATE = 2;
String crt_spending_tbl = "CREATE TABLE IF NOT EXISTS " + TABLE_SPENDING + "(" +
SPENDING_COL_ID + " INTEGER PRIMARY KEY, " +
SPENDING_COL_AMOUNT + " REAL DEFAULT 0.0," +
SPENDING_COL_CATEGORY + " TEXT" +
")";
String crt_transactions_tbl = "CREATE TABLE IF NOT EXISTS " + TABLE_TRANSACTIONS + "(" +
TRANSACTIONS_COL_ID + " INTEGER PRIMARY KEY," +
TRANSACTIONS_COl_DATE + " TEXT DEFAULT CURRENT_TIMESTAMP," +
TRANSACTION_COL_AMOUNT + " REAL," +
TRANSACTION_TYPE + " INTEGER" +
")";
SQLiteDatabase mDB;
public DBHelper01(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
@Override
public synchronized void close() {
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(crt_spending_tbl);
db.execSQL(crt_transactions_tbl);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public long addSpending(String category, Double amount) {
ContentValues cv = new ContentValues();
cv.put(SPENDING_COL_CATEGORY,category);
cv.put(SPENDING_COL_AMOUNT,amount);
return mDB.insert(TABLE_SPENDING,null,cv);
}
public long addTransactions(Double amount, int transaction_type ) {
ContentValues cv = new ContentValues();
cv.put(TRANSACTION_COL_AMOUNT,amount);
cv.put(TRANSACTION_TYPE,transaction_type);
return mDB.insert(TABLE_TRANSACTIONS,null,cv);
}
public Cursor getLatestTransaction() {
return mDB.query(TABLE_TRANSACTIONS,null,null,null,null,null,TRANSACTIONS_COl_DATE + " DESC","1");
}
public Cursor getTotalTransactionAmount() {
String[] columns = new String[]{"sum(" + TRANSACTION_COL_AMOUNT + ") AS summed_amounts" };
return mDB.query(TABLE_TRANSACTIONS,columns,null,null,null,null,null);
}
public Cursor getAllTransactions() {
return mDB.query(TABLE_TRANSACTIONS,null,null,null,null,null,TRANSACTIONS_COl_DATE + " ASC");
}
}
和下面的Activity(在最初添加一些数据之后)调用3个方法,然后在每次使用后转储Cursor:-
public class MainActivity extends AppCompatActivity {
DBHelper01 mDBHlpr;
Cursor mCsr; //<<<<<<<<<< THE CURSOR THAT WILL BE OVERIDDEN
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDBHlpr = new DBHelper01(this);
mDBHlpr.addSpending("TEST",123.45);
mDBHlpr.addTransactions(133.42,DBHelper01.TRANSACTION_TYPE_ADD);
mDBHlpr.addTransactions(127.45,DBHelper01.TRANSACTION_TYPE_DELETE);
mDBHlpr.addTransactions(111.11,DBHelper01.TRANSACTION_TYPE_UPDATE);
mDBHlpr.addTransactions(133.42,DBHelper01.TRANSACTION_TYPE_ADD);
mDBHlpr.addTransactions(127.45,DBHelper01.TRANSACTION_TYPE_DELETE);
mDBHlpr.addTransactions(111.11,DBHelper01.TRANSACTION_TYPE_UPDATE);
mCsr = mDBHlpr.getAllTransactions(); //<<<<<<<<<< First write to cursor
DatabaseUtils.dumpCursor(mCsr);
mCsr = mDBHlpr.getLatestTransaction(); //<<<<<<<<<< Overwrites the Cursor
DatabaseUtils.dumpCursor(mCsr);
mCsr = mDBHlpr.getTotalTransactionAmount(); // Overwrites the Cursor again
DatabaseUtils.dumpCursor(mCsr);
mCsr.close();
}
}
结果输出(确认每个游标都不同):-
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5345bd2c
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=1
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 1 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=2
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=127.45
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=1
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 2 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=3
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=111.11
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=2
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: 3 {
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: _id=4
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.937 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 4 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=5
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=127.45
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=1
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 5 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=6
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=111.11
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=2
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5342f0e4
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: _id=1
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: date=2018-10-30 11:17:31
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: amount=133.42
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: type=0
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@5342f6e8
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: 0 {
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: summed_amounts=743.96
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: }
10-30 11:17:31.941 1252-1252/axtest.axtest I/System.out: <<<<<