public void copyDbToInternalFolder() throws IOException {
String DB_PATH = Environment.getDataDirectory().getPath()+getPackageName()+"/Databases/"; // Don't worry I know this line doesn't work, however, nothing was working
String DB_NAME = "Client_Responses.db";
InputStream myInput = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS,"Client_Responses.db"); //needs to get database file from Downloads folder
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();
}
I have written several related apps that create sqlite database files in the devices Downloads folder. I need to stick to this method in order to support all the way back to first generation tablets. API 8 in essence.
Now, the manager needs to import found files from the Downloads folder and import them to its own database folder.
The above code doesn't work, null pointer exceptions and incorrect filename/pathname create errors and the app will force quit, and I have tried what seems like everything. My thinking is now bottlenecked.
I have been struggling with this and would welcome input.
Note: I will most likely duplicate the code in different class files, each copying a differing database name.
private void copyDbToInternalFolder() throws IOException {
String ToDB_PATH = "/data/data/"+this.getPackageName()+"/databases/";
String sourcePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Client_Responses.db";
File source = new File(sourcePath);
String destinationPath = ToDB_PATH + "/" + DB_NAME;
File destination = new File(destinationPath);
try
{
FileUtils.copyFile(source, destination);
}
catch (IOException e)
{
e.printStackTrace();
}
}
This seems to work on Android4.2.2 tablet but not on the emulator. I cannot work out whether the issue is the emulator and how it handles the path creation, or if its API8 that cannot understand them. If anyone has any suggestions I would be grateful. So the issue is only partially answered.
Related
Is there a way to copy sqlite database from external storage to assets at run time and then use it ? or is there a way to copy the ready sqlite from external storage to below path at run time and then read it in app? context.getDatabasepath. I have problem with the copying process, please help me because I got this error :
can't open file
My question may look like a duplicate but I couldn't find the answer.
public void copyDataBase() throws IOException
{
try
{
String inputDB = Environment.getExternalStorageDirectory().getPath() + "/test.sqlite" ;
File input = new File(inputDB);
InputStream myInput = new FileInputStream(input);
String outputFileName = databasePath + DATABASE_NAME ;
OutputStream myoutput = new FileOutputStream(outputFileName) ;
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 (Exception e)
{
e.printStackTrace();
}
}
I think the file should be opened first then we give it to the input stream because error was like : can't open the file. Please help me.
This question already has answers here:
How do I view the SQLite database on an Android device? [duplicate]
(19 answers)
Closed 7 years ago.
How can I get the database of my application running on my phone.
I use Android Studio and my database is in assets>MyDataBase.db
Is there a way to update this file ?
I have my own sqlite database, I insert data then I want to get the file back with the new data.
I don't use the emulator and my database was created by SQLite Browser.
[EDIT]
People said it was a duplicate of this question.
But I want to do the same in code line not in command.
If you want to edit file in assets folder of your app in Runtime - this is not possible. you should copy this DB to internal/external storage and edit it after that.
Yes you can edit in in many ways. One of them is using libraries such as this. Give it a look and try it in your app.
You cannot write data's to asset/Raw folder, since it is packed(.apk), the assets folder is read-only at runtime.
You need to choose a different storage location, here is a method to backup you db to your sdcard (external storage) dbName=MyDataBase.db in your case:
public static void backupDBonSDcard(Context context, String dbName){
String DB_PATH = context.getDatabasePath(dbName).getPath();
Log.d("DB_PATH:" + DB_PATH);
if(checkDataBase(DB_PATH)){
InputStream myInput;
try {
Log.e("[backupDBonSDcard] saving file to SDCARD");
myInput = new FileInputStream(DB_PATH);
// Path to the just created empty db
String outFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + java.io.File.separator + dbName;
//Open the empty db as the output stream
OutputStream myOutput;
try {
myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
} catch (FileNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//Close the streams
if(myOutput!=null){
myOutput.flush();
myOutput.close();
}
if(myInput!=null)
myInput.close();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
Log.d("DB "+dbName+" not found");
}
}
public static boolean checkDataBase(String fileName) {
java.io.File dbFile = new java.io.File(fileName);
return dbFile.exists();
}
Don't forget to add the following permission in your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am developing a simple database application.
I want to see the inserted data. I have follow this instructions to view the data.
But in eclipse the SQLite manage is hidden. Please let me any idea to show view the data.
Open view Window->Show View->File Explorer.
In this view go to data/data/"your app name"/databases/"your database" This is you database file.
For me, I use SQLite Database Browser.
Here, you can finf a good tutorial to know how to use it exactly.
Open the DDMS perspective and select the emulator, open the path: /data/data/your.app.name/databases/your.db and click the db file.
The icon will be then enabled
[UPDATE]
I noticed you are using ALL CAPS file names (...).
But I guess the plugin is case sensitive.
Try to make (at least) the extension SMALL CAPS: .db
Suppose If you have phone then you can copy the database to sdCard
private static String DB_NAME = "db_name";
private String DB_PATH = Environment.getDataDirectory().getAbsolutePath()
+ "/data/package_name/databases/";
private void copyToSdCard() throws IOException {
byte[] buffer = new byte[1024];
int length;
String outFileName = "/sdcard/file_name.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
InputStream myInput;
myInput = new FileInputStream(DB_PATH + DB_NAME);
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myInput.close();
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
Then using mozila firefox add the add-on sqlitemanager and you can check your db. please replace the package name and db name.
Ive created an app that stores user login information into an sqlite database. However when i try to access it using Root Explorer i get an error.
An error occurred while opening the database. disk i/o error (code 3850):, while compiling: Select * from sqlite_master where type in('table','view') order by name.
The app works fine, and it is storing the log in information as required. However i cant access it on the root explorer?
Any suggestions?
please call this function:
public static void copyDataBase(Context mActivity) throws IOException {
InputStream myInput = new FileInputStream(new File("/data/data/"
+ mActivity.getPackageName() + "/databases/" + "yourdb.sqlite"));
File files = new File("/sdcard/files/");
files.mkdirs();
String outFileName = "/sdcard/files/your.sqlite";
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bufferLength;
while ((bufferLength = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, bufferLength);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
your database is copied /sdcard/files/your.sqlite this path.
You could add a button in your app to view the database information or write the database information to a file in external storage.
Otherwise, maybe you need to root your device to access the database.
Just give permissions to all users to the file [YOUR_DB].db-journal
For me it worked.
I am currently trying to access my database using my android phone however it doesn't work. It does work on the emulator. So I was wondering if I need to copy the data into my phone's internal memory by using
try{
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}catch(SQLiteException e){
Another issue is that I did not use "SQLiteOpenHelper", so does the above method still work? I simply did a
private final String DB_NAME = "MemberData";
private final String TABLE_NAME = "MemberDB";
sampleDB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
Cursor c = sampleDB.rawQuery("SELECT companyNameEng FROM " +
TABLE_NAME + " ORDER BY companyNameEng ASC", null);
Yes, you must copy the db file out of the apk and into your apps database directory (or somewhere else that your app can access). The assets directory is not really a directory when the .apk file is created... it's just data that certain functions in the framework (getAssets()) know how to access. To allow other functions that expect a real path to work, you need to copy the data out to a proper directory on the device.
The usual destination for a database is "data/data/your.package.name/databases/".
Your method looks like will work fine... it is standard java io. SQLiteOpenHelper is more for helping you do upgrades and create new dbs. I always use one (never know if I'm going to change how I do things even if I don't need it initially), but you certainly don't have to.