Android SQLite数据库无法降级

时间:2015-12-11 11:45:59

标签: android sqliteopenhelper

提前感谢您回答我的问题。在过去的两天里,我一直在努力解决这个(这些)问题而且我真的很疯狂。让我告诉你我的故事, 我使用带有SimpleCursorAdapter的List以及从SQLite数据库中提取的信息。我是Android(2周)的新手,也是Java的新手。我很高兴地意识到我已经实现了我想要的一切,用我的小应用程序。

昨天,我发现我的数据库表的架构没有很好的结构。所以我试着改变它。

根据SO上的一个提示,我在onUpgrade方法中插入了ALTER查询,并将Dabase版本设置为更高的数字,以便触发该方法。从数据库版本1开始,我将其设为2,然后是3.我一直收到同样的错误:在表XXX中找不到COLUMN XXX。因此ALTER查询显然没有被触发。

我将版本设置回1,然后更改错误:无法降低数据库。我试图手动删除数据库。但由于d *文件夹权限,我无法访问它。我尝试按照一些教程来更改Windows Shell的文件权限。无用。意思是我无法更改权限。

此外,我尝试使用命令context.deleteDatabase(Databasename)删除数据库。之后我用适配器和SQLiteHelper扩展类重命名了Java类文件。我得到了同样的错误。

一遍又一遍!无法降级数据库。我该怎么办?

请!也许将所有内容导出到一个干净的新项目? :o 这是我祝福的课程代码!

public class VerbsAdapter{
//declare all the verb fields for the data columns

public static final String KEY_ROWID = "_id";
public static final String KEY_VERBNAME = "verbname";
public static final String KEY_FPERSSG = "firstperssg";
public static final String KEY_FPERSPL = "firstperpl";
public static final String KEY_SPERSSG = "secondperssg";
public static final String KEY_SPERSPL = "secondperspl";
public static final String KEY_TPERSSG = "thirdperssg";
public static final String KEY_TPERSPL = "thirdperspl";
public static final String TAG = "tag";

private static final String DB_NAME = "LearnGermanDB";
private static final String TABLE_NAME = "TableVerb";
private static final int DB_VERSION = 1;

private DataBaseHelper mDbHelper;
private SQLiteDatabase mDb;

private final Context mCtx;

private static final String DATABASE_CREATE_QUERY = "CREATE TABLE if not exists "+TABLE_NAME+ "(" + KEY_ROWID + " integer PRIMARY KEY autoincrement,"+TAG+","+KEY_VERBNAME +
        ","+KEY_FPERSSG + "," +KEY_FPERSPL + "," + KEY_SPERSSG + "," + KEY_SPERSPL + "," + KEY_TPERSSG + "," + KEY_TPERSPL+ ");";

    //extend the SQLiteOpenHelper
    //implement onCreate and onUpgrade

    private static class DataBaseHelper extends SQLiteOpenHelper{
        DataBaseHelper(Context context){
            super(context, DB_NAME,null,DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.w(TAG, DATABASE_CREATE_QUERY);
            db.execSQL(DATABASE_CREATE_QUERY);
        }

        @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 " + TABLE_NAME);
            onCreate(db);
        }
    }

    public VerbsAdapter(Context context){
        this.mCtx = context;
    }


    public VerbsAdapter open(){
        mDbHelper = new DataBaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    public void close() {
        if (mDbHelper != null) {
        mDbHelper.close();
        }
    }


   public void deleteDatabase(){
        mCtx.deleteDatabase(DB_NAME);
        Log.w(TAG,"Database DELETED");
        Toast.makeText(mCtx,"Database deleted",Toast.LENGTH_SHORT).show();
    }

    public long createVerb(String s1, String s2, String s3, String s4, String s5,String s6,String s7){
        ContentValues initialValues = new ContentValues();
        initialValues.put(TAG,"verb");
        initialValues.put(KEY_VERBNAME,s1);
        initialValues.put(KEY_FPERSSG,"ich "+s2);
        initialValues.put(KEY_FPERSPL,"wir "+s3);
        initialValues.put(KEY_SPERSSG,"du "+s4);
        initialValues.put(KEY_SPERSPL,"ihr "+s5);
        initialValues.put(KEY_TPERSSG,"er "+s6);
        initialValues.put(KEY_TPERSPL,"sie,Sie "+s7);

        return mDb.insert(TABLE_NAME,null, initialValues);
    }

    public boolean deleteAllVerbs() {

        int doneDelete = 0;
        doneDelete = mDb.delete(TABLE_NAME, null , null);
        Log.w(TAG, Integer.toString(doneDelete));
        return doneDelete > 0;

    }

    public Cursor fetchAllVerbs(){
        Cursor mCursor = mDb.query(TABLE_NAME,new String[] {KEY_ROWID,KEY_VERBNAME,
                KEY_FPERSSG,KEY_FPERSPL,KEY_SPERSSG,KEY_SPERSPL,KEY_TPERSSG,KEY_TPERSPL},null,null,null,null,null);
        if (mCursor!=null){
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public void insertVerbs(){
        createVerb("machen = to make , conjugated with: HABEN","mache","machen","machst","macht","macht","machen");
        createVerb("tun = to do, conjugated with HABEN","tue","tun","tust","tut","tut","tun");
        createVerb("essen = to eat, conjugated with HABEN","esse","essen","isst","esst","isst","essen");
    }
}

0 个答案:

没有答案