我创建了一个包含表格的数据库。当我尝试提取表中的数据时,Android Studio会显示一条错误消息,指出该表不存在。
我在这些链接中提到了帮助
第一个链接没有回答我的问题,第二个链接太复杂了。
以下是我的代码摘录。
DbHelper.java:
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase database) {
String CREATE_TABLE_NOTES_QUERY = "CREATE TABLE " +
NoteEntry.TABLE_NAME_NOTES + " (" +
NoteEntry._ID_NOTES + " INTEGER NOT NULL AUTOINCREMENT, " +
NoteEntry.COLUMN_COUNTRY + " TEXT NOT NULL, " +
NoteEntry.COLUMN_DENOMINATION + " REAL NOT NULL DEFAULT 0, " +
NoteEntry.COLUMN_YEAR + " INTEGER, " +
NoteEntry.COLUMN_OBVERSE_DESCRIPTION + " TEXT DEFAULT \"Not Available\", " +
NoteEntry.COLUMN_REVERSE_DESCRIPTION + " TEXT DEFAULT \"Not Available\", " +
NoteEntry.COLUMN_LENGTH + " REAL DEFAULT 0.0, " +
NoteEntry.COLUMN_BREADTH + " REAL DEFAULT 0.0, " +
NoteEntry.COLUMN_THICKNESS + " REAL DEFAULT 0.0);";
database.execSQL(CREATE_TABLE_NOTES_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
NoteActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NoteActivity.this, NoteEditorActivity.class);
startActivity(intent);
}
});
displayDatabaseInfo();
}
private void displayDatabaseInfo() {
CollectoDbHelper mDbHelper = new CollectoDbHelper(this);
SQLiteDatabase db = mDbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + CollectoContract.NoteEntry.TABLE_NAME_NOTES, null);
try {
TextView displayView = (TextView) findViewById(R.id.textview_note);
displayView.setText("Number of rows in notes table: " + cursor.getCount());
} finally {
cursor.close();
}
}
logcat的:
07-25 14:55:57.357 9162-9162/com.example.android.collecto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.collecto, PID: 9162
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.collecto/com.example.android.collecto.NoteActivity}: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT * FROM notes
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2762)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: no such table: notes (code 1): , while compiling: SELECT * FROM notes
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1284)
at com.example.android.collecto.NoteActivity.displayDatabaseInfo(NoteActivity.java:49)
at com.example.android.collecto.NoteActivity.onCreate(NoteActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6743)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2715)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2848)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6334)
at java.lang.reflect.Method.invoke(Native Method)
有人可以指导我解决我的错误吗?
答案 0 :(得分:1)
尝试拨打onUpgrade()
并在内部拨打onCreate()
;
另请卸载应用并重新安装
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_NOTES_QUERY);
// Create tables again
onCreate(db);
}