I have a list view which I populate with names entered in a database through SQLite. When I click on one of those names the app crashes.
Please, if you can find the issue, I'd be grateful. Thank you!
I have added the part of the logcat that appears when I click on a name from the list. COL0 = "ID" and COL1 = "Name".
OnClick method:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Log.e(TAG, name);
Log.e(TAG, "" + i);
Cursor player = dbHelper.getItemID(name); //this is line 51 -> see logcat
int itemID = -1;
while(player.moveToNext())
itemID = player.getInt(0);
if(itemID > -1) {
Intent intent = new Intent(PlayersList.this, PlayerCharacters.class);
intent.putExtra("id", itemID);
intent.putExtra("name", name);
startActivity(intent);
} else Toast.makeText(PlayersList.this, "No ID associated with that name.", Toast.LENGTH_SHORT).show();
}
});
}
getItemID() method in the database class:
public Cursor getItemID(String name) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = " + name;
Cursor data = db.rawQuery(query, null); //this is line 62 -> see logcat
return data;
}
The error log I get when clicking an item (name from the list):
D/AbsListView: onTouchUp() mTouchMode : 0
E/PlayersListActivity: Eu
3
E/SQLiteLog: (1) no such column: Eu
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.characters, PID: 30728
android.database.sqlite.SQLiteException: no such column: Eu (code 1): , while compiling: SELECT ID FROM PlayersTable WHERE Name = Eu
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: Eu (code 1): , while compiling: SELECT ID FROM PlayersTable WHERE Name = Eu)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1096)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:661)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1746)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1685)
at com.example.characters.DatabaseHelper.getItemID(DatabaseHelper.java:62) //---->here
at com.example.characters.PlayersList$1.onItemClick(PlayersList.java:51) //----->here
at android.widget.AdapterView.performItemClick(AdapterView.java:350)
at android.widget.AbsListView.performItemClick(AbsListView.java:1683)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4094)
at android.widget.AbsListView$10.run(AbsListView.java:6583)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Application terminated.
The string value name
must be enclosed in single quotes:
String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = '" + name + "'";
Without these single quotes the string value name
is considered a column name and as it does not exist such a column inside the table, the app crashes with an error like:
no such column: Eu
But the safe and recommended way of passing parameters to a query is this:
String query = "SELECT " + COL0 + " FROM " + TABLE_NAME + " WHERE " + COL1 + " = ?";
Cursor data = db.rawQuery(query, new String[] {name});
This way you don' have to worry about single quotes, as this will be handled by the method rawQuery()
.