2017-04-20 109 views
-1

我想創建一個表並添加用戶到它當註冊,然後一旦註冊我讓他們登錄即時通訊錯誤。當我點擊登錄按鈕或註冊按鈕時,應用程序崩潰,但我認爲問題出在數據庫幫助程序類上。以下是錯誤:Android Studio SQLite數據庫錯誤沒有這樣的表

E/AndroidRuntime: FATAL EXCEPTION: main 
        Process: ie.wit.budget, PID: 3915 
        android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: SELECT username, password from user 
         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:1318) 
         at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257) 
         at ie.wit.SQL.DatabaseHelper.checkUser(DatabaseHelper.java:72) 
         at ie.wit.budget.LoginActivity$1.onClick(LoginActivity.java:39) 
         at android.view.View.performClick(View.java:5610) 
         at android.view.View$PerformClick.run(View.java:22265) 
         at android.os.Handler.handleCallback(Handler.java:751) 
         at android.os.Handler.dispatchMessage(Handler.java:95) 
         at android.os.Looper.loop(Looper.java:154) 
         at android.app.ActivityThread.main(ActivityThread.java:6077) 
         at java.lang.reflect.Method.invoke(Native Method) 
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

DatabaseHelper類:

public class DatabaseHelper extends SQLiteOpenHelper{ 

    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    //Database Name 
    private static final String DATABASE_NAME = "User.db"; 

    //Table Created 
    private static final String TABLE_USER = "user"; 

    //Columns in Table 
    private static final String COLUMN_ID = "id"; 
    private static final String COLUMN_USERNAME = "username"; 
    private static final String COLUMN_EMAIL = "email"; 
    private static final String COLUMN_PASS = "password"; 
    SQLiteDatabase db; 

    private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "(" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_USERNAME + " TEXT," + 
      COLUMN_EMAIL + " TEXT," + COLUMN_PASS + " TEXT " + ")"; 

    private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER; 

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

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL(DROP_USER_TABLE); 
     this.onCreate(db); 

    } 

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

    public void insertUser(User user){ 
     db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     String query = "select * from user"; 
     Cursor cursor = db.rawQuery(query, null); 
     int count = cursor.getCount(); 
     values.put(COLUMN_ID, count); 
     values.put(COLUMN_USERNAME, user.getUsername()); 
     values.put(COLUMN_EMAIL, user.getEmail()); 
     values.put(COLUMN_PASS, user.getPassword()); 

     db.insert(TABLE_USER, null, values); 
     db.close(); 
    } 
    public String checkUser(String username){ 
     db = this.getReadableDatabase(); 
     String query = "SELECT username, password from " + TABLE_USER; 
     Cursor cursor = db.rawQuery(query, null); 

     String a, b; 
     b = "error"; 
     if(cursor.moveToFirst()){ 
      do{ 
       a = cursor.getString(0); 


       if(a.equals(username)){ 
        b = cursor.getString(1); 
        break; 
       } 
      }while(cursor.moveToNext()); 
     } 

     return b; 

    } 


} 

回答

0

好像你已經開始運行模擬器/設備上的應用程序,然後添加user_tableonCreate()。在這種情況下,您沒有更新數據庫版本。
要麼在您的模擬器/設備上卸載應用程序,然後運行該項目(這將重新安裝應用程序)。或者,您也可以更新數據庫版本。

//i.e 
private static final int DATABASE_VERSION = 2; 
+0

乾杯謝謝你的幫助:) – chris123

相關問題