【问题标题】:How to escape single and double quotes together in sql lite in android?如何在android的sqlite中一起转义单引号和双引号?
【发布时间】:2012-04-20 10:49:06
【问题描述】:

如果是字符串,如何转义单引号和双引号

t"'e'"s""t''

换句话说,如何在包含单引号和双引号的字符串中转义引号。

这个例子是这样的:

    String whereClauseToTracks = MediaStore.Audio.Media.ARTIST + " = '" + artist + "' "; // t"'e'"s""t'' - instead of artist
    Cursor cursorToTracks = mResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , new String[] { MediaStore.Audio.Media.ALBUM }, whereClauseToTracks, null, null);

当字符串包含引号时出现 sql lite 异常。

【问题讨论】:

    标签: android sql sqlite escaping quotes


    【解决方案1】:

    像这样在selection 中使用selectionArgs?

    String selection= MediaStore.Audio.Media.ARTIST + " = ?";
    Cursor cursorToTracks = mResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , new String[] { MediaStore.Audio.Media.ALBUM }, selection, new String[] {artist}, null);
    

    【讨论】:

    • 这不起作用,它与此中断:String sql = "My \' bad \" sql";
    • sql 在没有转义的情况下看起来应该如何? My ' bad " sql ?那么你不应该逃避单撇号......
    【解决方案2】:

    使用\'\"

    String whereClauseToTracks = MediaStore.Audio.Media.ARTIST + " = '" + "t\"\'e\'\"s\"\"t\'\'" + "' ";    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      相关资源
      最近更新 更多