我目前正在尝试研究如何将SQLiteDatabase用于词汇应用。目前我只有一个测试数据库来查看它是否正常工作。显然它不起作用:
06-05 20:29:14.942 21532-21532/de.dev.jana.applicationtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.dev.jana.applicationtest, PID: 21532
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: android.database.sqlite.SQLiteException: no such table: test (code 1): , while compiling: SELECT * FROM test
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at de.dev.jana.applicationtest.Model.getFirstFolderName(Model.java:27)
at de.dev.jana.applicationtest.MainActivity.onButtonClick(MainActivity.java:38)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我需要在不同的活动中访问数据库,因此我需要使数据库可以全局访问。因此,我创建了一个创建DatabaseHelper的应用程序。从那里,所有活动都可以访问数据库。
申请表:
public class MyApplication extends Application {
private static SQLiteDatabase database;
@Override
public void onCreate() {
super.onCreate();
DatabaseHelper dbHelper = DatabaseHelper.getInstance(this);
database = dbHelper.getReadableDatabase();
}
public static SQLiteDatabase getReadableDatabase() {
return database;
}
}
DatabaseHelper:
class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper instance = null;
private DatabaseHelper(Context context) {
super(context, "test.db", null, 1);
}
public static DatabaseHelper getInstance(Context context) {
if(instance == null)
instance = new DatabaseHelper(context);
return instance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在另一个类中,我尝试使用数据库中的信息。当我尝试查询数据库时,我得到了异常。它说表格不存在。但它确实存在于数据库文件中。我多次检查过这个。
当我按下按钮时,会在某个时刻调用getFirstFolderName()方法。
public class Model {
private SQLiteDatabase db;
public Model() {
db = MyApplication.getReadableDatabase();
}
public String getFirstFolderName() {
String query = "select * from test;";
Cursor cursor = db.rawQuery(query, null); // <- here is the problem
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex("text");
String name = cursor.getString(columnIndex);
cursor.close();
return name;
}
}
我已经搜索了很多关于这个问题的信息。在所有其他示例中,他们已在onCreate方法中创建了数据库。但我正在从资产文件夹中打开一个文件。有问题吗?
我见过如何打开数据库的旧例子,但我猜这些已经过时了。可以通过将正确的文件名传递给超级构造函数SQLiteOpenHelper来打开数据库文件吗?在API中写入&#34;该类负责打开数据库(如果存在),如果不存在则创建它,并根据需要进行升级。事务用于确保数据库始终处于合理状态。&#34; 或者我必须手动输入,例如在this示例中(从2009年开始)?
编辑:我找到了解决方案。愚蠢的简单。我现在使用libary SQLiteAssetHelper。