简体   繁体   中英

java.lang.IllegalArgumentException: column 'foo' does not exist android

Numerous StackOverflow problems are similar, but it is not an issue of incorrect spacing in the query string. I have two tables, one for peers and one for messages, which has foreign keys associated with ids in the peer table.

Here are my creation strings:

private static final String DATABASE_CREATE = "CREATE TABLE " + PEER_TABLE + " ("
        + PeerContract._ID    + " INTEGER PRIMARY KEY, "
        + PeerContract.NAME + " TEXT NOT NULL, "
        + PeerContract.ADDRESS  + " TEXT NOT NULL, "
        + PeerContract.PORT + " TEXT NOT NULL);";

private static final String MESSAGE_TABLE_CREATE = "CREATE TABLE " + MESSAGE_TABLE + " ("
        + MessageContract._ID + " INTEGER PRIMARY KEY, "
        + MessageContract.MESSAGE_TEXT  + " TEXT NOT NULL, "
        + MessageContract.SENDER + " TEXT NOT NULL, "
        + MessageContract.PEER_FOREIGN_KEY + " INTEGER NOT NULL, "
        + "FOREIGN KEY ("+ MessageContract.PEER_FOREIGN_KEY+") " +
        "REFERENCES "+PEER_TABLE+"("+PeerContract._ID+") ON DELETE CASCADE);";
private static final String CREATE_INDEX =  "CREATE INDEX " + INDEX + " ON " +
        MESSAGE_TABLE + "(" + MessageContract.PEER_FOREIGN_KEY + ");";

I want to query a list of all received messages so I did this:

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);
}

Which makes sense to me. And the spacing so far is alright there is no errors.

In my main activity I have a SimpleCursorAdapter:

   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);

I get "column 'name' does not exist" and it is driving me crazy! I properly defined it in DATABASE_CREATE as PeerContract.NAME. I'm guessing its happening because of fetchAllMessages but I am not sure how to fix it.

You are not selecting the PeerContract.NAME, MessageContract.MESSAGE_TEXT in your fetchAllMessages() method. Change it to this:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM