游标返回不正确的值

时间:2011-08-19 18:29:27

标签: android sqlite cursor android-sqlite

我正在创建一个监控个人财务的应用程序。用户创建帐户,类别和交易。 dBHelper.java类创建数据库,如下所示:

public void onCreate(SQLiteDatabase db) {
    //executes only once
    //create tables
    Log.v("TAG", "creating table:" +racuniTable);
    String sql = "create table " +racuniTable+" (" +colID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
    +colName+ " TEXT, " +colValue+ " REAL, " +colAccType+ " TEXT);";

    Log.v("TAG", "creating table:" +transakcijeTable);
    String sql1 = "create table " +transakcijeTable+" (" +colTransID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " 
    +colTransDate+ " DATE, "  +colAmount+ " REAL, " 
    +colIDracuni+ " INTEGER, " +colIDkategorije+ " INTEGER);";

    Log.v("TAG", "creating table:" +kategorijeTable);
    String sql2 = "create table " +kategorijeTable+" (" +colCatID+ " INTEGER PRIMARY KEY AUTOINCREMENT, " +colCatName+ " TEXT);";
    Log.v("TAG", "adding account:");

    db.execSQL(sql);
    db.execSQL(sql1);
    db.execSQL(sql2);
}

此功能创建三个数据库。当我尝试获取SQL语句配置的所有事务或任何其他报表的列表时,会出现问题:

private void getTransactions(){

    try {
        DbHelper dbHelper = new DbHelper(this.getApplicationContext());
        newDB = dbHelper.getWritableDatabase(); 
        m_transaction = new ArrayList<transaction>();
        Cursor c = newDB.rawQuery("SELECT rac.Ime, trans.Znesek, trans.Datum,   kate.Ime FROM Racuni AS rac, " + "Transakcije AS trans, Kategorije 
        as kate WHERE trans.ID_racuna=rac._id AND trans.ID_kategorije=kate._id", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String racun = c.getString(c.getColumnIndex("rac.Ime"));
                    double znesek = c.getDouble(c.getColumnIndex("trans.Znesek"));
                    String datum = c.getString(c.getColumnIndex("trans.Datum"));
                    String kategorija = c.getString(c.getColumnIndex("kate.Ime"));

                    transaction t1 = new transaction();
                    t1.setracun(racun);
                    t1.setznesek(znesek);
                    t1.setdatum(datum);
                    t1.setkategorija(kategorija);   

                    m_transaction.add(t1);

                }while (c.moveToNext());
            }
        }       
    } catch (SQLiteException se ) {
    Log.e(getClass().getSimpleName(), "Could not create or Open the database");
  }

我之前使用Cursor来显示帐户列表并且工作正常。我不知道为什么它不再起作用了。我没有在表中使用外键或类似的东西,它应该工作。如果我执行代码,我将得到错误的值和大量的错误。我在这些特定的行上得到了错误:

String racun = c.getString(c.getColumnIndex("rac.Ime"));
double znesek = c.getDouble(c.getColumnIndex("trans.Znesek"));
String datum = c.getString(c.getColumnIndex("trans.Datum"));
String kategorija = c.getString(c.getColumnIndex("kate.Ime"));

应用程序将运行,我将打印信息。所有打印的信息都是正确的。字符串racun与字符串kategorija相同,但不应该如此。我不确定为什么我一直从racun列中获取错误的值。我使用带有adb shell的SQLite 3测试了我的SQL,它运行正常。以下是LogCat的错误:

08-19 17:48:16.279: ERROR/Cursor(625): requesting column name with table name -- rac.Ime

08-19 17:48:16.331: ERROR/Cursor(625): requesting column name with table name -- trans.Znesek

08-19 17:48:16.359: ERROR/Cursor(625): requesting column name with table name -- trans.Datum

08-19 17:48:16.439: ERROR/Cursor(625): requesting column name with table name -- kate.Ime

是否有错误,因为我在SQL中使用了别名?这应该打印出来:

SELECT rac.Ime, trans.Znesek, trans.Datum, kate.Ime FROM Racuni AS rac, Transakcije AS trans, Kategorije as kate WHERE trans.ID_racuna=rac._id AND trans.ID_kate
    gorije=kate._id;
    Tomaz|50.0|3911-08-18|placa
    Tom|33.0|3912-07-18|placa
    Tomaz|70.0|3800-07-17|zapravlanje
    Tom|69.0|2010-07-17|placa
    Tomaz|30.0|2011-08-18|placa
    Tom|30.0|2011-08-12|zapravlanje
    Tom|50.0|2011-08-01|placa
    sqlite>

This是我收到的其余部分。有关如何解决此问题的任何建议吗?

2 个答案:

答案 0 :(得分:2)

更改

SELECT rac.Ime, trans.Znesek, trans.Datum, kate.Ime FROM ...

为:

SELECT rac.Ime racun, trans.Znesek, trans.Datum, kate.Ime kategorja FROM ...

然后分别改变

c.getColumnIndex(...)

答案 1 :(得分:0)

示例:

表1:ID,姓名,详情

表_2:ID,名称,价格

String ALL_COLUMNS_TABLE_2="Table_2.id _id, Table_2.name _name, Table_2.price _price";

Cursor cursor = db.rawQuery("select "+ Table_1 +".*,"+ALL_COLUMNS_TABLE_2+" from " + Table_1 + "," + Table_2,null);
try {
            if (cursor.moveToFirst()) {
                while (cursor.isAfterLast() == false) {

                    int idTable1 = cursor.getInt(cursor
                            .getColumnIndex("id"));
int idTable2 = cursor.getInt(cursor
                            .getColumnIndex("_id"));
....
}