【发布时间】:2017-12-26 18:47:42
【问题描述】:
在我最近的应用程序中,我已将新列 SUB_CATEGORY_ID 添加到现有表 transactions 中。在我将sub_category_id 值作为任何整数插入记录后,当我查询表并检查它返回值为-1 时sub_category_id 其余列都很好。我在插入之前检查了sub_category_id 的值,它不为空并且具有像 1,2 这样的整数值,...但它仍然将值保存为 -1。
任何建议。请在下面找到我的代码。
添加事务的代码:
public int addTransaction(int categoryId, int subCategoryId, int accountId, double amount,
String notes, String trxType, String subTrxType, int sourceTrxId) {
Log.d("addTransaction", "before addTransaction "+subCategoryId);
int flag=0;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_ID_COL, categoryId);
contentValues.put(ACCOUNT_ID_COL, accountId);
contentValues.put(AMOUNT_COL, amount);
contentValues.put(DUE_DATE_COL, getDateTime());
contentValues.put(CREATION_DATE, getDateTime());
contentValues.put(NOTES_COL, notes);
contentValues.put(TRANSACTION_TYPE_COL, trxType);
contentValues.put(TRANSACTION_SUB_TYPE_COL, subTrxType);
contentValues.put(SOURCE_TRX_ID_COL, sourceTrxId);
contentValues.put(SUB_CATEGORY_ID_COL, subCategoryId);
db.insert(TRANSACTIONS_TABLE_NAME, null, contentValues);
flag=1;
return flag;
}
查询交易记录的代码为 sub_category_id 列返回 -1:
String selectQuery1 = "SELECT TRX."+CATEGORY_ID_COL+", TRX."+SUB_CATEGORY_ID_COL+" from "+TRANSACTIONS_TABLE_NAME
+" AS TRX ORDER BY DATETIME(TRX."+CREATION_DATE+") DESC LIMIT 50";
Cursor c1 = db.rawQuery(selectQuery1, null);
if(c1.moveToFirst()){
do{
Log.d("fetchAllTrx", " CetgId="+c1.getLong(0)+"
subcatgid="+c1.getLong(1));
}while (c1.moveToNext());
}
c1.close();
【问题讨论】:
-
当您对数据库进行任何更改时,请卸载旧应用程序并在调试模式下安装新应用程序
标签: android sqlite android-sqlite