Home » Java » java – no such column: hey (code 1 SQLITE_ERROR[1]): , while compiling: SELECT ID FROM ALLWORKHOURS WHERE NOTEMEMOS = hey-Exceptionshub

java – no such column: hey (code 1 SQLITE_ERROR[1]): , while compiling: SELECT ID FROM ALLWORKHOURS WHERE NOTEMEMOS = hey-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Log Cat:

no such column: hey (code 1 SQLITE_ERROR[1]): , while compiling: SELECT ID FROM ALLWORKHOURS WHERE NOTEMEMOS = hey

code:

    102) public String getID(String note){
    103)      SQLiteDatabase db = this.getWritableDatabase();
    104)      String  query = ("SELECT " + COL_0 + " FROM " + TABLE_NAME + " WHERE " + COL_5 + " = " + note);
    105)      db.rawQuery(query,null);
    106)      return query;
    107) }

I do have a column name hey in my Database
Database Picture

How to&Answers:

Your terminology is confused.

hey is a value, NOTEMEMOS is a column.

You need to quote the value hey to let the SQL compiler know that it is a string value, rather than a column you are trying to compare against.

String  query = ("SELECT " + COL_0 + " FROM " + TABLE_NAME + " WHERE " + COL_5 + " = '" + note + "'");

Just a note, you would be better off using a parameterised query, as using raw values is insecure (see SQL injection).

String  query = ("SELECT " + COL_0 + " FROM " + TABLE_NAME + " WHERE " + COL_5 + " = ?"); // That's right, quotes aren't needed for a parameterized query.
String result = "";
Cursor cursor = db.rawQuery(query,new String[] {note} );
if (cursor.moveToFirst()) {
    result = cursor.getString(cursor.getColumnIndex(COL_0));
}
cursor.close();
return result;

Answer:

Your query should look like this

SELECT ID FROM ALLWORKHOURS WHERE NOTEMEMOS = 'hey'

Let me know if it helps

Answer:

The way that you concatenate the parameter note creates this sql statement:

SELECT ID FROM ALLWORKHOURS WHERE NOTEMEMOS = hey

so hey is considered a column identifier and not a string literal because it is not enclosed inside single quotes.
You could do this instead:

String  query = "SELECT " + COL_0 + " FROM " + TABLE_NAME + " WHERE " + COL_5 + " = '" + note + "'";

and it would work.

But the recommended way is passing parameters as a string array like this:

String  query = "SELECT " + COL_0 + " FROM " + TABLE_NAME + " WHERE " + COL_5 + " = ?";
Cursor cursor = db.rawQuery(query, new String[] {note});
return cursor;

This way you don’t worry about single quotes as this is taken care of by rawQuery().
I guess you want to return the Cursor object and not the sql query string, right?