Android中的SQLIte,来自Assets文件夹

时间:2022-02-23 00:26:01

I'm trying to use a database from the Assets Folder in my app, however I can't seem to reference it.

我试图使用我的应用中的Assets文件夹中的数据库,但是我似乎不能引用它。

I've got the structure setup like:

我有这样的结构设置:

public static String DBName = "myDB.db";
public static String DBLocation = "/data/data/com.yes.auntsally/databases/";
public static String DBFullLocation = DBLocation + DBName;

Then calling with:

然后调用:

public static void getDbFile() {
        if (dbFile == null) {
            dbFile = new File(Global.DBFullLocation);
        }
    }

public static void openDb() {
        if (db == null) {
            getDbFile();  
            db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
            setDbOpen(true);
        }
        if (!db.isOpen()) {
            getDbFile();  
            db = SQLiteDatabase.openOrCreateDatabase(dbFile, null);
            setDbOpen(true);
        }

    }

But i'm getting the error:

但我犯了一个错误:

04-02 11:51:31.375: E/SQLiteLog(4024): (14) cannot open file at line 30241 of [00bb9c9ce4]
04-02 11:51:31.375: E/SQLiteLog(4024): (14) os_unix.c:30241: (2) open(/data/data/com.yes.auntsally/databases/myDB.db) - 
04-02 11:51:31.390: E/SQLiteDatabase(4024): Failed to open database '/data/data/com.yes.auntsally/databases/myDB.db'.
04-02 11:51:31.390: E/SQLiteDatabase(4024): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

2 个解决方案

#1


2  

Try to refer below code

请参考下面的代码

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH;
private static String DB_NAME = "podcastDB.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
SharedPreferences sharedPreferences;


public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";

    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(myContext);
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    // ---Check whether database is already created or not---
    boolean dbExist = checkDataBase();

    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            // ---If not created then copy the database---
            copyDataBase();
        } catch (IOException e) {
            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() {
    try {
        String myPath = DB_PATH + DB_NAME;
        File f = new File(myPath);
        if (f.exists())
            return true;
        else
            return false;
    } catch (SQLiteException e) {
        Log.e("Podcast", "There was an error", e);
        return false;
    }
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {

    // --- Open the database---
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

#2


1  

Try this Its very simple library project done for the same

试试这个吧,它的非常简单的库项目也是这样做的

https://github.com/jgilfelt/android-sqlite-asset-helper

https://github.com/jgilfelt/android-sqlite-asset-helper

#1


2  

Try to refer below code

请参考下面的代码

public class DataBaseHelper extends SQLiteOpenHelper {

private static String DB_PATH;
private static String DB_NAME = "podcastDB.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
SharedPreferences sharedPreferences;


public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + myContext.getPackageName() + "/databases/";

    sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(myContext);
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    // ---Check whether database is already created or not---
    boolean dbExist = checkDataBase();

    if (dbExist) {
    } else {
        this.getReadableDatabase();
        try {
            // ---If not created then copy the database---
            copyDataBase();
        } catch (IOException e) {
            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() {
    try {
        String myPath = DB_PATH + DB_NAME;
        File f = new File(myPath);
        if (f.exists())
            return true;
        else
            return false;
    } catch (SQLiteException e) {
        Log.e("Podcast", "There was an error", e);
        return false;
    }
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();
}

public void openDataBase() throws SQLException {

    // --- Open the database---
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}

#2


1  

Try this Its very simple library project done for the same

试试这个吧,它的非常简单的库项目也是这样做的

https://github.com/jgilfelt/android-sqlite-asset-helper

https://github.com/jgilfelt/android-sqlite-asset-helper