Database not copying from assets
I am giving you the complete code,plz reply if you success
public class DataBaseHelper extends SQLiteOpenHelper{
private Context mycontext;
private String DB_PATH = "/data/data/gr.peos/databases/";
//private String DB_PATH = mycontext.getApplicationContext().getPackageName()+"/databases/";
private static String DB_NAME = "BLib.sqlite";//the extension may be .sqlite or .db
public SQLiteDatabase myDataBase;
/*private String DB_PATH = "/data/data/"
+ mycontext.getApplicationContext().getPackageName()
+ "/databases/";*/
public DataBaseHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println("Database exists");
opendatabase();
}
else
{
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException{
boolean dbexist = checkdatabase();
if(dbexist)
{
//System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copydatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
//checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE);
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
//Open your local db as the input stream
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("/data/data/gr.peos/databases/BLib.sqlite");
// transfer byte to inputfile to 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();
}
public void opendatabase() throws SQLException
{
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
Sqlite database not copied from asset folder Android
This code wlll help you to copy DB from assets folder.
You can check first if DB exists or not.
try{
// CHECK IS EXISTS OR NOT
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/"+getPackageName+"/databases/dbname.sqlite",null, 0);
dbe.close();
}
catch(Exception e)}
{
// COPY IF NOT EXISTS
AssetManager am = getApplicationContext().getAssets();
OutputStream os = new FileOutputStream("/data/data/"+getPackageName+"/databases/dbname.sqlite");
byte[] b = new byte[100];
int r;
InputStream is = am.open("dbname.sqlite");
while ((r = is.read(b)) != -1) {
os.write(b, 0, r);
}
is.close();
os.close();
}
Database file is not copying from assets to data/data/ package name /databases
Seriously!! I don't believe this. I just added db.openDataBase(); function on the first query of my app, then it worked. I cannot believe this. What I realize now is that I should open database before I query. It might help other in this kind of problem. CHEERSS!! :D
And do not forget to db.close in pause() and on resume() db.open();
Copying SQLite database from assets folder issue
The solution to my problem is this:
Since Android Studio uses the new Gradle-based build system, you should be putting assets/ inside of the source sets example myproject/src/main/assets/.
I created my own assets folder and put it where Eclipse usually puts it... which caused all these problems for me.
Database not being copied from assets folder to device
Got the answer... :)
From here..
http://www.anddev.org/networking-database-problems-f29/missing-table-in-sqlite-with-specific-version-of-desire-hd-t50364.html
it was the problem with version 2.3.6... it was working with other devices... just added three lines to solve the problem...
boolean dbExist = checkDataBase();
SQLiteDatabase db_Read = null;
if (!dbExist)
{
db_Read = this.getReadableDatabase();
db_Read.close();
try
{
copyDataBase();
}
catch (IOException e)
{
Log.d("Error", e.toString());
}
}
Cannot copy database from Asset folder in Android
You should make sure that the database path exists.
I assume you are trying to copy the database into a non-existing directory, because the error message is "No such file or directory" on the line where you create the FileOutputStream in copyDatabase().
android - Cannot copy database from assets folder : API 24
The issue is likely that the databases directoryr does not exist (it will not for a newly installed App).
Thus you need to create the directory if it doesn't exist. e.g. :-
private fun installDatabaseFromAssets() {
val inputStream = context.assets.open("$ASSETS_PATH/$DATABASE_NAME.db")
val dbFile = File(context.getDatabasePath(DATABASE_NAME).toString())
if (!dbFile.exists()) {
if (!dbFile.parentFile.exists()) {
dbFile.parentFile.mkdirs()
}
}
try {
//val outputFile = File(context.getDatabasePath(DATABASE_NAME).path)
val outputStream = FileOutputStream(dbFile)
inputStream.copyTo(outputStream)
inputStream.close()
outputStream.flush()
outputStream.close()
} catch (exception: Throwable) {
throw RuntimeException("The $DATABASE_NAME database couldn't be installed.", exception)
}
}
Related Topics
How to Write Ndef Records to Nfc Tag
Gson - Convert from JSON to a Typed Arraylist<T>
How to Use Interface to Communicate Between Two Activities
How to Downsample Images Within PDF File
Nullpointerexception in Java with No Stacktrace
Setmaxresults for Spring-Data-JPA Annotation
Java Datetimeformatterbuilder Fails on Testtime
How to Load Rsa Private Key from File
Java 8 Nested (Multi Level) Group By
How to Create a Zip File in Java
Android Changing Floating Action Button Color
How to Scroll a Scrollview Programmatically in Android
Can the Android Sdk Work with Jdk 1.7
Linux Command for Extracting War File
Differences Between Exception and Error
Java: (String[])List.Toarray() Gives Classcastexception
Calling One Jframe from Another Using Timer Without Any Buttons