gpt4 book ai didi

android - 日志 : column _id does not exist (SQL related)

转载 作者:行者123 更新时间:2023-11-30 03:44:31 24 4
gpt4 key购买 nike

我的应用程序目前遇到一个非常烦人的问题:目标是根据用户单击的按钮从数据库中读取 ListView 中的不同信息。不幸的是,我的应用程序总是崩溃,我在 stackoverflow 上的类似问题中找不到有效的解决方案(尝试更改数据库版本,添加一些关于光标的行等)。

这是我的代码的相关部分:

A.数据库适配器类:

public class DatabaseAdapter {

public static final String DATABASE_TABLE = "Restaurants";

public static final String COL_CAT1 = "cat1";
public static final String KEY_ROWID = "_id";
public static final String COL_CAT2 = "cat2";
public static final String COL_NAME = "name";
public static final String COL_COMMENTS = "comments";

private Context myContext;
private SQLiteDatabase myDatabase;
private DatabaseHelper dbHelper;
private Cursor c;

// Constructor
public DatabaseAdapter(Context context) {
this.myContext = context;

}

public DatabaseAdapter open() throws SQLException {
dbHelper = new DatabaseHelper(myContext);
// TODO: or maybe?
// database = dbHelper.getWritableDatabase();
try {
dbHelper.createDatabase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myDatabase = dbHelper.getReadableDatabase();
return this;
}

public void close() {
if (c != null) {
c.close();
}
try {
dbHelper.close();
myDatabase.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


public Cursor findNameInTable(int myVariable) {
c = myDatabase
.query(DATABASE_TABLE, new String[] { COL_NAME , COL_COMMENTS },
COL_CAT1+"=?",
new String[] { Integer.toString(myVariable) }, null,
null, null);
return c;
}

B. ResultListViewActivity(结果应该通过 ListView 中的此 Activity 显示):

public class ResultListViewActivity extends Activity {

private SimpleCursorAdapter cursorAdapter;
private DatabaseAdapter dbHelper;
ListView listview;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_list_view);

dbHelper = new DatabaseAdapter(this); // open() method is in DatabaseAdapter

dbHelper.open();


displayListView();
}

private void displayListView() {

Bundle bundle = getIntent().getExtras();
int myVariable = bundle.getInt("myVariable");

Cursor c = dbHelper.findNameInTable(myVariable);
// the desired columns to be bound
String[] columns = new String[] { DatabaseAdapter.COL_NAME,
DatabaseAdapter.COL_COMMENTS, };
// the XML defined views which the data will be bound to
int[] to = new int[] { R.id.name, R.id.comments, };
// create the adapter using the cursor pointing to the desired data
// as well as the layout information
cursorAdapter = new SimpleCursorAdapter(this, R.layout.poi_info, c,
columns, to, 0);

ListView listView = (ListView) findViewById(R.id.listview);
// Assign adapter to ListView
listView.setAdapter(cursorAdapter);
}

我的数据库图片:如你所见,实际上有一个 _id 列: enter image description here

最后,在以下情况下,我的 logcat 的图片:
enter image description here

如果您想查看我代码的其他部分,请告诉我。在此先感谢您提供任何帮助!

最佳答案

每个 ListView 在其选择查询中都需要一个 _id 列。您必须在列表查询中包含一个 _id 列

将您的查询更改为:

c = myDatabase
.query(DATABASE_TABLE, new String[] { KEY_ROWID,COL_NAME , COL_COMMENTS },
COL_CAT1+"=?",
new String[] { Integer.toString(myVariable) }, null,
null, null);

关于android - 日志 : column _id does not exist (SQL related),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217352/

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