问题描述
我无法SQLite数据库从资产产生文件夹复制到设备内存(试仿真器)。
I'm Unable to copy SQLite database from assests folder to device memory(trying on emulator).
我在我的项目的资产产生的文件夹的数据库具有含 pre-现有行的1000表
I have a database in my project's assests folder which has a table containing 1000s of pre-existing rows.
我打算从资产文件夹中现有的文件复制到模拟器的数据库文件夹。
I intend to copy the existing file from the assets folder into the database folder of emulator.
活动的片
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//All views(list,textboxes) are declared.
try {
dbM = new DbManager(this);
dbM.checkDataBase();
try {
dbM.createDataBase();
} catch (Exception e) {
throw new Error(" *** ERROR in DB Access *** " + e.getMessage());
}
dbM.openDB();
symbolarr = dbM.getSymbol();
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, symbolarr));
} catch (Exception e) {
throw new Error(" *** ERROR in onCreate *** " + e.getMessage());
}
finally {
dbM.close();
}
}
从我DbManager类code海贼王:
public boolean checkDataBase() {
String myPath = DATABASE_PATH + DATABASE_NAME;
File f = new File(myPath);
return f.exists();
}
public void createDataBase() {
try {
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
OutputStream myOutput = new FileOutputStream(DATABASE_PATH
+ DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (FileNotFoundException e) {
throw new Error("file not found -- " + e.getMessage());
} catch (IOException e) {
throw new Error("io exception " + e.getMessage());
} catch (Exception e) {
throw new Error(" exception " + e.getMessage());
}
}
public DbManager openDB() throws SQLException {
dbmgr = new DbManager(ctx);
mDb = dbmgr.getWritableDatabase();
return this;
}
public String[] getSymbol() {
Cursor cur;
try {
cur = mDb.rawQuery("select symbol,company_name from Scrip", null);
} catch (SQLiteException e) {
throw new Error(" *** ERROR in cursor *** " + e.getMessage());
}
String[] b1 = new String[1326];
int x = 0;
if (cur.moveToFirst()) {
do {
b1[x] = cur.getString(cur.getColumnIndex("symbol"));
x++;
} while (cur.moveToNext());
}
cur.close();
return b1;
}
的logcat
FATAL EXCEPTION: main
java.lang.Error: file not found -- AndroidDB.db
at com.dbexample.DbManager.createDataBase(DbManager.java:113)
at com.dbexample.DataAttach.onCreate(DataAttach.java:83)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
at android.app.ActivityThread.access$600(ActivityThread.java:122)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
注意
1:更改缓冲区大小没有帮助。
NOTE
1: Changing buffer size doesn't help..
2:有一个名为AndroidDB.db在我的资产文件夹大小62KB。
2: There is a file named AndroidDB.db with size 62kb in my assets folder..
3:两个数据库名称(在我的资产文件夹,并在我的code)是一样的,我在我的数据库表android_metadata这是资产的文件夹。
3: both the database names(in my assets folder and in my code) are same and I have android_metadata table in my DB which is in assets folder..
4:当我没有使用的 的CreateDatabase()
的,是越来越创建的数据库,但我的所需的表ieScrip没有得到复制。所以,当我试图从表中提取数据,我得到了一个异常没有这样的表以股代息
....
这意味着我需要从资产的文件夹复制以股代息
表数据库present模拟器上的内存。当我尝试做使用相同的的CreateDatabase()
,我得到的NullPointerException
4: When I was not using createDataBase()
, Database was getting created but my desired table i.e.Scrip was not getting copied. So when I tried to fetch the data from table, I was getting an exception that no such table Scrip
....That means I need to copy Scrip
table from assets folder to database present on emulator memory. When I try to do the same using createDataBase()
,I'm getting nullPointerException
5:当我试图遵循的CreateDatabase()code
5.: When I tried following code in createDatabase()
AssetManager assetManager = ctx.getAssets();
String[] files = assetManager.list("Files");
for (int i = 0; i < files.length; i++) {
str[i] = "\n=" + " file=" + " :" + i + "=" + " name=" + "> "
+ files[i];
}
Log.v("len=", "" + files.length);
然后files.length等于 0
。这只是意味着这是无法检测到资产的文件夹内的所有文件。
任何帮助将生命SAVER !!! 的
ANY HELP WILL BE LIFE-SAVER !!!
推荐答案
编辑:这个替换您的code,让我知道发生什么事,(这在我的情况能正常工作.. )
Replace your code with this and let me know what happen, (This works fine in my case..)
DbManager.java类:
public class DbManager {
private DatabaseHelper dataHelper;
private SQLiteDatabase mDb;
Context ctx;
String DATABASE_PATH = "/data/data/com.demo/databases/";
static String DATABASE_NAME="AndroidDB";
private static final int DATABASE_VERSION = 1;
DbManager(Context ctx)
{
this.ctx = ctx;
dataHelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
Context myContext = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w("DBHelper", "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
onCreate(db);
}
}
public boolean checkDataBase() {
String myPath = DATABASE_PATH + DATABASE_NAME;
File f = new File(myPath);
return f.exists();
}
public void createDataBase() {
openDB();
try {
InputStream myInput = ctx.getAssets().open("AndroidDB.db");
OutputStream myOutput = new FileOutputStream(DATABASE_PATH
+ "AndroidDB");
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
if (mDb.isOpen())
mDb.close();
myOutput.flush();
myOutput.close();
myInput.close();
} catch (FileNotFoundException e) {
throw new Error("file not found -- " + e.getMessage());
} catch (IOException e) {
throw new Error("io exception " + e.getMessage());
} catch (Exception e) {
throw new Error(" exception " + e.getMessage());
}
}
public DbManager openDB() throws SQLException {
mDb = dataHelper.getWritableDatabase();
return this;
}
public String[] getSymbol() {
Cursor cur;
try {
cur = mDb.rawQuery("select symbol,company_name from Scrip", null);
} catch (SQLiteException e) {
throw new Error(" *** ERROR in cursor *** " + e.getMessage());
}
String[] b1 = new String[1326];
int x = 0;
if (cur.moveToFirst()) {
do {
b1[x] = cur.getString(cur.getColumnIndex("symbol"));
x++;
} while (cur.moveToNext());
}
cur.close();
return b1;
}
public void close() {
try {
mDb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
这篇关于无法SQLite数据库从资产产生文件夹复制到设备内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!