Android Logcat:列id不存在(与SQL相关)
我目前在应用程序中遇到了一个非常恼人的问题:目标是根据用户单击的按钮在listview中读取数据库中的不同信息。不幸的是,我的应用程序总是崩溃,在stackoverflow的类似问题中,我找不到有效的解决方案(尝试更改数据库版本,添加一些关于光标的行,等等) 以下是我的代码的相关部分: A.数据库适配器类:Android Logcat:列id不存在(与SQL相关),android,sql,database,cursor,logcat,Android,Sql,Database,Cursor,Logcat,我目前在应用程序中遇到了一个非常恼人的问题:目标是根据用户单击的按钮在listview中读取数据库中的不同信息。不幸的是,我的应用程序总是崩溃,在stackoverflow的类似问题中,我找不到有效的解决方案(尝试更改数据库版本,添加一些关于光标的行,等等) 以下是我的代码的相关部分: A.数据库适配器类: public class DatabaseAdapter { public static final String DATABASE_TABLE = "Restaurants"; pub
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中显示):
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列:
最后,一张我的logcat的照片,以防:如果你想看到我代码的其他部分,请告诉我。提前感谢您的帮助 每个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);
每个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);
看,看。