sqlite.SQLiteException:靠近"s":语法错误:由于撇号 [英] sqlite.SQLiteException: near "s": syntax error: because of apostrophe

查看:62
本文介绍了sqlite.SQLiteException:靠近"s":语法错误:由于撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将其插入到表中的歌曲名称包含一个'"(撇号),所以执行请求时出错.我该如何解决

The song name that I want to insert it into my table contains a " ' " (apostrophe )so , there is an error when the request is excuted. How can I fix it

Cursor pid = mDB.rawQuery("select Id  FROM MusicPlayer WHERE "Path ='" + sname + "';", null);

我正在获取sname运行时,.. sname = /mnt/sdcard/Now那就是我所说的音乐85 [Bubanee]/16-让我们准备好隆隆声(100%广播混音)-PJ&Duncan.mp3

I am getting sname runtime so .. sname=/mnt/sdcard/Now Thats What I Call Music 85 [Bubanee]/16 - Let's Get Ready To Rhumble (100% Radio Mix) - PJ & Duncan.mp3

我得到以下错误.

android.database.sqlite.SQLiteException:在"s"附近:语法错误:,在编译时:从MusicPlayer WHERE Path ='/mnt/sdcard/Now那就是我所说的音乐85 [Bubanee]/16-让我们为隆隆声做准备(100%广播混音)-PJ&Duncan.mp3';

因为sname包含 let's 单词和',这给了我错误.

Because sname contain let's word with ' which gives me error.

推荐答案

从理论上讲,您可以在SQL中将'转换为''.

In theory you could escape ' as '' in SQL.

但是,最好使用变量绑定.用?替换SQL中的字符串文字,并在数组中提供相应数量的 String :

However, it's better to use variable binding. Replace the string literals in SQL with ? and supply corresponding number of Strings in an array:

Cursor pid = mDB.rawQuery("select Id  FROM MusicPlayer WHERE Path = ?;",
    new String[] { sname });

这篇关于sqlite.SQLiteException:靠近"s":语法错误:由于撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆