Get database path

后端 未结 4 836
我在风中等你
我在风中等你 2020-12-10 00:57

Im new to android and I want to create kind of CRUD with SQLite.

I have a public class DataBaseHelper which have a constructor:

public D         


        
相关标签:
4条回答
  • 2020-12-10 01:17

    You can get path by context.getDatabasePath(DataBaseHelper.dbName)

    0 讨论(0)
  • 2020-12-10 01:27

    I needed to access dbs for the app also. This worked for me:

    String dbFile;
    String[] dbNames = getSherlockActivity().databaseList(); // or ContextWrapper
    for (int i = 0; i < dbNames.length; i++) {
        dbFile = getSherlockActivity().getDatabasePath(dbNames[i]).toString();
        Log.i(LOG_TAG, dbFile);
    }
    

    Hope it helps! :-)

    0 讨论(0)
  • 2020-12-10 01:34

    To get the path of Sqlite data base

    here I am writing two ways to get the path.

    (1) using custom method

    public String getDbPath(Context context,String YourDbName)
    {
        return context.getDatabasePath(YourDbName).getAbsolutePath();
    }
    

    (2) using File class

      File path=context.getDatabasePath("YourDbName");
      String db_path=path.getAbsolutePath();
      Log.i("Path:",db_path);
    

    even you will see the both code ...then both code is same, 1st one is the shortcut method to get the database path, and it's occupy the less memory compare to second option.

    0 讨论(0)
  • 2020-12-10 01:39

    You can use getDatabasePath method inside your helper's constructor:

    public class MyDatabase extends SQLiteAssetHelper {
    
        private static final String DATABASE_NAME = "wl.db";
        private static final int DATABASE_VERSION = 1;  
        public String databasePath = "";    
    
        public MyDatabase(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
    
            databasePath = context.getDatabasePath("wl.db").getPath();
        }
    
    0 讨论(0)
提交回复
热议问题