I have searched allot for my problems but none of the solutions work!!
I have a class like this:
public class database extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.example.MEN/databases/";
private static String DB_NAME = "men";
private SQLiteDatabase myDataBase;
private final Context myContext;
public database(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if(dbExist){
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public String openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor c=myDataBase.rawQuery("select Mistake from info",null);
String s=c.getString(0);
return s;
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
and i use it in another class by code like this:
database db;
db = new database(this);
try {
db.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
String t;
t=db.openDataBase();
TextView tv= (TextView) findViewById(R.id.textView);
tv.setText(t);
}catch(SQLException sqle){
try {
throw sqle;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
but i get that error every time i run it!!!!
anybody can help me??