I have created a "sqlite" database using a Java application, and I inserted the data in a table that database. I checked it also with sqliteStudio. I copied the database in the asset folder of my Android application to use it. But when I try to select data from the table it gives me an exception "no such table OSOLDB". I followed this answer to open existing database and here is my code:
public class DBHelper extends SQLiteOpenHelper{
private static final String DBName="OSOL";
private static final String TABLE_NAME="OSOLDB";
private static final String PARAGRAPH_ID_COL="ID";
private static final String PARAGRAPH_COL="PAR";
private static File DATABASE_FILE;
private static boolean InvalidDB=false;
private static final int VERSION = 1;
private static DBHelper myHelper;
private Context context;
private static String DB_PATH="";
private SQLiteDatabase DB;
public DBHelper(Context context) {
super(context, DBName, null, VERSION);
// TODO Auto-generated constructor stub
Log.d("DBHELPER","constructor");
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.context = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e("DBHELPER", "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DBName;
DB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return DB != null;
}
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DBName);
Log.v("learn", dbFile.getName() + " "+ dbFile.exists());
return dbFile.exists();
}
/*
synchronized static DBHelper getInstance( Context context)
{
if(myHelper==null)
{
myHelper= new DBHelper(context.getApplicationContext());
}
return myHelper;
}*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
InvalidDB=true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
InvalidDB=true;
//isUpgraded=true;
}
@Override
public synchronized void onOpen(SQLiteDatabase db)
{
super.onOpen(db);
if(!db.isReadOnly())
{
DATABASE_FILE = context.getDatabasePath(DBName);
}
}
@Override
public synchronized void close()
{
super.close();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = context.getAssets().open(DBName);
String outFileName = DB_PATH + DBName;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
}
and this:
public class DataAdapter {
private final Context context;
private SQLiteDatabase DB;
private DBHelper helper;
public DataAdapter(Context contex)
{
this.context=contex;
helper= new DBHelper(contex);
}
public DataAdapter createDatabase() throws SQLiteException, IOException
{
helper.createDataBase();
return this;
}
public DataAdapter open() throws SQLException
{
try
{
helper.openDataBase();
helper.close();
DB = helper.getReadableDatabase();
Log.d("adapter","shiiiiiit");
}
catch (SQLException mSQLException)
{
Log.e("dataAdapter", "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
helper.close();
}
public Cursor getTestData()
{
try
{
String sql ="SELECT * FROM OSOLDB";
Cursor mCur = DB.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e("dataAdapter", "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
}
why I keep getting the exception even though the table exists?