Using free DBBrowser(SQLite) software I can see the data inside the data.sqlite file but when I copy it to Android Studio Assets folder no matter what code I use I get one of two errors: No such table Table1_Sheet1 or android.database.sqlite.SQLiteDatabaseCorruptException: file is encrypted or is not a database (code 26): , while compiling: SELECT Column2, Column3, Column4,Column5,Column6, Column7 FROM Table1_Sheet1 WHERE Column1=?
One of the codes I try:
public class MyCustomDBHelper extends SQLiteOpenHelper {
private Context mycontext;
@Override public void onCreate(SQLiteDatabase db){}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private String DB_PATH="";
private static String DB_NAME = "data.sqlite";
public SQLiteDatabase myDataBase;
public MyCustomDBHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
DB_PATH=mycontext.getApplicationInfo().dataDir+"/databases/";
boolean dbexist = checkdatabase();
if (dbexist) {
myDataBase=opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase(){
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
private boolean checkdatabase() {
boolean checkdb = false;
try {
String myPath = DB_PATH+ DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
InputStream myinput = mycontext.getAssets().open(DB_NAME);
FileOutputStream(mycontext.getApplicationInfo().dataDir+"/databases/data.sqlite");
//OutputStream myoutput = new FileOutputStream(mycontext.getDatabasePath("data.sqlite").getAbsolutePath());
OutputStream myoutput = new FileOutputStream(DB_PATH + DB_NAME);
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 SQLiteDatabase opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READONLY);
return myDataBase;
}
public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}
}
I get the error when I try to read data from the copied file.I also observed the copy loop doesn't take long time only few times around 7 times. I also observed in the mobile I used for testing inside the data folder nothing is actually created for the project. Last thing I observed the data.sqlite file inside Assets folder has question mark on its file icon does this means it is not identified by android studio ?