我正在尝试使用onUpgrade()
添加新表。我已经给出了查询来创建一个表,如果oldVersion小于newVersion,则执行该表。我增加了数据库版本,但我NullPointerException
db.execSQL()
onUpgrade()
db
onUpgrade()
。
我检查调试模式并看到NullPointerException
中的newVersion(我的代码中为i2)仍为7(见代码),但我改为8.
为什么我在db
onUpgrade()
获得SQLiteDatabase db
{。}}。我已将onCreate()
中this.db = db
的值定义为onCreate(SQLiteDatabase db,..)
(我从方法onUpgrade
获得的值),并且我在构造函数中调用了SQLiteOpenHelper
类扩展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);
}
}
并将oldVersion和newVersion值传递给它。对不起,如果我试图解释太多让你困惑。我是android的新手,不知道这是否是向现有数据库添加新表的正确方法。
以下是代码:
DatabaseHelper.class
$.ajax({
url: "https://localhost:8443/v1/accounts",
type: 'GET',
dataType: 'jsonp',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Basic bHVpZ2lAZ21haWwuY29tOmFiYzEyMzQ1');
}
})
答案 0 :(得分:1)
在onUpgrade
中您使用了错误的引用:
db.execSQL("create table emptyTable(name text)");
而不是
sqLiteDatabase.execSQL("create table emptyTable(name text)");
您的构造函数将调用升级并在之前创建获取数据库引用,因此在升级时它仍然为null。
答案 1 :(得分:0)
当数据库版本增加时您必须通过onCreate
这样调用onUpgrade
来呼叫@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
:
assert req['status'] == 0