2017-07-25 45 views
-1

我已經創建了一個包含表格的數據庫。當我嘗試提取表中存在的數據時,Android Studio會顯示錯誤,指出該表不存在。表不存在錯誤Android

我提到這些鏈接求助

  1. SQL table not found exception
  2. http://mobisys.in/blog/2012/01/tutorial-using-database-in-android-applications/

第一個環節沒有回答我的問題,第二個鏈接是太複雜了。

這是我的代碼的摘錄。

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

是什麼的'NoteEntry.TABLE_NAME_NOTES' – akhilesh0707

+0

@AkhileshPatil,NoteEntry.TABLE_NAME_NOTES =「筆記」 –

+0

有你叫你DBHelper類的onCreate任何地方創造價值的數據庫中的表? –

回答

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); 
} 
+0

仍然無法使用? – akhilesh0707

+0

你可以試試這個參考https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – akhilesh0707

+0

謝謝!有效!並且我認爲我沒有將PRIMARY KEY添加到_ID列。我的錯。但非常感謝Akhilesh –