Copy database file to local storage The 2019 Stack Overflow Developer Survey Results Are InHow to list the tables in a SQLite database file that was opened with ATTACH?Local Storage vs CookiesHTML5 Local storage vs. Session storageAndroid SQL Database - rawQuery() Source not FoundCopying data from one file to another file very slow in android?Android - Can't open asset fileHow to copy sqlite database populated with data in my application?NanoHTTPD. Cache InputStream to file and continue streamingCopy Database with Android 4.4Android file storage
Falsification in Math vs Science
writing variables above the numbers in tikz picture
Getting crown tickets for Statue of Liberty
"as much details as you can remember"
Why didn't the Event Horizon Telescope team mention Sagittarius A*?
Why not take a picture of a closer black hole?
What do these terms in Caesar's Gallic wars mean?
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Slides for 30 min~1 hr Skype tenure track application interview
Why couldn't they take pictures of a closer black hole?
Accepted by European university, rejected by all American ones I applied to? Possible reasons?
Is it okay to consider publishing in my first year of PhD?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
How to obtain a position of last non-zero element
Can there be female White Walkers?
Button changing its text & action. Good or terrible?
Pokemon Turn Based battle (Python)
Likelihood that a superbug or lethal virus could come from a landfill
Is Astrology considered scientific?
Is it correct to say the Neural Networks are an alternative way of performing Maximum Likelihood Estimation? if not, why?
Cooking pasta in a water boiler
What's the name of these plastic connectors
How do you keep chess fun when your opponent constantly beats you?
Copy database file to local storage
The 2019 Stack Overflow Developer Survey Results Are InHow to list the tables in a SQLite database file that was opened with ATTACH?Local Storage vs CookiesHTML5 Local storage vs. Session storageAndroid SQL Database - rawQuery() Source not FoundCopying data from one file to another file very slow in android?Android - Can't open asset fileHow to copy sqlite database populated with data in my application?NanoHTTPD. Cache InputStream to file and continue streamingCopy Database with Android 4.4Android file storage
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a sqlite database in assete folder.
I want copy this to local storage.
I use this code,it copy database without tables. when i want to open database in sqlliteeditor, i get error 14.
private void copyDataBase2() throws IOException
OutputStream os = new FileOutputStream( "/data/data/pakagename/Learning.db");
InputStream is = context.getAssets().open("databases/Learning.db");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
is.close();
os.flush();
os.close();
android sqlite local-storage
add a comment |
I have a sqlite database in assete folder.
I want copy this to local storage.
I use this code,it copy database without tables. when i want to open database in sqlliteeditor, i get error 14.
private void copyDataBase2() throws IOException
OutputStream os = new FileOutputStream( "/data/data/pakagename/Learning.db");
InputStream is = context.getAssets().open("databases/Learning.db");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
is.close();
os.flush();
os.close();
android sqlite local-storage
add a comment |
I have a sqlite database in assete folder.
I want copy this to local storage.
I use this code,it copy database without tables. when i want to open database in sqlliteeditor, i get error 14.
private void copyDataBase2() throws IOException
OutputStream os = new FileOutputStream( "/data/data/pakagename/Learning.db");
InputStream is = context.getAssets().open("databases/Learning.db");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
is.close();
os.flush();
os.close();
android sqlite local-storage
I have a sqlite database in assete folder.
I want copy this to local storage.
I use this code,it copy database without tables. when i want to open database in sqlliteeditor, i get error 14.
private void copyDataBase2() throws IOException
OutputStream os = new FileOutputStream( "/data/data/pakagename/Learning.db");
InputStream is = context.getAssets().open("databases/Learning.db");
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
os.write(buffer, 0, length);
is.close();
os.flush();
os.close();
android sqlite local-storage
android sqlite local-storage
edited Mar 22 at 6:36
VikaS
579219
579219
asked Mar 22 at 4:40
ar.gorginar.gorgin
2,14864375
2,14864375
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Try copying the DB file like this:
public class DbHelper extends SQLiteOpenHelper
private static String DB_PATH = new File(getFilesDir(), "databases").getPath();
private static String DB_NAME = "test_db.db";
private SQLiteDatabase dataBase;
private final Context fContext;
public DbHelper(Context context)
super(context, DB_NAME, null, 1);
this.fContext = context;
public void createDataBase() throws IOException
boolean dbExist = checkDataBase();
if (dbExist)
//ничего не делаем – файл базы данных уже есть
else
this.getReadableDatabase();
try
copyDataBase();
catch (IOException e)
throw new Error("Error copying database");
private boolean checkDataBase()
SQLiteDatabase checkDB = null;
try
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
catch (SQLiteException e)
//файл базы данных отсутствует
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
private void copyDataBase() throws IOException
InputStream input = fContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0)
output.write(buffer, 0, length);
output.flush();
output.close();
input.close();
public void openDataBase() throws SQLException
String path = DB_PATH + DB_NAME;
dataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
@Override
public synchronized void close()
if (dataBase != null)
dataBase.close();
super.close();
@Override
public void onCreate(SQLiteDatabase db)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
If you use API 19 or higher:
String db_path = fContext.getDatabasePath(DB_NAME);
When i want to open database in sqlliteeditor, i get error 14.
Also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Usenew File(getFilesDir(), "databases").getPath()
or something.
– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get errorjava.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
add a comment |
It's just file copying, it does not matters it's a database or what. Actually the code above is not aware about tables and therefore has no ability to copy database without tables.
You can compare hashcodes of the two db files. To do so, you can change your code to copy the file to external storage instead of your app's data directory for now. Then retrieve the file back to PC via adb and do the comparation.
According to your code, there aren't problems and they should be the same.
It's likely (either) the db file itself is broken (or) you're opening it the wrong way.
add a comment |
Try using this :
private void copyDB(Context context) throws Exception
File destinationDirectory = new File(Environment.getExternalStorageDirectory() + "/Destination/");
File destinationFile = new File(destinationDirectory + "/myDatabase.db");
if (!destinationDirectory.isDirectory())
destinationDirectory.mkdir();
destinationFile.createNewFile();
File dbFile = context.getDatabasePath("my_database.db");
if (dbFile.exists())
copyFile(dbFile.getPath(), logFile.getAbsolutePath(), false);
private boolean copyFile(String sPath, String dPath, boolean isDeleteSourceDir)
FileInputStream fis = null;
FileOutputStream fos = null;
try
File sourceFile = new File(sPath);
fis = new FileInputStream(sourceFile);
File dir = new File(dPath);
if (!dir.exists())
dir.mkdirs();
File outfile = new File(dPath);
outfile.delete();
if (!outfile.exists())
outfile.createNewFile();
fos = new FileOutputStream(outfile);
byte[] block = new byte[1000];
int i;
while ((i = fis.read(block)) != -1)
fos.write(block, 0, i);
fos.close();
if (isDeleteSourceDir)
deleteDirectory(new File(sPath));
return true;
catch (Exception e)
e.printStackTrace();
return false;
finally
fis.close();
fos.close();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293020%2fcopy-database-file-to-local-storage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try copying the DB file like this:
public class DbHelper extends SQLiteOpenHelper
private static String DB_PATH = new File(getFilesDir(), "databases").getPath();
private static String DB_NAME = "test_db.db";
private SQLiteDatabase dataBase;
private final Context fContext;
public DbHelper(Context context)
super(context, DB_NAME, null, 1);
this.fContext = context;
public void createDataBase() throws IOException
boolean dbExist = checkDataBase();
if (dbExist)
//ничего не делаем – файл базы данных уже есть
else
this.getReadableDatabase();
try
copyDataBase();
catch (IOException e)
throw new Error("Error copying database");
private boolean checkDataBase()
SQLiteDatabase checkDB = null;
try
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
catch (SQLiteException e)
//файл базы данных отсутствует
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
private void copyDataBase() throws IOException
InputStream input = fContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0)
output.write(buffer, 0, length);
output.flush();
output.close();
input.close();
public void openDataBase() throws SQLException
String path = DB_PATH + DB_NAME;
dataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
@Override
public synchronized void close()
if (dataBase != null)
dataBase.close();
super.close();
@Override
public void onCreate(SQLiteDatabase db)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
If you use API 19 or higher:
String db_path = fContext.getDatabasePath(DB_NAME);
When i want to open database in sqlliteeditor, i get error 14.
Also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Usenew File(getFilesDir(), "databases").getPath()
or something.
– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get errorjava.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
add a comment |
Try copying the DB file like this:
public class DbHelper extends SQLiteOpenHelper
private static String DB_PATH = new File(getFilesDir(), "databases").getPath();
private static String DB_NAME = "test_db.db";
private SQLiteDatabase dataBase;
private final Context fContext;
public DbHelper(Context context)
super(context, DB_NAME, null, 1);
this.fContext = context;
public void createDataBase() throws IOException
boolean dbExist = checkDataBase();
if (dbExist)
//ничего не делаем – файл базы данных уже есть
else
this.getReadableDatabase();
try
copyDataBase();
catch (IOException e)
throw new Error("Error copying database");
private boolean checkDataBase()
SQLiteDatabase checkDB = null;
try
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
catch (SQLiteException e)
//файл базы данных отсутствует
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
private void copyDataBase() throws IOException
InputStream input = fContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0)
output.write(buffer, 0, length);
output.flush();
output.close();
input.close();
public void openDataBase() throws SQLException
String path = DB_PATH + DB_NAME;
dataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
@Override
public synchronized void close()
if (dataBase != null)
dataBase.close();
super.close();
@Override
public void onCreate(SQLiteDatabase db)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
If you use API 19 or higher:
String db_path = fContext.getDatabasePath(DB_NAME);
When i want to open database in sqlliteeditor, i get error 14.
Also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Usenew File(getFilesDir(), "databases").getPath()
or something.
– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get errorjava.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
add a comment |
Try copying the DB file like this:
public class DbHelper extends SQLiteOpenHelper
private static String DB_PATH = new File(getFilesDir(), "databases").getPath();
private static String DB_NAME = "test_db.db";
private SQLiteDatabase dataBase;
private final Context fContext;
public DbHelper(Context context)
super(context, DB_NAME, null, 1);
this.fContext = context;
public void createDataBase() throws IOException
boolean dbExist = checkDataBase();
if (dbExist)
//ничего не делаем – файл базы данных уже есть
else
this.getReadableDatabase();
try
copyDataBase();
catch (IOException e)
throw new Error("Error copying database");
private boolean checkDataBase()
SQLiteDatabase checkDB = null;
try
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
catch (SQLiteException e)
//файл базы данных отсутствует
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
private void copyDataBase() throws IOException
InputStream input = fContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0)
output.write(buffer, 0, length);
output.flush();
output.close();
input.close();
public void openDataBase() throws SQLException
String path = DB_PATH + DB_NAME;
dataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
@Override
public synchronized void close()
if (dataBase != null)
dataBase.close();
super.close();
@Override
public void onCreate(SQLiteDatabase db)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
If you use API 19 or higher:
String db_path = fContext.getDatabasePath(DB_NAME);
When i want to open database in sqlliteeditor, i get error 14.
Also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
Try copying the DB file like this:
public class DbHelper extends SQLiteOpenHelper
private static String DB_PATH = new File(getFilesDir(), "databases").getPath();
private static String DB_NAME = "test_db.db";
private SQLiteDatabase dataBase;
private final Context fContext;
public DbHelper(Context context)
super(context, DB_NAME, null, 1);
this.fContext = context;
public void createDataBase() throws IOException
boolean dbExist = checkDataBase();
if (dbExist)
//ничего не делаем – файл базы данных уже есть
else
this.getReadableDatabase();
try
copyDataBase();
catch (IOException e)
throw new Error("Error copying database");
private boolean checkDataBase()
SQLiteDatabase checkDB = null;
try
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
catch (SQLiteException e)
//файл базы данных отсутствует
if (checkDB != null)
checkDB.close();
return checkDB != null ? true : false;
private void copyDataBase() throws IOException
InputStream input = fContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0)
output.write(buffer, 0, length);
output.flush();
output.close();
input.close();
public void openDataBase() throws SQLException
String path = DB_PATH + DB_NAME;
dataBase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
@Override
public synchronized void close()
if (dataBase != null)
dataBase.close();
super.close();
@Override
public void onCreate(SQLiteDatabase db)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
If you use API 19 or higher:
String db_path = fContext.getDatabasePath(DB_NAME);
When i want to open database in sqlliteeditor, i get error 14.
Also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
edited Mar 22 at 5:55
answered Mar 22 at 4:52
StreletzStreletz
14112
14112
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Usenew File(getFilesDir(), "databases").getPath()
or something.
– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get errorjava.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
add a comment |
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Usenew File(getFilesDir(), "databases").getPath()
or something.
– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get errorjava.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
Thanks, it is copy database without tables!!!
– ar.gorgin
Mar 22 at 5:25
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
@ar.gorgin, also I advise you to check the DB file. If an error occurs when you open it in the editor, it is likely that the problem is not in the file copy algorithm.
– Streletz
Mar 22 at 5:43
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
when i open with navicat, it don't have error. with this code, copy database without tables
– ar.gorgin
Mar 22 at 5:50
1
1
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Use
new File(getFilesDir(), "databases").getPath()
or something.– Meow Cat 2012
Mar 22 at 5:51
Don't hardcode the "/data/data/com.example.dbreadytest/databases/" thing. It can change in multi-user or container environment. Use
new File(getFilesDir(), "databases").getPath()
or something.– Meow Cat 2012
Mar 22 at 5:51
thanks, when use it, i get error
java.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
thanks, when use it, i get error
java.io.FileNotFoundException: /data/data/'domain name'/files/databases/Learning.db: open failed: ENOENT (No such file or directory)
– ar.gorgin
2 days ago
add a comment |
It's just file copying, it does not matters it's a database or what. Actually the code above is not aware about tables and therefore has no ability to copy database without tables.
You can compare hashcodes of the two db files. To do so, you can change your code to copy the file to external storage instead of your app's data directory for now. Then retrieve the file back to PC via adb and do the comparation.
According to your code, there aren't problems and they should be the same.
It's likely (either) the db file itself is broken (or) you're opening it the wrong way.
add a comment |
It's just file copying, it does not matters it's a database or what. Actually the code above is not aware about tables and therefore has no ability to copy database without tables.
You can compare hashcodes of the two db files. To do so, you can change your code to copy the file to external storage instead of your app's data directory for now. Then retrieve the file back to PC via adb and do the comparation.
According to your code, there aren't problems and they should be the same.
It's likely (either) the db file itself is broken (or) you're opening it the wrong way.
add a comment |
It's just file copying, it does not matters it's a database or what. Actually the code above is not aware about tables and therefore has no ability to copy database without tables.
You can compare hashcodes of the two db files. To do so, you can change your code to copy the file to external storage instead of your app's data directory for now. Then retrieve the file back to PC via adb and do the comparation.
According to your code, there aren't problems and they should be the same.
It's likely (either) the db file itself is broken (or) you're opening it the wrong way.
It's just file copying, it does not matters it's a database or what. Actually the code above is not aware about tables and therefore has no ability to copy database without tables.
You can compare hashcodes of the two db files. To do so, you can change your code to copy the file to external storage instead of your app's data directory for now. Then retrieve the file back to PC via adb and do the comparation.
According to your code, there aren't problems and they should be the same.
It's likely (either) the db file itself is broken (or) you're opening it the wrong way.
answered Mar 22 at 5:59
Meow Cat 2012Meow Cat 2012
393212
393212
add a comment |
add a comment |
Try using this :
private void copyDB(Context context) throws Exception
File destinationDirectory = new File(Environment.getExternalStorageDirectory() + "/Destination/");
File destinationFile = new File(destinationDirectory + "/myDatabase.db");
if (!destinationDirectory.isDirectory())
destinationDirectory.mkdir();
destinationFile.createNewFile();
File dbFile = context.getDatabasePath("my_database.db");
if (dbFile.exists())
copyFile(dbFile.getPath(), logFile.getAbsolutePath(), false);
private boolean copyFile(String sPath, String dPath, boolean isDeleteSourceDir)
FileInputStream fis = null;
FileOutputStream fos = null;
try
File sourceFile = new File(sPath);
fis = new FileInputStream(sourceFile);
File dir = new File(dPath);
if (!dir.exists())
dir.mkdirs();
File outfile = new File(dPath);
outfile.delete();
if (!outfile.exists())
outfile.createNewFile();
fos = new FileOutputStream(outfile);
byte[] block = new byte[1000];
int i;
while ((i = fis.read(block)) != -1)
fos.write(block, 0, i);
fos.close();
if (isDeleteSourceDir)
deleteDirectory(new File(sPath));
return true;
catch (Exception e)
e.printStackTrace();
return false;
finally
fis.close();
fos.close();
add a comment |
Try using this :
private void copyDB(Context context) throws Exception
File destinationDirectory = new File(Environment.getExternalStorageDirectory() + "/Destination/");
File destinationFile = new File(destinationDirectory + "/myDatabase.db");
if (!destinationDirectory.isDirectory())
destinationDirectory.mkdir();
destinationFile.createNewFile();
File dbFile = context.getDatabasePath("my_database.db");
if (dbFile.exists())
copyFile(dbFile.getPath(), logFile.getAbsolutePath(), false);
private boolean copyFile(String sPath, String dPath, boolean isDeleteSourceDir)
FileInputStream fis = null;
FileOutputStream fos = null;
try
File sourceFile = new File(sPath);
fis = new FileInputStream(sourceFile);
File dir = new File(dPath);
if (!dir.exists())
dir.mkdirs();
File outfile = new File(dPath);
outfile.delete();
if (!outfile.exists())
outfile.createNewFile();
fos = new FileOutputStream(outfile);
byte[] block = new byte[1000];
int i;
while ((i = fis.read(block)) != -1)
fos.write(block, 0, i);
fos.close();
if (isDeleteSourceDir)
deleteDirectory(new File(sPath));
return true;
catch (Exception e)
e.printStackTrace();
return false;
finally
fis.close();
fos.close();
add a comment |
Try using this :
private void copyDB(Context context) throws Exception
File destinationDirectory = new File(Environment.getExternalStorageDirectory() + "/Destination/");
File destinationFile = new File(destinationDirectory + "/myDatabase.db");
if (!destinationDirectory.isDirectory())
destinationDirectory.mkdir();
destinationFile.createNewFile();
File dbFile = context.getDatabasePath("my_database.db");
if (dbFile.exists())
copyFile(dbFile.getPath(), logFile.getAbsolutePath(), false);
private boolean copyFile(String sPath, String dPath, boolean isDeleteSourceDir)
FileInputStream fis = null;
FileOutputStream fos = null;
try
File sourceFile = new File(sPath);
fis = new FileInputStream(sourceFile);
File dir = new File(dPath);
if (!dir.exists())
dir.mkdirs();
File outfile = new File(dPath);
outfile.delete();
if (!outfile.exists())
outfile.createNewFile();
fos = new FileOutputStream(outfile);
byte[] block = new byte[1000];
int i;
while ((i = fis.read(block)) != -1)
fos.write(block, 0, i);
fos.close();
if (isDeleteSourceDir)
deleteDirectory(new File(sPath));
return true;
catch (Exception e)
e.printStackTrace();
return false;
finally
fis.close();
fos.close();
Try using this :
private void copyDB(Context context) throws Exception
File destinationDirectory = new File(Environment.getExternalStorageDirectory() + "/Destination/");
File destinationFile = new File(destinationDirectory + "/myDatabase.db");
if (!destinationDirectory.isDirectory())
destinationDirectory.mkdir();
destinationFile.createNewFile();
File dbFile = context.getDatabasePath("my_database.db");
if (dbFile.exists())
copyFile(dbFile.getPath(), logFile.getAbsolutePath(), false);
private boolean copyFile(String sPath, String dPath, boolean isDeleteSourceDir)
FileInputStream fis = null;
FileOutputStream fos = null;
try
File sourceFile = new File(sPath);
fis = new FileInputStream(sourceFile);
File dir = new File(dPath);
if (!dir.exists())
dir.mkdirs();
File outfile = new File(dPath);
outfile.delete();
if (!outfile.exists())
outfile.createNewFile();
fos = new FileOutputStream(outfile);
byte[] block = new byte[1000];
int i;
while ((i = fis.read(block)) != -1)
fos.write(block, 0, i);
fos.close();
if (isDeleteSourceDir)
deleteDirectory(new File(sPath));
return true;
catch (Exception e)
e.printStackTrace();
return false;
finally
fis.close();
fos.close();
answered Mar 22 at 6:26
ShreyShrey
516
516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55293020%2fcopy-database-file-to-local-storage%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown