skip to Main Content

I have a dictionary app where user needs to input in an editText and click the translate button to display the result. It works perfectly but whenever a user inputs a query including ‘ (e.g. Baker’s), I get SQLite error as follows:

android.database.sqlite.SQLiteException: near "s": syntax error (code 1 SQLITE_ERROR[1]): , while compiling: select * from words where vocab = ‘Baker’s’

binding.transcribe.setOnClickListener {
        
        val Word = binding.editText.text.toString()

        val Answer = dbHelp.getAnswer(Word)
        binding.transcribedVersion.text = Answer
    }

How can I fix this problem?

2

Answers


  1. you can use double quote instead of single quote, like this:
    select * from words where vocab = 'Baker''s'

    Login or Signup to reply.
  2. Use selection args:

    select * from words where vocab = ?
    

    that you can bind later using your value

    val selectionArgs = arrayOf("Baker's")
    

    update

    If you are using rawQuery it would be like

    db.rawQuery("select * from words where vocab = ?", arrayOf("Baker's"))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search