2011-11-20 81 views
-2

我有一個Sqlite與很多表,我想做一個TextBox這是能夠搜索SQL,我該怎麼做?我們如何搜索自己的SQLite?

+1

你需要教程(複數),而不是在stackoverflow上的一個問題。我建議你搜索谷歌。並嘗試逐件學習。在所有應有的尊重下,這不是一個幫助臺,我們可以爲您提供完整的代碼解決方案,而是提供特定(準時)問題並瞭解更多信息的地方。 – davidcesarino

+0

的確,你應該嘗試通過一個包含使用Sqlite的代碼的教程,並將這些知識應用於你的目的......你可能會從這樣的事情開始:http://coenraets.org/blog/android-samples/androidtutorial/ – HostileFork

回答

1

首先:這個問題太大了。第二:你通常不希望人們搜索你的整個數據庫......原因很明顯:他們不應該看到的內容,比如其他用戶的密碼。 因此,您必須編譯應該首先可以搜索的所有內容的列表。在每個在你的數據庫的表,你可以通過例如搜索欄:

SELECT linkToResource FROM tableName WHERE allowedContent1 like '%searchphrase%' or allowedContent2 like '%searchphrase%'; 

這是假設你有一個表,狀如ID, linkToResource, allowedContent1, allowedContent2, secretContent1, secretContent2

在Android的這一聲明是這樣的:

public List<String> getLinksToSearchResults(String searchphrase) { 
    List<String> result = new ArrayList<String>(); 

    Cursor c = yourDatabase.query(
      "yourTableName", 
      new String[] { "linkToResource" }, // The column you want as a result of the Query 
      "allowedContent1 like '%?%' OR allowedContent2 like '%?%'", // The where-Clause of the statement with placeholders (?) 
      new String[] { searchphrase, searchphrase }, // One String for every Placeholder-? in the where-Clause 
      null, // if you like a order by put it here 
      null, // group by here 
      null // having here 
      ); 
    while (c.moveToNext()) { 
     result.add(c.getString(0)); 
    } 
    c.close(); 
    return result; 
} 

您從中檢索的EditText字符串是這樣的:

String searchphrase = searchphraseEditText.getText(); 

這裏是希望你有一個想法:D
下一次,請提供一些更精確的問題的代碼(例如, 「爲什麼下面的代碼不會返回四位數的int?」)

Enjoy!

相關問題