2013-03-20 77 views
-3

我的Android應用程序出現問題。我試圖用多個表查詢數據庫並在應用程序中顯示結果。這是一本字典。問題是我的查詢不起作用。我不知道我做錯了什麼。 以下是我使用的代碼示例。如果您需要更多信息,請告訴我。 謝謝。問題與Android應用程序中的我的sqlite查詢

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 
+0

什麼不起作用?你可以例外嗎?你沒有得到任何數據?你得到錯誤的數據? – RvdK 2013-03-20 14:26:38

+0

我沒有收到任何數據,它說'數據庫中沒有這個詞' – user2047427 2013-03-20 14:28:26

+0

是崩潰還是顯示某種錯誤信息? – hardartcore 2013-03-20 14:35:57

回答

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); 

否則coulmn未正確定義。

+0

我試過了,但它仍然沒有改變任何東西。 – user2047427 2013-03-20 14:31:29

+0

嘗試提供異常詳細信息} catch(SQLiteException sqle){convertedWord =「該單詞不在數據庫中」+ sqle.getMessage(); } – Gyonder 2013-03-20 14:35:06

+0

我不明白你的答案能解決什麼問題 – njzk2 2013-03-20 14:48:12

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

我沒有這裏的工具來驗證代碼,請評論或編輯,如果有錯誤。

+0

我使用JOIN的原因是因爲我有一個包含多個表的數據庫。我不知道是否有其他的方法 – user2047427 2013-03-20 17:21:51

+0

該代碼無效。看來,無論我輸入什麼,它都會將其作爲列名稱,而不是我正在搜索的單詞。 – user2047427 2013-03-20 17:24:50