Reputation: 1251
I work on a project with two applications. The first enable to record information on database and the second use this information. I have no problem for the first but when I try access to the database with the second, I have the error :
08-05 14:22:29.545: E/AndroidRuntime(4751): FATAL EXCEPTION: main 08-05 14:22:29.545: E/AndroidRuntime(4751): android.database.sqlite.SQLiteException: no such table: matable (code 1): , while compiling: SELECT * FROM matable 08-05 14:22:29.545: E/AndroidRuntime(4751): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 08-05 14:22:29.545: E/AndroidRuntime(4751): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 08-05 14:22:29.545: E/AndroidRuntime(4751): ...
I have set on the two manifest the variable android:sharedUserId="mySharedUserId"
I have link my first app to my second app to reuse the class of the first app (DataBaseHelper, DAO, ...). It is the same code which is execute to acces (write or read) to the database.
My DataBaseHelper.java
package com.mypackage.database;
import java.io.FileOutputStream;
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String TAG = "DataBaseHelper";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static int DATABASE_VERSION = 1;
private static String DATABASE_NAME = "db_devicesManager";
private String DATABASE_PATH = "/data/data/com.mypackage.myapp/databases/";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(TAG, "path :"+DATABASE_PATH);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
copyDataBase();
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try{
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
InputStream inputStream = myContext.getAssets().open(DATABASE_NAME);
String myPath = DATABASE_PATH + DATABASE_NAME;
OutputStream outputStream = new FileOutputStream(myPath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
public boolean openDataBase() throws SQLException {
String myPath = DATABASE_PATH + DATABASE_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return myDataBase!=null;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int oldVersion, int newVersion) {
}
}
I have tried to uninstall apps, change version of database unsuccessfully.
Thank you for your help
Upvotes: 0
Views: 95
Reputation: 38595
First: Do not use hardcoded paths. Use Context.getDatabasePath(String)
instead.
Second: If the data you want is inside another application, then that application needs to expose an interface for you to communicate across process boundaries. Typically this means the serving app (the one with the data) implements either a ContentProvider
or a Bound Service
, the latter might possibly require the use of AIDL
Upvotes: 2