在Android中读取const SQLite3 db文件

时间:2013-07-29 09:29:51

标签: android database sqlite

我一整天都在努力编写一个超级基本的Android应用程序,当给出一个字符串时,读取/搜索" word"我从CMUDict的133000字词字典生成的sqlite3数据库文件的列(在下面的示例中,我搜索对应于" Hello"在字列中的行的代码列条目)并输出到logcat相应的条目。我知道我有一些方法可以在数据库中设置值,但我的目标是只搜索我已经拥有的数据库。

在终端,我可以做到

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

搜索数据库。它返回正确的值。

但是当我运行我的程序时,它会立即崩溃。而logcat说我有一个cursorIndexOutofBounds错误。我不知道我做错了什么,因为我对读取logcat输出不太好。

我已放置" cmudict.0.7a.sqlite3"在与AndroidManifest.xml位于同一级别的assets目录中。如果我需要在任何必须引用sqlite3文件的xml文件中做出其他声明,那么我不太肯定。而且我不确定是否找到了数据库文件,但似乎是这样,因为在/ data / data / packageName / database中我有一个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;
    }
}

这是一个名为word的类,它包含sqlite数据库中单行的属性(即ID,word,code)。和吸气者和二传手

/*
 * 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;
    }
}

这是活动。我只是用它来测试我是否可以获得" Code"列为" Word" column对应于打印到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

相当陌生

1 个答案:

答案 0 :(得分:2)

默认情况下,Cursor指向第一个结果行之前的索引。在调用光标上的任何列getter之前,您需要将其移动到所需的行。

如果您期望单个结果行,请使用以下内容:

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

如果您可能有很多结果行,请使用以下内容:

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