我正在创建一个用于监控个人财务的应用程序。用户创建帐户、类别和交易。该类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");
}
我以前用 aCursor
来显示帐户列表,效果很好。我不知道为什么它不再工作了。我没有在表中使用外键或类似的东西,它应该可以工作。如果我执行代码,我将得到错误的值和大量错误。我在这些特定行上得到错误:
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
列中得到错误的值。我使用带有 shell 的 SQLite 3 测试了我的 SQL adb
,它工作正常。以下是来自 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>
这是我收到的其余部分。有关如何解决此问题的任何建议?