gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:51:04 25 4
gpt4 key购买 nike

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

这是我创建的字符串:

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

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

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

这对我来说很有意义。到目前为止间距没问题,没有错误。

在我的主要 Activity 中,我有一个 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);

我收到“列‘名称’不存在”,这让我发疯!我在 DATABASE_CREATE 中将其正确定义为 PeerContract.NAME。我猜它的发生是因为 fetchAllMessages 但我不确定如何修复它。

最佳答案

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

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

关于java.lang.IllegalArgumentException : column 'foo' does not exist android 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com