我在android aplication(produtos,lista和items)中有一个包含3个表的数据库。 但是当尝试进行查询时,我得到了“没有这样的表项”的异常。
我的DatabaseHelper的代码:
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE lista(" +
"id INT AUTO_INCREMENT, " +
"nome VARCHAR(50), " +
"data VARCHAR(10), " +
"total REAL, " +
"primary key(id));");
db.execSQL("CREATE TABLE items( " +
"id INT AUTO_INCREMENT, " +
"lista INT, " +
"produto INT, " +
"valor REAL, " +
"primary key(id));");
db.execSQL("CREATE TABLE produtos(" +
"id INT AUTO_INCREMENT, " +
"descricao VARCHAR(100), " +
"primary key(id));");
}
我在代码的这一部分得到了例外:
String sql = "SELECT i.id, i.valor, i.lista, p.descricao " +
"FROM items i " +
"INNER JOIN produtos p " +
"ON (i.produto = p.id) " +
"WHERE (items.lista = ?)";
try
{
Cursor cursor = banco.rawQuery(sql, new String[]{String.valueOf(lista)}); //the exception
cursor.moveToFirst();
while(!cursor.isAfterLast())
{
Item i = new Item();
i.setId(cursor.getInt(0));
i.setValor(cursor.getDouble(1));
i.setLista(cursor.getInt(2));
i.setDescricao(cursor.getString(3));
l.add(i);
cursor.moveToNext();
}
cursor.close();
}
catch(SQLiteException e)
{
Toast.makeText(contexto, e.getMessage(), 100000).show();
}
我创建了另外两个表,并且在查询时我没有问题,我认为错误在查询字符串中,但我不知道为什么。
奇怪的是,显然我可以在这张表中插入。
答案 0 :(得分:0)
尝试使用“WHERE(i.lista =?)”
您为“项目”指定了别名。