Home > Mobile >  Android Studio SQLite query from the user including " ' "
Android Studio SQLite query from the user including " ' "

Time:12-15

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?

CodePudding user response:

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

CodePudding user response:

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"))
  • Related