【问题标题】:How to change SQLite database path to different folder (in application directory) using SQliteOpenhelper :android project如何使用 SQliteOpenhelper 将 SQLite 数据库路径更改为不同的文件夹(在应用程序目录中):android 项目
【发布时间】:2020-09-28 17:11:55
【问题描述】:

我目前正在开发 Android 项目,它有一个 SQLite 数据库。我之前使用颤振创建了数据库,并使用下面的代码来获取创建数据库的路径。

// Get the directory path for both Android and iOS to store database.
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + '/ideaDatabase.db';

它指向路径

/data/data/com.example.myprojet01/app_flutter/ideaDatabase.db

根据我的要求,我必须为项目添加 android 特定代码(Java)。我必须从 java 和 dart 部分在数据库上方添加数据/访问。任何人都可以帮助我如何从 Java 访问数据库。 我正在使用 SQliteOpenhelper 类来访问数据库。

SQLiteopenhelper 默认在创建数据库

/data/data/com.example.myprojet01/databases/ideaDatabase.db

是否可以指定 SQliteopenhelper 从 /app_flutter/ 目录而不是 /databases/ 打开数据库。?

代码 java 数据库助手

public class Database_Helper_widget extends SQLiteOpenHelper {

    public static final String TAG="Database_Helper_widget";

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "ideaDatabase.db";

    public Database_Helper_widget(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CreateTable=MycreateString;
        db.execSQL(CreateTable);
    }

代码颤动:

 Future<Database> initializeDatabase() async {
    // Get the directory path for both Android and iOS to store database.
    Directory directory = await getApplicationDocumentsDirectory();
    String path = directory.path + '/ideaDatabase.db';
    debugPrint('path to database $path');
    // Open/create the database at a given path
    var ideaDatabase =
        await openDatabase(path, version: 1, onCreate: _createDb);
    return ideaDatabase;
  }

【问题讨论】:

    标签: java android flutter android-sqlite


    【解决方案1】:

    注意:回答我自己的问题,这不是一个完美的解决方案,而是我暂时使用的解决方法。如果您有更好的解决方案,请改进此答案或添加一个新答案。

    而不是使用Application目录文件夹(Flutter)来存储数据库。我正在使用 "getDatabasesPath()" 。这样,Android 特定代码和 Flutter 代码都指向 Android 中的相同数据库文件夹(不确定 IOS)

    这指向您的数据库位置:

    /data/data/com.example.myprojet01/databases/ideaDatabase.db
    

    这与 java 中的 SQliteOpenHelper 类创建的默认数据库位置相同。

      Future<Database> initializeDatabase() async {
        // Get the directory path for both Android and iOS to store database.
        // Directory directory = await getApplicationDocumentsDirectory();
        String databasePath = await getDatabasesPath();
        String path = databasePath + '/ideaDatabase.db';
        // Open/create the database at a given path
        var ideaDatabase =
            await openDatabase(path, version: 1, onCreate: _createDb);
        return ideaDatabase;
      }
    

    如果您想从 Android 特定代码和 Flutter 代码访问公共数据库,则此方法适用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2021-10-09
      • 2018-11-04
      • 2019-11-19
      • 2018-05-31
      • 2015-11-08
      相关资源
      最近更新 更多