撇号SQLite的选择查询 [英] Apostrophe in SQLite select query

查看:152
本文介绍了撇号SQLite的选择查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite的问题,有些定制的SQL:

I have a problem with SQLite and a bit of bespoke SQL:

public Cursor getExistingSpeciesDetails(String sub_id, String english_name) {
    Cursor c = mDb.rawQuery(
            "SELECT mobileobs.how_many, mobileobs.comment, mobileobs.sensitive, "
                    + "mobileobs.breeding_evidence from mobileobs "
                    + "WHERE mobileobs.sub_id = '" + sub_id
                    + "' and mobileobs.english_name = '" + english_name
                    + "'", null);
    return c;
}

我的问题是中英文名称撇号(宽尾树莺)引起的SQL错误(强制关闭我的Andr​​oid应用程序)。有没有办法逃避这种类型的条款?

My problem is an apostrophe in the english name (Cetti's Warbler) is causing an error in the SQL (force closing my Android app). Is there any way to escape the apostrophe in this type of clause?

在此先感谢

推荐答案

为了这个目的,你需要使用SQLite的参数注射。例如:

For that purpose you need to use SQLIte's parameter injections. For example:

Cursor c = mDb.rawQuery(
            "SELECT mobileobs.how_many, mobileobs.comment, mobileobs.sensitive, "
             + "mobileobs.breeding_evidence from mobileobs "
             + "WHERE mobileobs.sub_id = ? AND mobileobs.english_name = ?"
             , new String[]{sub_id, english_name});

或者如下改变你的输入参数:

Or change your input parameter as below:

Cetti''s Warbler

这篇关于撇号SQLite的选择查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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