java.lang.IllegalArgumentException:column' foo'不存在android

时间:2016-02-14 07:14:20

标签: java android sqlite

许多StackOverflow问题都是类似的,但它不是查询字符串中不正确间距的问题。我有两个表,一个用于对等,一个用于消息,它具有与对等表中的ID相关联的外键。

以下是我的创作字符串:

public Cursor fetchAllMessages(){
    String query = "SELECT " + MESSAGE_TABLE + "." + MessageContract._ID + ", "
            + MessageContract.MESSAGE_TEXT + ", "
            + MessageContract.SENDER
            + " FROM " + MESSAGE_TABLE + " JOIN " + PEER_TABLE + " ON "
            + MESSAGE_TABLE + "." + MessageContract.PEER_FOREIGN_KEY
            + "=" + PEER_TABLE + "." + PeerContract._ID;
    return db.rawQuery(query, null);
}

我想查询所有收到邮件的列表,所以我这样做了:

   listView = (ListView) findViewById(R.id.msgList);
    dbAdapter = new ServerDbAdapter(this);
    dbAdapter.open();
    String[] from = new String[] {PeerContract.NAME, MessageContract.MESSAGE_TEXT};
    int[] to = new int[] {android.R.id.text1, android.R.id.text2};
    cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, from, to);
    listView.setAdapter(cursorAdapter);
    cursor = dbAdapter.fetchAllMessages();
    cursorAdapter.swapCursor(cursor);

这对我有意义。到目前为止,间距是没有错误的。

在我的主要活动中,我有一个SimpleCursorAdapter:

{{1}}

我得到"列'名称'不存在"它让我抓狂! 我在DATABASE_CREATE中将其正确定义为PeerContract.NAME。我因为fetchAllMessages而猜测它的发生但是我不确定如何修复它。

1 个答案:

答案 0 :(得分:3)

您没有在PeerContract.NAME, MessageContract.MESSAGE_TEXT方法中选择fetchAllMessages()。将其更改为:

public Cursor fetchAllMessages(){
    String query = "SELECT " + MESSAGE_TABLE + "." + MessageContract._ID + ", "
            + MessageContract.MESSAGE_TEXT + ", "
            + PeerContract.NAME + ", "
            + PeerContract.MESSAGE_TEXT + ", "
            + MessageContract.SENDER 
            + " FROM " + MESSAGE_TABLE + " JOIN " + PEER_TABLE + " ON "
            + MESSAGE_TABLE + "." + MessageContract.PEER_FOREIGN_KEY
            + "=" + PEER_TABLE + "." + PeerContract._ID;
    return db.rawQuery(query, null);
}