我的数据库出现了一些问题,一旦我尝试从数据库中获取某些内容或将某些内容写入数据库,我就会将其实施到我的应用中。我还发现导致崩溃的线路,但我真的不知道如何解决它。
以下是我的数据库代码+错误消息
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "routeLogBook";
// Table Names
private static final String TABLE_DRIVE = "drive";
private static final String TABLE_ROUTE = "route";
private static final String TABLE_CAR = "car";
private static final String TABLE_FUEL = "fuel";
private static final String TABLE_FUELCALC = "fuelcalc";
// Drive
private static final String KEY_DRIVE_ID = "did";
private static final String KEY_DRIVE_BEGIN = "begin";
private static final String KEY_DRIVE_END = "end";
private static final String KEY_DRIVE_CAR = "car";
// Route
private static final String KEY_ROUTE_ID = "rid";
private static final String KEY_ROUTE_DATE = "date";
private static final String KEY_ROUTE_LATITUDE = "latitude";
private static final String KEY_ROUTE_LONGITUDE = "longitude";
private static final String KEY_ROUTE_DRIVE = "drive";
// Car
private static final String KEY_CAR_ID = "cid";
private static final String KEY_CAR_NAME = "name";
private static final String KEY_CAR_MARQUE = "marque";
private static final String KEY_CAR_FUEL = "fuel";
// Fuel
private static final String KEY_FUEL_ID = "fid";
private static final String KEY_FUEL_FUEL = "fuel";
// Table Create Statements
// Drive table create statement
private static final String CREATE_TABLE_DRIVE = "CREATE TABLE "
+ TABLE_DRIVE + "("
+ KEY_DRIVE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_DRIVE_BEGIN + " TEXT,"
+ KEY_DRIVE_END + " TEXT,"
+ KEY_DRIVE_CAR + " INTEGER,"
+ " FOREIGN KEY (" + KEY_DRIVE_CAR
+ ") REFERENCES " + TABLE_CAR + " (" + KEY_CAR_ID + ")" + ");";
// Route table create statement
private static final String CREATE_TABLE_ROUTE = "CREATE TABLE "
+ TABLE_ROUTE + "("
+ KEY_ROUTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_ROUTE_DATE + " TEXT,"
+ KEY_ROUTE_LATITUDE + " TEXT,"
+ KEY_ROUTE_LONGITUDE + " TEXT,"
+ KEY_ROUTE_ID + " INTEGER,"
+ " FOREIGN KEY (" + KEY_ROUTE_DRIVE
+ ") REFERENCES " + TABLE_DRIVE + " (" + KEY_DRIVE_ID + ")" + ");";
// Car table create statement
private static final String CREATE_TABLE_CAR = "CREATE TABLE "
+ TABLE_CAR + "("
+ KEY_CAR_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_CAR_NAME + " TEXT,"
+ KEY_CAR_MARQUE + " TEXT,"
+ KEY_CAR_FUEL + " INTEGER,"
+ " FOREIGN KEY (" + KEY_CAR_FUEL
+ ") REFERENCES " + TABLE_FUEL + " (" + KEY_FUEL_ID + ")" + ");";
// Fuel table create statement
private static final String CREATE_TABLE_FUEL = "CREATE TABLE "
+ TABLE_FUEL + "(" + KEY_FUEL_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FUEL_FUEL + " TEXT"
+ ");";
// FuelCalc table create statement
// TODO
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
if (!db.isReadOnly()) {
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_FUEL);
db.execSQL(CREATE_TABLE_CAR);
db.execSQL(CREATE_TABLE_DRIVE); This one and the one the line below are the cause of the crashes i think
db.execSQL(CREATE_TABLE_ROUTE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_FUEL);
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_CAR);
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_DRIVE);
db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_ROUTE);
// create new tables
onCreate(db);
}
// close database
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}
}
错误日志,
01-23 13:12:33.530: E/SQLiteOpenHelper(880): Couldn't open routeLogBook for writing (will try read-only):
01-23 13:12:33.530: E/SQLiteOpenHelper(880): android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
答案 0 :(得分:3)
android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
当您的代码尝试使用版本1打开数据库时,您的设备有一个版本为2的数据库文件。您不提供onDowngrade()
回调,因此会抛出异常。
只需删除旧数据库即可创建新数据库。卸载应用程序将删除数据库文件。