Android SQLite - 首先插入的值总是丢失

时间:2014-06-01 03:13:19

标签: android sqlite

这有点令人毛骨悚然 - 首先插入的数据总是丢失。

我做什么 - 我总是丢失我的第一笔交易细节。 同样如下所示我添加了两次交易 - 但是当我得到交易表的返回结果大小时,它总是1 - 而不是2。

如果我做错了,请告诉我?

提前致谢

我按如下方式添加交易:

transactionHelper th = new transactionHelper(this.activity.getApplicationContext());
values.clear();
values = new ContentValues();

values.put("accountid", accountID );
values.put("amount", totalBalance);
values.put("transactiontype", 1);
values.put("checknumber", "");
values.put("checkdate", transactionDate);
values.put("cleared", 1);
values.put("notes", "Starting Balance");
values.put("categoryid", startCat);

boolean added = th.addTransaction(values);
Log.v("jv", "Is this added or not: " + added);

th.addTransaction(values);
Log.v("jv", "nothing added: " + th.getAll().size());

我的班级看起来像:

public transactionHelper(Context context) { super(context, DATABASE_NAME , null, 1); }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "CREATE TABLE IF NOT EXISTS transactions ( " +
                "transactionid INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "accountid INTEGER, "+
                "amount REAL, " +
                "transactiontype INTEGER, " +
                "checknumber TEXT, " +
                "checkdate TEXT, " +
                "cleared INTEGER," +
                "notes TEXT," +
                "categoryid INTEGER)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL("DROP TABLE IF EXISTS transactions");
         onCreate(db);
    }

    public boolean addTransaction(ContentValues values) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.insert("transactions", null, values);
        return true;
    }

    public boolean updateTransaction(Integer id, ContentValues values) {
        try {
            SQLiteDatabase db = this.getWritableDatabase();
            db.update("transactions", values, "transactionid = ? ", new String[] { Integer.toString(id) } );
        } catch(Exception e) { Log.v("jv", "Exception in Update Contact: " + e.toString()); return false;}
        return true;
    }

    public int deleteTransaction(Integer id) {
        int numDeleted = 0;

        try {
            SQLiteDatabase db = this.getWritableDatabase();
            numDeleted = db.delete("transactions","transactionid = ? ", new String[] { Integer.toString(id) });
        } catch(Exception e) { Log.v("jv", "Exception in deleteTransaction: " + e.toString()); }


        return numDeleted;
    }

    public cTransactions getTransaction(int id){
         cTransactions  ct = new cTransactions();

         try {
             SQLiteDatabase db = this.getReadableDatabase();
             Cursor cur =  db.rawQuery( "select * from transactions where transactionid="+id+"", null );

             if( cur.moveToFirst() ) {
                ct.setTransactionID(cur.getInt(0));
                ct.setAccountID(cur.getInt(1));
                ct.setAmount(cur.getDouble(2));
                ct.setTransactionType(cur.getInt(3));
                ct.setCheckNumber(cur.getString(4));
                ct.setCheckDate(cur.getString(5));
                ct.setCleared( cur.getInt(6));
                ct.setNotes(cur.getString(7));
                ct.setCategory(cur.getInt(8));
             }
         } catch (Exception e) { Log.v("jv", "Exception in GetTransaction: " + e.toString() ); }
         return ct;
     }

    public ArrayList<Object> getAll() {
        ArrayList<Object> ret = new ArrayList<Object>();

        try {
            String query = "SELECT * FROM transactions";
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cur = db.rawQuery(query, null);

            if( cur != null )
                cur.moveToFirst();

            while( cur.moveToNext()) {
                cTransactions ct = new cTransactions();
                ct.setTransactionID(cur.getInt(0));
                ct.setAccountID(cur.getInt(1));
                ct.setAmount(cur.getDouble(2));
                ct.setTransactionType(cur.getInt(3));
                ct.setCheckNumber(cur.getString(4));
                ct.setCheckDate(cur.getString(5));
                ct.setCleared( cur.getInt(6));
                ct.setNotes(cur.getString(7));
                ct.setCategory(cur.getInt(8));
                ret.add(ct);
            }
        } catch(Exception e) { Log.v("jv", e.toString()); }
        return ret;
    }

1 个答案:

答案 0 :(得分:9)

cursor.moveToFirst()将光标设置为第一个结果行。然后你立即调用cursor.moveToNext(),它前进到第二个结果行。所以你只是总是跳过getAll()中的第一行。

您可以将循环更改为do-while来修复它:

cursor.moveToFirst();
do{
  // get the values
} while (cursor.moveToNext());