首先,我想说我已经阅读了大多数(如果不是全部)有关同一问题的其他主题,并尝试了尽可能多的解决方案但是无济于事。
我正在尝试使用3个表创建一个SQLDatabase。 First表及其相关方法都可以正常工作(insertData,getAllData,deleteRow)。然而,第二个表是问题所在,每当我尝试插入一行时,它都会抛出"没有这样的表异常"。
以下是创建表格的代码
private static final String DATABASE_NAME = "Information";
private static final int DATABASE_VERSION = 2;
//Allergy Table
private static final String TABLE_NAME_A = "AllergyTable";
private static final String NAME = "Allergy";
private static final String UIDA = "_id";
private static final String CREATE_TABLE_A = "CREATE TABLE " + TABLE_NAME_A + "(" + UIDA + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR(255));";
private static final String DROP_TABLE_A = "DROP TABLE IF EXISTS " + TABLE_NAME_A;
//Medication Table
private static final String TABLE_NAME_M = "MedicationsTable";
private static final String MEDICATION = "Medication";
private static final String DOSAGE = "Dosage";
private static final String UIDM = "_id";
private static final String CREATE_TABLE_M = "CREATE TABLE " + TABLE_NAME_M + "(" + UIDM + " INTEGER PRIMARY KEY AUTOINCREMENT, " + MEDICATION + " VARCHAR(255)), " + DOSAGE + " VARCHAR(255));";
private static final String DROP_TABLE_M = "DROP TABLE IF EXISTS " + TABLE_NAME_M;
//Conditions Table
private static final String TABLE_NAME_C = "ConditionsTable";
private static final String CONDITION = "Condition";
private static final String INFO = "Info";
private static final String UIDC = "_id";
private static final String CREATE_TABLE_C = "CREATE TABLE " + TABLE_NAME_C + "(" + UIDC + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CONDITION + " VARCHAR(255)), " + INFO + " VARCHAR(255));";
private static final String DROP_TABLE_C = "DROP TABLE IF EXISTS " + TABLE_NAME_C;
这些是我的onCreate和onUpgrade方法:
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_A);
db.execSQL(CREATE_TABLE_M);
db.execSQL(CREATE_TABLE_C);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE_A);
db.execSQL(DROP_TABLE_M);
db.execSQL(DROP_TABLE_C);
onCreate(db);
} catch (SQLException e) {
e.printStackTrace();
}
}
这是我正在使用的插入方法:
//Medication Table
public long insertDataMeds(String meds, String dosage) {
if(meds.length() > 0) {
SQLiteDatabase db = sqlHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(sqlHelper.MEDICATION, meds);
cv.put(sqlHelper.DOSAGE, dosage);
long id = db.insert(sqlHelper.TABLE_NAME_M, null, cv);
return id;
}
return -1;
}
以下混乱是我的LogCat文件。
FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: no such table: MedicationsTable (code 1): , while compiling: SELECT Medication, Dosage FROM MedicationsTable
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
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:1314)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
at com.medinfo.database.SQLDataBaseAdapter.getAllDataMeds(SQLDataBaseAdapter.java:73)
at com.medinfo.main.EditMedication.onClick(EditMedication.java:46)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我已经尝试在模拟器和物理设备上运行应用程序,我尝试过对应用程序进行干净的重建,我还卸载并重新安装了应用程序。我还增加了数据库版本号。似乎没什么用。而且我不知道为什么第一张表和它的方法都应该正常工作。