如何从资产使用preloaded SQLite数据库Android中 [英] How to use preloaded SQLite database in Android from Assets
问题描述
我想用 preloaded数据库
在我的应用程序手段试图获取数据库中的APK安装,以便可以使用已经保存在该数据的时间。我复制的ingredients.db
文件中的资产的文件夹中。并使用以下code
但得到的错误的问题复制数据库从资源文件
我该如何解决这个问题呢? 请给我建议的方法可能
我的数据库辅助类就是这样
类IngredientHelper扩展SQLiteOpenHelper {
私有静态最后弦乐DATABASE_PATH =/data/data/com.example.$p$ploadeddatabase/databases/;
私有静态最后弦乐DATABASE_NAME =ingredients.db;
私有静态最后弦乐TABLE_NAME =成分;
私有静态最后弦乐COLUMN_ID =_id;
私有静态最后弦乐COLUMN_TITLE =ingredient_name;
私有静态最终诠释SCHEMA_VERSION = 1;
公共SQLiteDatabase dbSqlite;
私人最终语境myContext;
公共IngredientHelper(上下文的背景下){
超(背景下,DATABASE_NAME,空,SCHEMA_VERSION);
this.myContext =背景;
}
@覆盖
公共无效的onCreate(SQLiteDatabase DB){
}
@覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
}
公共无效的CreateDatabase(){
CREATEDB();
}
公共无效CREATEDB(){
布尔dbExist = DbExists();
如果(!dbExist){
this.getReadableDatabase();
copyDataBase();
}
}
私人布尔DbExists(){
SQLiteDatabase DB = NULL;
尝试 {
字符串databasePath = DATABASE_PATH + DATABASE_NAME;
DB = SQLiteDatabase.openDatabase(databasePath,空,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(真正的);
db.setVersion(1);
}
赶上(SQLiteException E){
Log.e(把SqlHelper,数据库中找不到);
}
如果(DB!= NULL){
db.close();
}
返回分贝!= NULL?真假;
}
私人无效copyDataBase(){
的InputStream的IStream = NULL;
的OutputStream的ostream = NULL;
字符串outFilePath = DATABASE_PATH + DATABASE_NAME;
尝试 {
的IStream = myContext.getAssets()开(DATABASE_NAME)。
ostream的=新的FileOutputStream(outFilePath);
byte []的缓冲区=新的字节[2048];
INT长;
而((长度= iStream.read(缓冲液))大于0){
oStream.write(缓冲液,0,长度);
}
oStream.flush();
oStream.close();
iStream.close();
}
赶上(IOException异常E){
抛出新的错误(问题复制数据库从资源文件);
}
}
公共无效的openDatabase()抛出的SQLException {
字符串mypath中= DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(mypath中,空,
SQLiteDatabase.OPEN_READWRITE);
}
@覆盖
市民同步无效的close(){
如果(dbSqlite!= NULL){
dbSqlite.close();
}
super.close();
}
公共光标getCursor(){
SQLiteQueryBuilder QueryBuilder的=新SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
的String [] asColumnsToReturn =新的String [] {COLUMN_ID,COLUMN_TITLE};
光标mCursor = queryBuilder.query(dbSqlite,asColumnsToReturn,
NULL,NULL,NULL,NULL,ingredient_name ASC);
返回mCursor;
}
公共字符串的getName(光标C){
返程(c.getString(1));
}
不要打开读取数据库,直到你的资源文件复制到数据库目录之后。很可能你得到的异常,因为你要写入一个文件,你已经打开的(因为它被打开,当你调用getReadableDatabase())。
I want to use preloaded database
in my app means trying to get database at the time the apk is installed so can use the data already saved in that. I copy the "ingredients.db"
file in the assets folder. And Use the following code
but this get the error " Problem Copying Database From Resource File "
How can i solve this problem? please suggest me the ways possible
My Database Helper class is like that
class IngredientHelper extends SQLiteOpenHelper {
private static final String DATABASE_PATH = "/data/data/com.example.preloadeddatabase/databases/";
private static final String DATABASE_NAME = "ingredients.db";
private static final String TABLE_NAME = "Ingredients";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TITLE = "ingredient_name";
private static final int SCHEMA_VERSION = 1;
public SQLiteDatabase dbSqlite;
private final Context myContext;
public IngredientHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase() {
createDB();
}
public void createDB() {
boolean dbExist = DbExists();
if (!dbExist) {
this.getReadableDatabase();
copyDataBase();
}
}
private boolean DbExists() {
SQLiteDatabase db = null;
try {
String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null,
SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
}
catch (SQLiteException e) {
Log.e("SqlHelper", "Database Not Found");
}
if (db != null) {
db.close();
}
return db != null ? true : false;
}
private void copyDataBase() {
InputStream iStream = null;
OutputStream oStream = null;
String outFilePath = DATABASE_PATH + DATABASE_NAME;
try {
iStream = myContext.getAssets().open(DATABASE_NAME);
oStream = new FileOutputStream(outFilePath);
byte[] buffer = new byte[2048];
int length;
while ((length = iStream.read(buffer)) > 0) {
oStream.write(buffer, 0, length);
}
oStream.flush();
oStream.close();
iStream.close();
}
catch (IOException e) {
throw new Error("Problem Copying Database From Resource File");
}
}
public void openDatabase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if (dbSqlite != null) {
dbSqlite.close();
}
super.close();
}
public Cursor getCursor() {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(TABLE_NAME);
String[] asColumnsToReturn = new String[] { COLUMN_ID, COLUMN_TITLE };
Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn,
null, null, null, null, "ingredient_name ASC");
return mCursor;
}
public String getName(Cursor c) {
return (c.getString(1));
}
Don't open the readable database until after you copy the resource file to your database directory. It is likely that you're getting the exception since you're trying to write to a file you already have open (since it gets opened when you call getReadableDatabase()).
这篇关于如何从资产使用preloaded SQLite数据库Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!