我整天都在尝试编写一个超基本的 Android 应用程序,当给定一个字符串时,它会读取/搜索我从 CMUDict 的 133000 单词词典生成的 sqlite3 数据库文件的“单词”列,(在下面的示例中,我搜索code 列条目对应于“Hello”在 word 列中的行)并输出到 logcat 相应的条目。我知道我有一些方法可以在数据库中设置值,但我的目标是简单地搜索我已经拥有的数据库。
在终端,我可以做
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/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、单词、代码)。以及 getter 和 setter
/*
* 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”列对应的“Code”列打印到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 还很陌生