0

当时我的应用程序在移动设备中加载时,数据库存储在以下路径中:

DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";

我认为这个数据库存储在缓存中。

现在的问题是,当我更新数据库时,假设我添加一个表并启动我的应用程序,那么旧版本的数据库不会用新版本更新。所以它给了我 SQLException。

当我启动我的应用程序时,如果它在缓存中,有什么方法可以更新数据库?

编辑

请告诉我我的代码有什么问题。这是我的代码:

public class DataBaseAdapter {
protected static final String TAG = "DataAdapter";

private final Context mContext;
private SQLiteDatabase mDb;
private DatabaseHandler mDbHelper;

public DataBaseAdapter(Context context) {
    this.mContext = context;
    mDbHelper = new DatabaseHandler(mContext);
}

public DataBaseAdapter createDatabase() throws SQLException {
    try {
        mDbHelper.createDataBase();
    } catch (IOException mIOException) {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public DataBaseAdapter open() throws SQLException {
    try {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } catch (SQLException mSQLException) {
        Log.e(TAG, "open >>" + mSQLException.toString());
        throw mSQLException;
    }
    return this;
}

public void close() {
    mDbHelper.close();
}



public Cursor getTestData() {
    try {
        String sql = "SELECT EmployeeId, Name, Email FROm Employees";

        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur != null) {
            mCur.moveToNext();
        }
        return mCur;
    } catch (SQLException mSQLException) {
        Log.e(TAG, "getTestData >>" + mSQLException.toString());
        throw mSQLException;
    }
}
}

这是DatabaseHandler.java文件

public class DatabaseHandler extends SQLiteOpenHelper {
private static String TAG = "DatabaseHandler"; // Tag just for the LogCat
                                                // window
// destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME = "ItemManager";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;

public DatabaseHandler(Context context) {
    super(context, DB_NAME, null, 1);// 1? its Database Version
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    this.mContext = context;
}

public void createDataBase() throws IOException {
    // If database not exists copy it from the assets

    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        try {
            // Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

// Check that the database exists here: /data/data/your package/databases/Da
// Name
private boolean checkDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);
    // Log.v("dbFile", dbFile + "   "+ dbFile.exists());
    return dbFile.exists();
}

// Copy the database from assets
private void copyDataBase() throws IOException {
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer)) > 0) {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

// Open the database, so we can query it
public boolean openDataBase() throws SQLException {
    String mPath = DB_PATH + DB_NAME;
    // Log.v("mPath", mPath);
    mDataBase = SQLiteDatabase.openDatabase(mPath, null,
            SQLiteDatabase.CREATE_IF_NECESSARY);
    // mDataBase = SQLiteDatabase.openDatabase(mPath, null,
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    return mDataBase != null;
}

@Override
public synchronized void close() {
    if (mDataBase != null)
        mDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase arg0) {

}

MainActivity.java文件的 OnCreate() 方法中:

DataBaseAdapter mDbHelper = new DataBaseAdapter(this);
    mDbHelper.createDatabase();
    mDbHelper.open();

编辑

原木猫

10-18 19:33:56.000: E/AndroidRuntime(14285): java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.example.demo/com.example.demo.EmployeeActivity}: android.database.sqlite.SQLiteException: no such table: Video: , while compiling: SELECT  * FROM Video WHERE EMPID=1
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.os.Looper.loop(Looper.java:130)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at java.lang.reflect.Method.invokeNative(Native Method)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at java.lang.reflect.Method.invoke(Method.java:507)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at dalvik.system.NativeStart.main(Native Method)
10-18 19:33:56.000: E/AndroidRuntime(14285): Caused by: android.database.sqlite.SQLiteException: no such table: Video: , while compiling: SELECT  * FROM Video WHERE EMPID=1
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1358)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1326)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at com.example.demo.DatabaseHandler.getAllVideos(DatabaseHandler.java:151)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at com.example.demoEmployeeActivity.setAdapter(VideoPlayActivity.java:52)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at com.example.demo.EmployeeActivity.onCreate(VideoPlayActivity.java:42)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 19:33:56.000: E/AndroidRuntime(14285):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

提前致谢。

4

1 回答 1

0

您将希望在升级数据库时将其删除,以便您的代码可以自动重新创建它。为此,添加

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // This database is only a cache for online data, so its upgrade policy is
    // to simply to discard the data and start over
    db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
    createDataBase();
}

然后,每当您升级版本号时,数据库将被删除并重新创建。

注意:我还没有实际测试过,但这应该可以。

于 2013-11-06T03:26:44.493 回答