即使增加数据库版本也不会调用 onCreate()

onCreate() is not called even when Database version is increased

我正在尝试使用 onUpgrade() 添加新的 table。我已经给出了在其中创建一个 table 的查询,如果旧版本小于新版本,则执行该查询。我已经增加了数据库版本,但我在 onUpgrade() 中的 db.execSQL() 中得到了 NullPointerException for db

我在调试模式下查看并看到 onUpgrade() 中的新版本(我代码中的 i2)仍然是 7(见代码),尽管我已更改为 8。

为什么我在 onUpgrade()db 得到 NullPointerException。我已将 onCreate() 中的 SQLiteDatabase db 的值定义为 this.db = db(我从方法 onCreate(SQLiteDatabase db,..) 中获得的值)并且我在构造函数中调用了 onUpgrade class 扩展 SQLiteOpenHelper 并将 oldVersion 和 newVersion 值传递给它。抱歉,如果我试图解释太多而让您感到困惑。我是 android 的新手,不知道这是否是向现有数据库添加新 table 的正确方法。

代码如下:

DatabaseHelper.class

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "database";
SQLiteDatabase db;
ArrayList<String> arrayList;
Context cont;

DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.cont = context;
    arrayList = new ArrayList<>();

    db = this.getWritableDatabase();
    this.onUpgrade(db, 7, 8);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table tableName(name text,email text primary key,password varchar,phno int)");
    db.execSQL("create table bookingDetails(email text, date text, time text,vehicle text,fromLocation text,destination text)");
    db.execSQL("create table emptyTable (name text)");
    db.execSQL("insert into emptyTable values(ad)");
    this.db = db;
}

public boolean save(String name, String email, String password, String phno) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", name);
    values.put("email", email);
    values.put("password", password);
    values.put("phno", phno);
    long rows;
    rows = db.insert("tableName", null, values);
    if (rows <= 0) {
        Toast.makeText(cont, "Failed", Toast.LENGTH_LONG).show();
        return false;

    }
    return true;
}

public String getUserName(String email) {
    Cursor c = db.rawQuery("select name from tableName where email = '" + email + "'", null);
    c.moveToFirst();
    return c.getString(0);

}

public void saveBookingDetails(String email, String date, String time, String vehicleType, String fromLocation, String destination) {
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("email", email);
    values.put("date", date);
    values.put("time", time);
    values.put("vehicle", vehicleType);
    values.put("fromLocation", fromLocation);
    values.put("destination", destination);
    long rows;
    rows = db.insert("bookingDetails", null, values);
    if (rows <= 0) {
        Toast.makeText(cont, "Failed", Toast.LENGTH_LONG).show();
    } else
        Toast.makeText(cont, "Saved", Toast.LENGTH_LONG).show();
}


public void delete() {
    db.execSQL("delete from bookingDetails");
}

public ArrayList displayUserDetails(String email) {
    int i = 0;
    Cursor cursor = db.rawQuery("select name,email,phno from tableName where email = '" + email + "'", null);
    cursor.moveToFirst();
    do {
        arrayList.add(cursor.getString(cursor.getColumnIndex("name")));
        arrayList.add(cursor.getString(cursor.getColumnIndex("email")));
        arrayList.add(cursor.getString(cursor.getColumnIndex("phno")));
        i++;
    } while (cursor.moveToNext());
    return arrayList;
}

public boolean getLogin(String email, String pass) {
    db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select password from tableName where email = '" + email + "'", null);
    if (cursor.moveToFirst() && cursor.getCount() > 0 && cursor != null) {
        if (cursor.getString(0).equals((pass))) {
            return true;
        }
    }
    return false;

}

public long getTaskCount() {
    return DatabaseUtils.queryNumEntries(getReadableDatabase(), "tableName");
}

public ArrayList getBookingHistory(String email) {
    ArrayList bookingHistoryList = new ArrayList();
    String e = email;
    Cursor c = db.rawQuery("select * from bookingDetails where email = '" + email + "'", null);
    int cou = c.getCount();
    c.moveToFirst();
    do {
        bookingHistoryList.add(c.getString(c.getColumnIndex("date")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("time")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("fromLocation")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("destination")));
        bookingHistoryList.add(c.getString(c.getColumnIndex("vehicle")));
    }
    while (c.moveToNext());
    return bookingHistoryList;
}

public ArrayList getTextViewText() {
    ArrayList txtViewList = new ArrayList();
    Cursor cursor = db.rawQuery("select * from tableName", null);
    cursor.moveToFirst();
    do {
        txtViewList.add(cursor.getString(cursor.getColumnIndex("name")));
        txtViewList.add(cursor.getString(cursor.getColumnIndex("email")));
        txtViewList.add(cursor.getString(cursor.getColumnIndex("phno")));
    } while (cursor.moveToNext());

    return txtViewList;
}

public void saveOnEdit(String email, String name, String phno, String password) {
    ContentValues values = new ContentValues();


    if (name.length() == 0) {
        if (phno.length() == 0) {
            values.put("password", password);
            db.update("tableName", values, "email = '" + email + "'", null);
        } else if (password.length() == 0) {
            values.put("phno", phno);
            db.update("tableName", values, "email = '" + email + "'", null);
        } else {
            values.put("phno", phno);
            values.put("password", password);
        }
    } else if (password.length() == 0 && phno.length() == 0) {
        values.put("name", name);
        db.update("tableName", values, "email = '" + email + "'", null);
    } else if (password.length() == 0) {
        values.put("name", name);
        values.put("phno", phno);
        db.update("tableName", values, "email = '" + email + "'", null);
    } else if (phno.length() == 0) {
        values.put("name", name);
        values.put("password", password);
        db.update("tableName", values, "email = '" + email + "'", null);
    } else {
        values.put("name", name);
        values.put("password", password);
        values.put("phno", phno);
        db.update("tableName", values, "email = '" + email + "'", null);
    }
}

public String empty() {
    Cursor c = db.rawQuery("select name from emptyTable", null);
    return c.getString(c.getColumnIndex("name"));
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
    if (i < i2) {
        db.execSQL("create table emptyTable(name text)");
        ContentValues values = new ContentValues();
        values.put("name", "ad");
        db.insert("emptyTable", null, values);
    }

}

@Override
public void onConfigure(SQLiteDatabase db) {
    super.onConfigure(db);
}

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    super.onDowngrade(db, oldVersion, newVersion);
}

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
}
}

onUpgrade 中,您使用了错误的参考:

    db.execSQL("create table emptyTable(name text)");

而不是

    sqLiteDatabase.execSQL("create table emptyTable(name text)");

您的构造函数将调用升级并在您获得数据库引用之前创建,因此升级时它仍然为空。

当数据库版本增加时,您必须通过在 onUpgrade 中调用它来调用 onCreate,如下所示:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    onCreate(db);
}