无法在某些手机上复制数据库

时间:2014-09-30 20:17:04

标签: android sqlite

我正在使用以下代码将sqlite databaseasset文件夹复制到/data/data/{package_name}/databases/,并且它在我测试过的大多数手机(HTC,三星,索尼,华为)上运行正常但是它在某些手机上不起作用(如LG G2)

public void createDatabase() throws IOException {
    boolean dbExist = checkDatabase();
    try {
        if (dbExist)
            deleteDatabase();

        this.getReadableDatabase();
        copyDatabase();
    } catch (IOException e) {
        Log.v("db", e.toString());
        throw new Error(e.getMessage());
    }
}

我也试过下面的代码,而不是使用this.getReadableDatabase();

private void createDirectory() {
    File dbDirectory = new File(DB_PATH);
    if (!dbDirectory.exists())
        dbDirectory.mkdirs();
}
但结果相同,问题是什么?我该如何解决呢?

修改 完整代码:

public class SQLiteDBHelper extends SQLiteOpenHelper
{
private final Context myContext;

public SQLiteDBHelper(Context context) {
    super(context, G.DB_NAME, null, 1);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and overwrite it with your own
 * database.
 **/
public void createDatabase() throws IOException {
    boolean dbExist = checkDatabase();
    if (dbExist) {
        try {
            deleteDatabase();
        } catch (IOException e) {
            Log.v("db", e.toString());
            throw new Error("Error copying database");
        }
    }
    // if (!dbExist) {
    try {
    super.getReadableDatabase();

        copyDatabase();
    } catch (IOException e) {
        Log.v("db", e.toString());
        throw new Error("Error copying database");
    }
    // }
}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDatabase() {
    SQLiteDatabase checkDB = null;

    try {
        checkDB = SQLiteDatabase.openDatabase(G.DB_FULL_PATH, null,
                SQLiteDatabase.OPEN_READONLY
                        | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    } catch (SQLiteException e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}

private void copyDatabase() throws IOException {
    String[] dbFiles = myContext.getAssets().list(G.ASSETS_DB_FOLDER);
    String outFileName = G.DB_FULL_PATH;
    Log.v("db", dbFiles[0]);
    Log.v("db", outFileName);
    InputStream in = myContext.getAssets().open(
            G.ASSETS_DB_FOLDER + "/" + dbFiles[0]);
    OutputStream out = new FileOutputStream(outFileName);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();

    /*
     * OutputStream myOutput = new FileOutputStream(outFileName); //for(int
     * i =0; i < dbFiles.length; i++) { InputStream myInput =
     * myContext.getAssets().open(G.ASSETS_DB_FOLDER+"/"+dbFiles[0]); byte[]
     * buffer = new byte[1024]; int length; while ((length =
     * myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); }
     * myInput.close(); //} myOutput.flush(); myOutput.close();
     */
}

public void deleteDatabase() throws IOException {
    if (checkDatabase())
        G.context.deleteDatabase(G.DB_FULL_PATH);
}

public void openDatabase() throws SQLException {
    // Open the database
    G.database = SQLiteDatabase.openDatabase(G.DB_FULL_PATH, null,
            SQLiteDatabase.OPEN_READONLY
                    | SQLiteDatabase.NO_LOCALIZED_COLLATORS);
}

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

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

@Override
public synchronized SQLiteDatabase getReadableDatabase() {
    try {
        createDatabase();
        openDatabase();
    } catch (SQLException e) {
        G.database = null;
        e.printStackTrace();
    } catch (IOException e) {
        G.database = null;
        e.printStackTrace();
    }
    return G.database;
}
}

它在this.getReadableDatabase()上给我一个IOException我觉得这个问题与sdcard有关

1 个答案:

答案 0 :(得分:0)

我认为问题是用于定位数据库的db文件的路径:

DB_PATH = Environment.getExternalStorageDirectory()+ context.getApplicationContext().getPackageName() +File.separatorChar+ "databases" + File.separatorChar + "/test.db";

并获得

/ data / data / pkgname / databases / dbname

使用以下

ContextWrapper c = new ContextWrapper(this);
String DBPath = c.getDatabasePath(DB_NAME).getPath();

我希望这有用。