2016-12-15 165 views
0

你好,我有我的類,它擴展SQLiteOpenHelper創造的onCreate表中的問題,下面的代碼:SQLite的錯誤創建表

public class EntriesDatabaseHelper extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_NAME = "accounts.db"; 
    private static final String TABLE_NAME = "diary"; 
    private static final String COLUMN_ID = "id"; 
    private static final String COLUMN_TITLE = "title"; 
    private static final String COLUMN_DETAILS = "details"; 
    private static final String COLUMN_SAVE_DATE = "save_date"; 
    private static final String COLUMN_USER_ID = "user_id"; 
    SQLiteDatabase db; 

    private static final String TABLE_CREATE = "create table " + TABLE_NAME 
     + " (" + COLUMN_ID + " integer primary key not null, " 
     + COLUMN_TITLE + " text not null, " 
     + COLUMN_DETAILS + " text not null, " 
     + COLUMN_SAVE_DATE + " text not null, " 
     + COLUMN_USER_ID + " integer not null);"; 


    public EntriesDatabaseHelper(Context context){ 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(TABLE_CREATE); 
     this.db = db; 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int i, int i1) { 
     String query = "DROP TABLE IF EXISTS " + TABLE_NAME; 
     db.execSQL(query); 
     this.onCreate(db); 
    } 

    public void insertEntry(DiaryEntries entries){ 
     db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 

     String query = "SELECT * FROM " + TABLE_NAME; 
     Cursor cursor = db.rawQuery(query, null); 
     int count = cursor.getCount(); 
     values.put(COLUMN_ID, count); 
     values.put(COLUMN_TITLE, entries.getTitle()); 
     values.put(COLUMN_DETAILS, entries.getDetails()); 
     values.put(COLUMN_SAVE_DATE, entries.getDate()); 
     values.put(COLUMN_USER_ID, entries.getUser_id()); 

     db.insert(TABLE_NAME, null, values); 
     db.close(); 

    } 

} 

Error: android.database.sqlite.SQLiteException: no such table: diary (code 1): , while compiling: SELECT * FROM diary

充分的logcat:

FATAL EXCEPTION: main 
       android.database.sqlite.SQLiteException: no such table: diary (code 1): , while compiling: SELECT * FROM diary 
       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:1314) 
       at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
       at elec4.app.android.diaryapp.EntriesDatabaseHelper.insertEntry(EntriesDatabaseHelper.java:55) 
       at elec4.app.android.diaryapp.WriteDiary$1.onClick(WriteDiary.java:81) 
       at android.view.View.performClick(View.java:4240) 
       at android.view.View$PerformClick.run(View.java:17721) 
       at android.os.Handler.handleCallback(Handler.java:730) 
       at android.os.Handler.dispatchMessage(Handler.java:92) 
       at android.os.Looper.loop(Looper.java:137) 
       at android.app.ActivityThread.main(ActivityThread.java:5103) 
       at java.lang.reflect.Method.invokeNative(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:525) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
       at dalvik.system.NativeStart.main(Native Method) 

這裏的該代碼在哪裏使用類:

EntriesDatabaseHelper helper = new EntriesDatabaseHelper(this); 
DiaryEntries entries = new DiaryEntries(); 
      entries.setTitle(data_title); 
      entries.setDetails(data_details); 
      entries.setDate(date); 
      entries.setUser_id(Integer.parseInt(user_id)); 

      helper.insertEntry(entries); 
      //inserted into the database 
      finish(); 

它應該已經創建了表格,但它怎麼沒有。幫助謝謝

+1

嘗試改變DATABASE_VERSION版本從1到2 –

+0

OK生病嘗試後運行它,U傢伙應該回答不作評論等等你可以得分 –

+0

你可以找到類似的qsn很多答案:) @ρяσѕρєяK有足夠的代表:) – Raghavendra

回答

0

確定有人發表評論來改變數據庫版本,我有第一類使用database_version = 1,我改變了值爲2。我想問你爲什麼需要改變版本?對不起,挺新的在這裏的SQLite

+0

因爲如果數據庫之前是使用某個模式創建的,它不會更新,除非您增加版本號。你有一個沒有「日記」表的數據庫。 –

+0

這是因爲你寫的代碼 @Override public void onUpgrade(SQLiteDatabase db,int i,int i1){ String query =「DROP TABLE IF EXISTS」+ TABLE_NAME; db.execSQL(query); this.onCreate(db); } 如果你改變版本,它會調用onupgrade調用,否則沒有做任何數據庫更改 – Godwin

+0

@DiegoTorresMilano哦好吧,我現在明白了,謝謝 –

0

試試這個private static final String TABLE_CREATE = "create table " + TABLE_NAME + " (" + COLUMN_ID + " integer primary key not null, " + COLUMN_TITLE + " text not null, " + COLUMN_DETAILS + " text not null, " + COLUMN_SAVE_DATE + " text not null, " + COLUMN_USER_ID + " integer not null)";

和使用debugCompile 'com.amitshekhar.android:debug-db:0.4.0'檢查