有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更改数据库版本后出现java错误

我在数据库中更改了一些table Meeting和minutes字段,并将数据库版本从1更改为3。当我在emulator上运行应用程序时,我得到一个错误,即表仍然存在。我怎样才能放下桌子

日志目录:

05-31 15:30:32.338: E/AndroidRuntime(20347): 安卓.database.sqlite.SQLiteException: table     Meeting already exists: , while compiling: create table Meeting (_id integer primary key   autoincrement, meet text not null, venue text not null, agenda text not null, time text not null,  date text not null, phoneNo text not null, email text not null);
05-31 15:30:32.338: E/AndroidRuntime(20347):    at 安卓.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)

数据库代码:

public class DBUserAdapter 
{ 
public static final String KEY_ROWID = "_id"; 
private static final String TAG = "DBAdapter";
public static final String KEY_ROWID2 = "_id1"; 
private static final String DATABASE_NAME = "usersdb";
private static final String DATABASE_TABLE = "Meeting";
private static final String DATABASE_TABLE1 = "minutes";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_CREATE =
    "create table Meeting (_id integer primary key autoincrement, "
    + "meet text not null, "
    + "venue text not null, "
    + "agenda text not null, "
    + "time text not null, "
    + "date text not null, "
    + "phoneNo text not null, "
    + "email text not null);";

private static final String MINUTES_TABLE_CREATE =
        "create table minutes (_id1 integer primary key autoincrement, "
        + "meet text not null, "
        + "minutes text not null,";

       private SQLiteDatabase db;
     public DBUserAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);
        db.execSQL(MINUTES_TABLE_CREATE);
    }


     @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE ");
        db.execSQL("DROP TABLE IF EXISTS DATABASE_TABLE1 ");
        onCreate(db);
    }
}    

我不知道onupgrade()是如何工作的? 我该怎么办?放下桌子


共 (1) 个答案

  1. # 1 楼答案

    Change this    
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
    

    其他桌子也一样。您想要的是DATABASE_TABLE的值,而不是字符串“DATABASE_TABLE”