Simplecursoradapter Is Being Difficult To Use
Solution 1:
The new error is saying that you don't have the table "DATABASE_PATIENT_TABLE" in your database. From your code that appears to be the Static Field name for the string that contains the name of the table in the database and not the name of the table in the database.
In the code below you need to change "DATABASE_PATIENT_TABLE" to the name of the table in the database.
Stringdb_sel="SELECT id as _id, KEY_ROOM_NUMBER" +
",KEY_PATIENT_INITIAL FROM DATABASE_PATIENT_TABLE";
Try this and see if it will work:
Stringdb_sel="SELECT id as _id, KEY_ROOM_NUMBER" +
",KEY_PATIENT_INITIAL FROM" + DATABASE_PATIENT_TABLE;
As for your original error a SimpleCursorAdapter must contain the field of _id. The change you made to the SQL Statment that powers the cursor should fix it but you need to make sure you have the right table name in the SQL statement.
Hope this helps.
Solution 2:
Have you read this part of error stack:
android.database.sqlite.SQLiteException: nosuchtable: DATABASE_PATIENT_TABLE:
? You just have no such table in database. It's not CursorAdapter problem. Your loop seems completely pointless - just read some java handbook. Ok, lets start to list some problems in your code:
String[] columns = newString[] { c.getString(1), c.getString(2) };
columns[]
should contain columns names not values...
In your while{}
loop you are creating dozens(?) of SimpleCursorAdapters while only the last on is passed.
This part of code:
Stringdb_sel="SELECT id as _id, KEY_ROOM_NUMBER" +
",KEY_PATIENT_INITIAL FROM DATABASE_PATIENT_TABLE";
is obviously wrong. I assume that you have something like
privatestaticStringDATABASE_PATIENT_TABLE="patient";
so just use those constants:
Stringdb_sel="select id as _id, +"KEY_ROOM_NUMBER+", "+ KEY_PATIENT_INITIAL +" from "+DATABASE_PATIENT_TABLE;
Replace that:
String[] columns = newString[] { c.getString(0),c.getString(1), c.getString(2) };
int[] to = newint[] { x, R.id.room_number_db, R.id.pt_initial_db };
With that:
String[] columns = newString[] { c.getColumnName(1), c.getColumnName(2) };
int[] to = newint[] {R.id.room_number_db, R.id.pt_initial_db };
Solution 3:
The problem is fixed, the main thing is that I should make sure I always query for the _id with each query to the database, but if I do it with cursor.getString(someIndex) then this gets misleaded as the adapter in my case is taking the content of first column as the rowid and is trying to search the row.
Nether doing rawQuery(),or using "Select _id as _id" stuffs are mandatory. these things are sometime misleading as I felt.
So keeping things simple below is the code. Database code
publicCursorgetAllRows_Patient_Db()
{
return db.query(DATABASE_PATIENT_TABLE, newString[] {
KEY_ROWID,
KEY_ROOM_NUMBER,
KEY_PATIENT_INITIAL
},
null,
null,
null,
null,
null);
}
Activity Code
HospitalData = newDatabase(this);
HospitalData.open();
Cursor c = HospitalData.getAllRows_Patient_Db();
startManagingCursor(c);
String[] columns = new String[] {HospitalData.KEY_ROOM_NUMBER,HospitalData.KEY_PATIENT_INITIAL };
int[] to = newint[] { R.id.room_number_db, R.id.pt_initial_db };
adapter = newSimpleCursorAdapter(this,R.layout.patient_db, c,columns,to);
c.moveToNext();
setListAdapter(adapter);
Post a Comment for "Simplecursoradapter Is Being Difficult To Use"