-1

I had a db named test.db located in application directory . I retrieved the path using the method "getPath()" which gives entire path of db . How can I get the name of the db from that path. I ma receiving path a data\test.db . I need to get only test.db . How can I retrieve the name of the databae only from the path in android.

Thanks in Advance

3 Answers3

1

used split() for string splitting like

String str=yourpathstring;
String[] str1=str.split("/");
String dbname=str1[1];
M D
  • 47,665
  • 9
  • 93
  • 114
0

Try this

    String dbpath="yourdbpath";
    File f = new File(dbpath);
    String dbName = f.getName();
Shini
  • 573
  • 4
  • 11
0

as explained in another threads you can do so with:

 context.getDatabasePath(DataBaseHelper.dbName)

in your case the name is test.db if I understood well; this is the proper way to do it and I think you'll get the path you expect.

source: Get database path

Either way; if you just want to extract the name from the String data\test.db then you can:

String path = yourpath;
String dbName = path.substring(path.length - 8, 7)

Using Substring this way will always extract the last 7 characters.

source: Extract substring from a string

Community
  • 1
  • 1
Miquel Coll
  • 759
  • 15
  • 49