How to change SQLite database path to different folder (in application directory) using SQliteOpenhelper :android project

huangapple go评论98阅读模式
英文:

How to change SQLite database path to different folder (in application directory) using SQliteOpenhelper :android project

问题

我目前正在进行Android项目的开发,其中涉及一个SQLite数据库。之前我使用Flutter创建了数据库,并使用以下代码获取用于创建数据库的路径。

  1. // 获取适用于Android和iOS的目录路径,用于存储数据库。
  2. Directory directory = await getApplicationDocumentsDirectory();
  3. String path = directory.path + '/ideaDatabase.db';

它指向路径:

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

根据我的需求,我不得不为项目添加Android特定的Java代码,并且我需要从Java和Dart部分都访问上述数据库。有谁可以帮助我如何从Java中访问数据库。
我正在使用SQliteOpenHelper类来访问数据库。

SQLiteOpenHelper默认在以下位置创建数据库:

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

是否有可能指定SQLiteOpenHelper从/app_flutter/目录而不是/databases/目录打开数据库?

Java数据库助手代码:

  1. public class Database_Helper_widget extends SQLiteOpenHelper {
  2. public static final String TAG = "Database_Helper_widget";
  3. public static final int DATABASE_VERSION = 1;
  4. public static final String DATABASE_NAME = "ideaDatabase.db";
  5. public Database_Helper_widget(Context context) {
  6. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  7. }
  8. @Override
  9. public void onCreate(SQLiteDatabase db) {
  10. String CreateTable = MycreateString;
  11. db.execSQL(CreateTable);
  12. }
  13. }

Flutter代码:

  1. Future<Database> initializeDatabase() async {
  2. // 获取适用于Android和iOS的目录路径,用于存储数据库。
  3. Directory directory = await getApplicationDocumentsDirectory();
  4. String path = directory.path + '/ideaDatabase.db';
  5. debugPrint('数据库路径:$path');
  6. // 打开/创建位于给定路径的数据库
  7. var ideaDatabase =
  8. await openDatabase(path, version: 1, onCreate: _createDb);
  9. return ideaDatabase;
  10. }
英文:

I am currently working on Android project, it has a SQLite database. i have created the database using flutter previously and used below code to get path for creating database.

  1. // Get the directory path for both Android and iOS to store database.
  2. Directory directory = await getApplicationDocumentsDirectory();
  3. String path = directory.path + &#39;/ideaDatabase.db&#39;;

it points to path

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

as per my requirement i had to add android specific code(Java) for the project. and i had to add data/access above database from both java and dart part. can anyone help me how to get access the database from Java.
i am using SQliteOpenhelper class for accessing database.

SQLiteopenhelper is creating database by default in

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

Is to possible to make specify SQliteopenhelper to open database from /app_flutter/ directory instead of /databases/.?

Code java database helper

  1. public class Database_Helper_widget extends SQLiteOpenHelper {
  2. public static final String TAG=&quot;Database_Helper_widget&quot;;
  3. public static final int DATABASE_VERSION = 1;
  4. public static final String DATABASE_NAME = &quot;ideaDatabase.db&quot;;
  5. public Database_Helper_widget(Context context) {
  6. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  7. }
  8. @Override
  9. public void onCreate(SQLiteDatabase db) {
  10. String CreateTable=MycreateString;
  11. db.execSQL(CreateTable);
  12. }

Code Flutter:

  1. Future&lt;Database&gt; initializeDatabase() async {
  2. // Get the directory path for both Android and iOS to store database.
  3. Directory directory = await getApplicationDocumentsDirectory();
  4. String path = directory.path + &#39;/ideaDatabase.db&#39;;
  5. debugPrint(&#39;path to database $path&#39;);
  6. // Open/create the database at a given path
  7. var ideaDatabase =
  8. await openDatabase(path, version: 1, onCreate: _createDb);
  9. return ideaDatabase;
  10. }

答案1

得分: 1

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

与其使用应用程序目录文件夹(Flutter)来存储数据库,我正在使用"getDatabasesPath()"。这样一来,Android特定的代码和Flutter代码都指向Android中的同一个数据库文件夹(关于iOS我不确定)。

这将指向您的数据库位置:

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

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

  1. Future<Database> initializeDatabase() async {
  2. // 获取用于在Android和iOS上存储数据库的目录路径。
  3. // Directory directory = await getApplicationDocumentsDirectory();
  4. String databasePath = await getDatabasesPath();
  5. String path = databasePath + '/ideaDatabase.db';
  6. // 在给定路径上打开/创建数据库
  7. var ideaDatabase =
  8. await openDatabase(path, version: 1, onCreate: _createDb);
  9. return ideaDatabase;
  10. }

这种方法适用于希望从Android特定代码和Flutter代码中访问共同数据库的情况。

英文:

Note: answering my own Question, Not a perfect solution but a Workaround i am using temporarily. If you got a better solution please improve this answer or add a new one.

Rather than using Application directory folder (Flutter) to store databases. I am using "getDatabasesPath()" . This way both Android Specific code and Flutter code points to same database folder in Android (Not sure about IOS)

This points to your databases location:

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

which is same as default databases location in created by SQliteOpenHelper class in java.

  1. Future&lt;Database&gt; initializeDatabase() async {
  2. // Get the directory path for both Android and iOS to store database.
  3. // Directory directory = await getApplicationDocumentsDirectory();
  4. String databasePath = await getDatabasesPath();
  5. String path = databasePath + &#39;/ideaDatabase.db&#39;;
  6. // Open/create the database at a given path
  7. var ideaDatabase =
  8. await openDatabase(path, version: 1, onCreate: _createDb);
  9. return ideaDatabase;
  10. }

This approach works if you want to access common database from both Android Specific code and Flutter code.

huangapple
  • 本文由 发表于 2020年9月29日 01:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64106658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定