SQLite: Escaping Special Characters

后端 未结 1 738
醉话见心
醉话见心 2021-01-23 04:28

Is there a common way within Android to escape all of the characters that aren\'t allowed in a SQLite database? For instance, \"You\\\'Me\'\". Instead of me figuring out every

相关标签:
1条回答
  • 2021-01-23 05:13

    In SQL, strings are delimited with 'single quotes'. To use one inside a string, you have to double it.

    There are no other characters that need to be escaped in SQL. (If you're embedding strings in another language, such as Java, you also have to use the escape mechanisms of that language.)

    To avoid string formatting problems, you should use parameters instead:

    String name = "me";
    db.rawQuery("SELECT ... WHERE name = ?", new String[]{ name });
    
    0 讨论(0)
提交回复
热议问题