2013-07-29 37 views
0

我一直試圖編寫一個超級基本Android應用程序,當給定一個字符串時,讀取/搜索我從CUDict的133000字詞典中生成的sqlite3數據庫文件的「word」列,(在下面的示例中,我將搜索與單詞列中的「Hello」相對應的代碼列條目),並輸出logcat相應的條目。我知道我有一些方法來設置數據庫中的值,但我的目標是簡單地搜索我已經擁有的數據庫。在Android中讀取常量SQLite3數據庫文件

在終端,我可以做

kizzlebot$ sqlite3 cmudict.0.7a.sqlite3
> SELECT code FROM cmudict WHERE word="HELLO"

搜索數據庫。它返回正確的值。

但是當我運行我的程序時,它只是立即崩潰。 logcat說我有一個cursorIndexOutofBounds錯誤。我不知道我在做什麼錯,因爲我不太擅長閱讀logcat輸出。

我已將「cmudict.0.7a.sqlite3」放置在與AndroidManifest.xml位於同一級別的資產目錄中。如果在需要引用sqlite3文件的任何xml文件中需要做其他聲明,我不太肯定。我不知道數據庫文件是否被發現,但似乎是這樣,因爲在/ data/data/packageName /數據庫中我有一個cmudict.0.7a文件。

/* 
* File: DatabaseHandler.java 
*/ 
public class DatabaseHandler extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "cmudict.0.7a.sqlite"; 
    private static final String TABLE_DICTS = "cmudict"; 
    private static final String KEY_ID = "id"; 
    private static final String KEY_WORD = "word"; 
    private static final String KEY_CODE = "code"; 

    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String CREATE_WORDS_TABLE = "CREATE TABLE " + TABLE_DICTS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_WORD + " TEXT," 
      + KEY_CODE+ " TEXT" + ")"; 
     db.execSQL(CREATE_WORDS_TABLE); 
    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_DICTS); 

     // Create tables again 
     onCreate(db); 
    } 


    public void addWord(Word word) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_WORD, word.getWord()); // 
     values.put(KEY_CODE, word.getCode()); // 

     // Inserting Row 
     db.insert(TABLE_DICTS, null, values); 
     db.close(); // Closing database connection 
    } 

    public Word getWord(String word) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     String sqlQuery = "SELECT "+KEY_CODE+" FROM "+TABLE_DICTS+" WHERE "+KEY_WORD+"=?"; 
     String[] r = {word}; 

     Cursor cursor = db.rawQuery(sqlQuery,r); 

     // ERROR on this line I think: cursorIndexOutofBounds 
     Word aWord = new Word(cursor.getString(0), cursor.getString(1)); 

     // Can't reach here. 
     Log.d("!!!!!MAJOR AVOIDED ERROR HERE", "SHIIIIIIT"); 

     // return contact 
     return aWord; 
    } 
} 

這裏是一類稱爲字包含在SQLite數據庫的單個行(即ID,字,代碼)的屬性。以及獲取者和設置者

/* 
* File: Word.java 
* Desription: SQLite3 database has columns "_id", "word", "code" (Arpabet representation). This class encapsulates a single row of the sqlite database 
*/ 
public class Word{ 

    //private variables 
    int _id; 
    String _word; 
    String _code; 

    // Empty constructor 
    public Word(){ 

    } 
    // Overridden constructor 
    public Word(int id, String word, String code){ 
     this._id = id; 
     this._word = word; 
     this._code= code; 
    } 

    // constructor 
    public Word(String word, String code){ 
     this._word = word; 
     this._code= code; 
    } 
    // getting ID 
    public int getID(){ 
     return this._id; 
    } 

    // setting id 
    public void setID(int id){ 
     this._id = id; 
    } 

    // getting word 
    public String getWord(){ 
     return this._word; 
    } 

    // setting word 
    public void setWord(String word){ 
     this._word = word ; 
    } 

    // getting code 
    public String getCode(){ 
     return this._code; 
    } 

    // setting code 
    public void setCode(String code){ 
     this._code = code; 
    } 
} 

這是活動。我只是用它來測試是否可以得到「Word」列對應的「代碼」列以打印到logcat。

/* 
* File: MyActivity.java 
*/ 
public class MyActivity extends Activity { 
    @Override 
    public void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     DatabaseHandler db = new DatabaseHandler(this); 
     Word wrd = db.getWord("HELLO");      // I simply want to query the database for the word "HELLO", which I know for a fact is in the database because I can find it using desktop app. 
     Log.d("!!!!!!!!!!!",Word.getString().toString()); 
    } 
} 

我知道它有很多代碼要求有人閱讀,所以提前謝謝。我出來的想法,我很新的Android開發和SQLite

回答

2

默認Cursor指向第一個結果行之前的索引。在調用遊標上的任何列獲取器之前,您需要將其移動到所需的行。

如果你希望一個結果行,使用這樣的:

if (cursor.moveToFirst()) { 
    // use row data from cursor here 
} 

如果你有許多潛在的結果行,使用的財產以後這樣的:

while (cursor.moveToNext()) { 
    // use row data from cursor here 
} 
+0

我試過你」 ve說,把這個if語句放在rawQuery方法調用之後,但我仍然得到錯誤,引用了Java NullPointerException。它在聲明wrd.getCode()方法調用時聲明瞭它。然後logcat顯示一堆「權限被拒絕:checkComponentPermission()owningUid = 10037」行。 –

+0

好吧,我得到的應用程序完全讀取sqlite文件,但只有我通過adb push替換/data/data/com.ex.app/database/sqlitefile.sqlite。爲什麼我的sqlite文件沒有與應用程序一起安裝? –