本文介绍了Android的rawquery动态Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android中像这样的执行SQL查询:

I'm attempting to perform a SQL query in Android like this one:

SELECT * FROM Flashcards WHERE (category = 'Anatomy' OR category = 'Surgery') AND (difficulty = 'Easy' OR difficulty = 'Medium');

类别和困难的数量在由用户选择运行时确定。到目前为止,我已经使用带有查询参数的原始查询试过,但我觉得我在这种情况下战斗的框架:

The number of categories and difficulties is determined at runtime by user selection. So far I've tried using raw query with query parameters, but I feel like I am fighting the framework in this case:

String query = "SELECT * FROM Flashcards WHERE (?) AND (?) ORDER BY RANDOM() LIMIT 1";
    Cursor cursor = db.rawQuery(query, queryParameters);

查询参数我试着传递包括:

The query parameters I've tried passing in include:

category = 'Anatomy' OR category = 'Surgery'

category = Anatomy OR category = Surgery

该查询返回的保持没有结果。在同一个数据库上执行相同的查询外没有返回结果。

The query keeps returning no results. The same query performed on the same database externally does return results.

推荐答案

尝试:

String query = "SELECT * FROM Flashcards WHERE category = ? OR category = ? ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(query, queryParameters);

仅供参考,参数的地方应该是描绘?登录。

FYI, parameters places should be depicted with ? sign.

当你想通过字段名和值运行时,你可以建立格式化字符串,是这样的:

As you want to pass field name and values run time, you can build formatted string, something like:

String query = "SELECT * FROM Flashcards WHERE %s AND %s ORDER BY RANDOM() LIMIT 1";
Cursor cursor = db.rawQuery(String.format(query, queryParameters));

在这里,你必须建立的东西,如类别='解剖'OR类 queryParameters 值='外科手术'正如你所提到的。

where you have to build queryParameters values with something like category = 'Anatomy' OR category = 'Surgery' as you have mentioned.

这篇关于Android的rawquery动态Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:16