Android Studio SQLite数据库错误没有这样的表

时间:2017-04-20 19:12:02

标签: android sqlite android-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;

    }


}

1 个答案:

答案 0 :(得分:0)

您似乎最初在模拟器/设备上运行该应用程序,然后将user_table添加到onCreate()。在这种情况下,您尚未更新数据库版本。
在模拟器/设备上卸载应用程序,然后运行项目(将重新安装应用程序)。或者,您也可以只更新数据库版本。

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