Sqlite插入无法正常工作

时间:2015-11-07 16:51:18

标签: java android sqlite

第一次在这里编写Android应用程序。

我的使用案例如下:在一项活动中,我有TextView显示摘要(金额总和),Button显示EditText。想法是按下按钮会在sqlite数据库中添加一个条目。

问题是,每次按下按钮时,似乎输入的先前值都被添加到数据库中。

  1. 输入10 - >按下按钮 - > 没有反应
  2. 输入7 - >按下按钮 - > 摘要更改10
  3. 输入3 - >按下按钮 - > 摘要更改为7
  4. 这是OnClickListener中使用的方法:

    public static void addExpense(Context ctx, Expense expense) {
            ExpensesDbHelper dbHelper = new ExpensesDbHelper(ctx);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
    
    
            ContentValues values = expense.toContentValues();
            Log.i("Daily-", "addExpense: " + values.toString());
    
            long newRowId;
            db.beginTransaction();
            try {
                newRowId = db.insert(
                        ExpensesReaderContract.ExpenseEntry.TABLE_NAME,
                        null,
                        values);
    
                db.setTransactionSuccessful();
            } finally{
                db.endTransaction();
            }
    
    
            Log.i("Daily-", "addExpense: new id " + newRowId);
            Cursor c = db.rawQuery("SELECT COUNT(*) from entry", null);
            c.moveToFirst();
            Log.i("Daily-", "Number of rows: " + c.getInt(0));
            c.close();
    
            Log.i("Daily-", "addExpense: Existing " + Expense.printable(getExpenses(db)));
        }
    

    这里是来自该输入序列的日志语句:

    I/Daily-: addExpense: timestamp=2015-11-07 16:44:38 comment=Expense amount=10.0
    I/Daily-: addExpense: new id 1
    I/Daily-: Number of rows: 1
    I/Daily-: addExpense: Existing 
    I/Daely-Log: 9
    I/Daily-: addExpense: timestamp=2015-11-07 16:44:41 comment=Expense amount=7.0
    I/Daily-: addExpense: new id 2
    I/Daily-: Number of rows: 2
    I/Daily-: addExpense: Existing Expense(10.000000,Expense,2015-11-07T16:44:38.000Z), 
    I/Daely-Log: 9
    I/Daily-: addExpense: timestamp=2015-11-07 16:44:44 comment=Expense amount=3.0
    I/Daily-: addExpense: new id 3
    I/Daily-: Number of rows: 3
    I/Daily-: addExpense: Existing Expense(7.000000,Expense,2015-11-07T16:44:41.000Z), Expense(10.000000,Expense,2015-11-07T16:44:38.000Z), 
    

    这里是来自处理事件流的活动的代码:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_summary);
            showAvailable();
    
            Button addExpenseButton = (Button) findViewById(R.id.add_expense);
            final EditText amountField = (EditText) findViewById(R.id.amount);
    
            addExpenseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    try {
                        float amount = Float.parseFloat(amountField.getText().toString());
                        addExpense(amount, "Expense");
    
                        showAvailable();
                    } catch (NumberFormatException e) {
    
                    }
                }
    
            });
        }
    

    现在很明显,在db.insert返回后,该行仍然不在应该的位置。

    我如何保证下次调用getExpenses肯定会返回新行?

    感谢您的帮助。

    更新Expense是一个非常简单的类:

    public class Expense {
        public static DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
        private float amount;
    
        public float getAmount() {
            return amount;
        }
    
        public String getComment() {
            return comment;
        }
    
        public DateTime getTimestamp() {
            return timestamp;
        }
    
        private String comment;
        private DateTime timestamp;
    
        public Expense(float amount, String comment, DateTime timestamp) {
            this.amount = amount;
            this.comment = comment;
            this.timestamp = timestamp;
        }
        ...
         public ContentValues toContentValues() {
            ContentValues values = new ContentValues();
            values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_AMOUNT, getAmount());
            values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_COMMENT, getComment());
            values.put(ExpensesReaderContract.ExpenseEntry.COLUMN_NAME_TIMESTAMP, iso8601Format.format(getTimestamp().toDate()));
            return values;
        }
    }
    

1 个答案:

答案 0 :(得分:0)

您的日志表明插入确实确实有效 - 您正在获取正确的COUNT(*),不是吗?我会更关注Expense.printable()getExpenses()中的问题,但是当您发布代码时,您似乎已经删除了部分代码。最有可能的是,你在那里的某处丢了一排。

如果我猜测,你在getExpenses()方法中做了类似的事情:

if (cursor.moveToFirst()) {
    while(cursor.moveToNext()) {
        <stuff>
    }
}

这会让你错过第一排。如果这证明是您的问题,只需跳过moveToFirst()调用,或将循环更改为do-while。