English 中文(简体)
安德列:活动管理中的替罪羊。
原标题:Android: Escape SQL in Activity.managedQuery()
  • 时间:2011-10-27 20:33:39
  •  标签:
  • android
  • sql

I m developing an Anders application that make a Query using Activity. Ould Query (, which take a String for the selection/code>. 论点是<代码>。 WHERE 声明条款,但排除了<编码>WHERE关键词。

My application uses the first and last names of people who might be in the user s address book. However, some people have a name which contains a single quote character. For example, John O Reilly. This causes a SQLiteException because the single quote terminated the string and it doesn t know how to handle Reilly.

I tried doing a simple:
name = name.replace(" ", "\ ");
But this didn t work.

完全例外:

android.database.sqlite.SQLiteException: near "Reilly": syntax error: , while compiling:     
SELECT raw_contact_id, display_name FROM view_data_restricted data WHERE (1) AND 
(in_visible_group =  1  AND display_name = John O Reilly ) ORDER BY display_name 
COLLATE LOCALIZED ASC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
at android.content.ContentResolver.query(ContentResolver.java:262)
at android.app.Activity.managedQuery(Activity.java:1550)
at org.jonescb.myApp.MyClass.queryFriends(MyClass.java:68)

这是我的法典:

fname = fname.replace(" ", "\ ");
Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Data.RAW_CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +
                " =  1  AND " + ContactsContract.Contacts.DISPLAY_NAME +
                " = " + fname + " ";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";
Cursor cursor = activity.managedQuery(
                contacts,
                projection,
                selection,
                null,
                sortOrder
                );
最佳回答

use selectionArguments

Uri contacts = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Data.RAW_CONTACT_ID,
    ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP +
                " =? AND " + ContactsContract.Contacts.DISPLAY_NAME +
                " =?";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
                " COLLATE LOCALIZED ASC";
Cursor cursor = activity.managedQuery(
                contacts,
                projection,
                selection,
                new String[] {"1", fname},
                sortOrder
                );
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签