Android sqlite database copy error in real HTC phone
I have to set up 2 database for one application. I did & working fine in the emulator.Emulator minSDKVersion is : 8 real phone version is :2.2.1. It support. When I setup the database in real phone I couldn't set up. Always I am getting unable create DB.I think one is rooted one, because when I go to File explore -> data. I couldn't open data folder.
I have doubt in apk, because My application size is 8 MB.It contain database in asset folder. That size is 4.65 MB. But apk file size is 1.14MB.
How it will read from asset folder(In the real phone)?
I really don't know whether it is wrong or right..These are my doubt.Please explain me.
I got error this :
Please help me.Is there any mistake my coding part or other than anything?
Please help me.
Thanks in advance....
Edited
This is my code for copyDatabase:
public class DataBaseMasterHelper extends SQLiteOpenHelper {
// The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.android.xont.controller/databases/";
private static String DB_NAME = "MasterUserVentura_HEMA.db";
private SQLiteDatabase ventura;
private Context myContext;
private DataBaseHelper myDbHelper;
public DataBaseMasterHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
**/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
File Path = myContext.getDir("Data", 0);
File DBFile = new File(Path, DB_NAME);
System.out.println("-------" + DB_NAME);
if(!DBFile.exists()) //Need to copy...
copyDataBase(myContext, DBFile);
}
public boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase(Context Ctxt, File DBFile) 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 {
String myPath = DB_PATH + DB_NAME;
ventura = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (ventura != null)
super.close();
ventura.close();
}
and calling like this:
MyGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id) {
if (position == 0) {
try {
myDbMasterHelper.createDataBase();
Toast.makeText(DatabaseSetupActivity.this, "Successfully copied database",Toast.LENGTH_SHORT).show();
} catch (IOException ioe) {
Toast.makeText(DatabaseSetupActivity.this, "Unable to create database",Toast.LENGTH_SHORT).show();
ioe.printStackTrace();
}catch (Exception ioe) {
Toast.makeText(DatabaseSetupActivity.this, "Unable to create database",Toast.LENGTH_SHORT).show();
ioe.printStackTrace();
}
}else if (position == 1) {
try {
myDbHelper.createDataBase();
Toast.makeText(DatabaseSetupActivity.this, "Successfully copied database",Toast.LENGTH_SHORT).show();
} catch (IOException ioe) {
Toast.makeText(DatabaseSetupActivity.this, "Unable to create database",Toast.LENGTH_SHORT).show();
ioe.printStackTrace();
}catch (Exception ioe) {
Toast.makeText(DatabaseSetupActivity.this, "Unable to create database",Toast.LENGTH_SHORT).s开发者_StackOverflow中文版how();
ioe.printStackTrace();
}
}
}
});
working fine the emulator. If the file size problem, then should have get error in emulator also?
I got error like this:
java.io.FileNotFoundException: /data/data/com.android.xont.controller/databases /MasterUserVentura_HEMA.db (No such file or directory)
I have changed e.printStackTrace() instead of throw expecption.
I think,This is not file size problem.because I did same thing in small size 300kb(testng2.db).That one also giving same error
My real phone is HTC HD2 its Windows Mobile® 6.5 Professional.
Please help me.
Thanks in advance...
The problem is you cannot read the data from the asset folder. Make sure the DB_NAME
has form
"mydatabase.db"
(including the extension). Try using lower cases, it might be case sensitive.
Thanks for posting your source code, i see problem in method: copyDataBase()
because wrong path of DB_NAME
, correct is "databases/MasterUserVentura_HEMA.db"
Try this:
InputStream myInput = myContext.getAssets().open("databases/MasterUserVentura_HEMA.db");
精彩评论