-3

我的 Android 应用程序有问题。我正在尝试使用多个表查询数据库并在应用程序中显示结果。这是一本字典。问题是我的查询不起作用。我不知道我做错了什么。这是我用于它的代码示例。如果您需要更多信息,请告诉我。谢谢你。

try {

        Cursor cursor = dictionary.getDictionaryDatabase().rawQuery("SELECT index_nom_adresse" + word[0].toLowerCase().trim() + " FROM adresse_definition JOIN   definition ON adresse_definition.definition = definition.data_id ", null);
        //Cursor cursor = dictionary.getDictionaryDatabase().query("adresse_definition", null, "index_nom_adresse= '" + word[0].toLowerCase().trim() + "' or definition= '" + word[0].toUpperCase().trim() + "' ", null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() != 0) {

            if (word[1] == null || word[1].equals("English")) {
                translatedWord = cursor.getString(2);
            } else {
                translatedWord = cursor.getString(1);
            }
        } else {
            translatedWord = "The word is not in database";
        }
        cursor.close();
    } catch (SQLiteException sqle) {
        translatedWord = "The word is not in database";
    }

这是我在尝试搜索单词时得到的 logCat

03-20 14:34:17.341: I/SqliteDatabaseCpp(516): sqlite returned: error code = 1, msg = no such column: index_nom_adressebonbon, db=/data/data/com.example.application/databases/dict.db
4

2 回答 2

0

查询应该是:

           Cursor cursor = dictionary.getDictionaryDatabase().rawQuery("SELECT index_nom_adresse" + word[0].toLowerCase().trim() + " FROM adresse_definition JOIN   definition ON adresse_definition.definition = definition.data_id ", null);

否则没有正确定义库。

于 2013-03-20T14:28:07.683 回答
0

首先,您通过捕获Exception然后说“该词不在数据库中”来隐藏真正的错误。由于您提供的信息量有限,您可能遇到的问题只是猜测,正在传递SELECT, 而不是WHERE从句中的单词。

您的 SQL 查询可能需要是这样的:

SELECT index_nom_adresse
FROM adresse_definition 
JOIN definition ON adresse_definition.definition = definition.data_id
WHERE index_nom_adresse = word[0].toLowerCase().trim() OR definition = word[0].toUpperCase().trim()

这转化为:

String sql = "SELECT index_nom_adresse ";
sql += "FROM adresse_definition JOIN definition ON adresse_definition.definition = definition.data_id ";
sql += "WHERE index_nom_adresse = " + word[0].toLowerCase().trim() " OR definition=" + word[0].toUpperCase().trim();
Cursor cursor = dictionary.getDictionaryDatabase().rawQuery(sql, null);

或(更好的解决方案)(不知道您将如何进行连接,是否JOIN需要?)

Cursor cursor = dictionary.getDictionaryDatabase().query(
    "adresse_definition", 
    new String[] {"index_nom_adresse"}, 
    "index_nom_adresse= ? OR definition=?'", 
    new String[] {word[0].toLowerCase().trim(), word[0].toUpperCase().trim()}, 
    null, 
    null, 
    null);

请阅读 Android 和 SQL:http ://www.vogella.com/articles/AndroidSQLite/article.html

我这里没有工具来验证代码,如果有错误请评论或编辑。

于 2013-03-20T16:29:45.840 回答