/tags/20090907/src/com/menny/android/anysoftkeyboard/Dictionary/DictionarySQLiteConnection.java

http://softkeyboard.googlecode.com/ · Java · 104 lines · 89 code · 14 blank · 1 comment · 7 complexity · 1a777647b6de3639e0d57a9c85ad301f MD5 · raw file

  1. package com.menny.android.anysoftkeyboard.Dictionary;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10. public class DictionarySQLiteConnection extends SQLiteOpenHelper
  11. {
  12. public class DictionaryWord
  13. {
  14. private final String mWord;
  15. private final int mFrequency;
  16. public DictionaryWord(String word, int freq)
  17. {
  18. if (word == null)
  19. {
  20. Log.e("AnySoftKeyboard", "Got a NULL word from dictionary! This is illegal!");
  21. word = "" + this.hashCode();
  22. }
  23. mWord = word;
  24. mFrequency = freq;
  25. }
  26. public String getWord() {return mWord;}
  27. public int getFrequency() {return mFrequency;}
  28. }
  29. protected final String mTableName;
  30. protected final String mWordsColumnName;
  31. protected final String mFrequencyColumnName;
  32. protected final Context mContext;
  33. public DictionarySQLiteConnection(Context context, String dbName, String tableName, String wordsColumnName, String frequencyColumnName) {
  34. super(context, dbName, null, 3);
  35. mContext = context;
  36. mTableName = tableName;
  37. mWordsColumnName = wordsColumnName;
  38. mFrequencyColumnName =frequencyColumnName;
  39. }
  40. @Override
  41. public void onCreate(SQLiteDatabase db) {
  42. db.execSQL("CREATE TABLE " + mTableName + " ("
  43. + "Id INTEGER PRIMARY KEY,"
  44. + mWordsColumnName+" TEXT,"
  45. + mFrequencyColumnName+" INTEGER"
  46. + ");");
  47. }
  48. @Override
  49. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  50. db.execSQL("DROP TABLE IF EXISTS "+mTableName);
  51. onCreate(db);
  52. }
  53. public void addWord(String word, int freq)
  54. {
  55. SQLiteDatabase db = super.getWritableDatabase();
  56. ContentValues values = new ContentValues();
  57. values.put("Id", word.hashCode());//ensuring that any word is inserted once
  58. values.put(mWordsColumnName, word);
  59. values.put(mFrequencyColumnName, freq);
  60. long res = db.insert(mTableName, null, values);
  61. if (res < 0)
  62. {
  63. Log.e("AnySoftKeyboard", "Unable to insert '"+word+"' to the fall-back dictionary! Result:"+res);
  64. }
  65. else
  66. {
  67. Log.d("AnySoftKeyboard", "Inserted '"+word+"' to the fall-back dictionary. Id:"+res);
  68. }
  69. }
  70. public List<DictionaryWord> getAllWords()
  71. {
  72. //starting with a big storage
  73. List<DictionaryWord> words = new ArrayList<DictionaryWord>(5000);
  74. SQLiteDatabase db = getReadableDatabase();
  75. Cursor c = db.query(mTableName, new String[]{mWordsColumnName, mFrequencyColumnName}, null, null, null, null, null);
  76. if (c != null)
  77. {
  78. if (c.moveToFirst()) {
  79. while (!c.isAfterLast()) {
  80. String word = c.getString(0);
  81. int freq = c.getInt(1);
  82. words.add(new DictionaryWord(word, freq));
  83. c.moveToNext();
  84. }
  85. }
  86. c.close();
  87. }
  88. return words;
  89. }
  90. }