I have a pre-filled database in assets folder, which I move to internal storage on first run. It works fine on some devices, but on some I get the following error: sqlite returned: error code = 1, msg = no such table: FTSgesla, db=/data/data/com.example.enigmar/databases/gesla
The mentioned table definately exist, so I don’t know where the problem is.
SQLiteOpenHelper code:
public static class DictionaryOpenHelper extends SQLiteOpenHelper
{
private final Context mHelperContext;
private SQLiteDatabase mDatabase;
//private static String DB_PATH="/data/data/com.example.enigmar/databases/";
private static String DB_PATH="";
private static final String DB_NAME = "gesla";
DictionaryOpenHelper(Context context)
{
super(context, DB_NAME, null, 1);
//
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
//
this.mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
public void createDataBase() throws IOException
{
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
new Thread(new Runnable()
{
public void run()
{
try
{
copyDataBase();
Log.e(TAG, "createDatabase database created");
BAZA_NAREJENA=true;
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
}).start();
}
}
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException
{
InputStream mInput = mHelperContext.getAssets().open(DB_NAME);
//final Resources resources = mHelperContext.getResources();
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
//InputStream mInput = resources.openRawResource(R.raw.gesla);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
mDatabase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return mDatabase != null;
}
@Override
public synchronized void close() {
if(mDatabase != null)
mDatabase.close();
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
What is wrong?
In your
public void createDataBase(), you are using a Thread to copy your database in. Are you sure you have finished the copy before you try to access it? This is my working copy of the code which is very similar to yours you may want to see. Another thing istry 4096 , I had problems with 1024 before.