【问题标题】:Get database path获取数据库路径
【发布时间】:2012-03-21 18:14:09
【问题描述】:

我是 android 新手,我想用 SQLite 创建一种 CRUD。

我有一个public class DataBaseHelper,它有一个构造函数:

public DataBaseHelper(Context context)
{
    this.context = context;
}

还有一个获取数据库的方法:

public SQLiteDatabase getDataBase() {
    if (dataBase == null) {
        try {
            dataBase = context.openOrCreateDatabase(DataBaseHelper.dbName,
                                                    Context.MODE_PRIVATE, null);

            dataBase.execSQL("CREATE TABLE " + DataBaseHelper.accountTblName + " " +
                                        "(ID integer PRIMARY KEY AUTOINCREMENT" +
                                        ",name TEXT" +
                                        ",address TEXT" +
                                        ",password TEXT)");
            }
            catch(Exception e) {
                Log.e(context.toString(), e.getMessage());

                if (dataBase != null) {
                    dataBase.close();
                }
            }
    }       
    return dataBase;
}

如果我是第一次启动一个程序 - 一切似乎都很好。数据库为空,我创建它(使用表)。但是当我重新启动时,我的应用程序数据库再次为空,并且无法创建表。

我决定编写一个 checkDB 函数,它试图打开一个数据库。但是我在哪里可以找到它的路径?我不希望它被“硬编码”。

或者我可能在做某种反模式?

【问题讨论】:

    标签: android database


    【解决方案1】:

    您可以通过context.getDatabasePath(DataBaseHelper.dbName)获取路径

    【讨论】:

    • 它是否给出了字符串路径??我知道它返回文件。如何获取字符串?
    • @Archie.bpgc context.getDatabasePath(DataBaseHelper.dbName).toString;
    • 这也是从 API 级别 1 开始。聪明。
    • 是toString();
    【解决方案2】:

    获取Sqlite数据库路径

    这里我写了两种获取路径的方法。

    (1) 使用自定义方法

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

    (2) 使用 File 类

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

    即使你会看到这两个代码......然后两个代码是相同的, 第一种是获取数据库路径的快捷方式,与第二种方法相比,它占用的内存更少

    【讨论】:

      【解决方案3】:

      您可以在助手的构造函数中使用 getDatabasePath 方法:

      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();
          }
      

      【讨论】:

        【解决方案4】:

        我还需要访问应用程序的数据库。这对我有用:

        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);
        }
        

        希望对您有所帮助! :-)

        【讨论】:

          猜你喜欢
          • 2011-02-12
          • 2013-02-22
          • 1970-01-01
          • 2012-04-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-23
          相关资源
          最近更新 更多