-1

I want to get my package name when it extends SQLiteOpenHelper. In this class I myself change my package name and it is hard code. I just want that automaticly it gets package name. how I can fix it? It extends from SQLiteOpenHelper not Acctivity that's my problem.

enter code here
     public class database extends SQLiteOpenHelper {


public final String path="data/data/com.fatemehkh.hamkon/databases/";

public final String Name="database";
public SQLiteDatabase mydb;

private final Context mycontext;


public database(Context context) {
    super(context, "database", null, 1);
    mycontext=context;

}


@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}


public void useable(){

    boolean checkdb=checkdb();

    if(checkdb){

    }else{

        this.getReadableDatabase();

        try{
        copydatabase();
        }catch(IOException e){
        }

    }



}

public void open(){

    mydb=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READWRITE);

}

public void close(){
    mydb.close();
}

public boolean checkdb(){

    SQLiteDatabase db=null;
    try{    
    db=SQLiteDatabase.openDatabase(path+Name, null, SQLiteDatabase.OPEN_READONLY);
    }
    catch(SQLException e)
    {

    }
    //mydb.close();
    return db !=null ? true:false ;

}

public void copydatabase() throws IOException{
    OutputStream myOutput = new FileOutputStream(path+Name);
    byte[] buffer = new byte[1024];
    int length;
    InputStream myInput = mycontext.getAssets().open(Name);
    while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
    }
    myInput.close();
    myOutput.flush();
    myOutput.close();
}

how I change this line.

public final String path="data/data/com.fatemehkh.hamkon/databases/";
mhs
  • 41
  • 7

1 Answers1

1

Try this:

public static final String DATABASE_NAME = "your_data.db";
public database(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

    String path=context.getDatabasePath(database.DATABASE_NAME).getAbsolutePath();
}
Mithun
  • 2,075
  • 3
  • 19
  • 26
  • Then when and where i use path? – mhs May 23 '15 at 18:52
  • That's really a broader context. I suggest you to post a new question instead. If this answer helped your question in this post please accept it. – Mithun May 23 '15 at 18:56
  • I dont know how to use it. – mhs May 23 '15 at 18:58
  • you could get help from http://stackoverflow.com/questions/3436434/is-it-possible-to-move-the-internal-db-to-the-sdcard/3436869#3436869 and http://stackoverflow.com/questions/3474538/how-to-access-path-of-database-or-file-of-different-app-in-android on how to use it... possible use case would include `to move the internal DB to the SDCard` – Mithun May 23 '15 at 19:59
  • Thanks after a lot of tries I solve it by your help. – mhs May 23 '15 at 20:31