英文:
How to change SQLite database path to different folder (in application directory) using SQliteOpenhelper :android project
问题
我目前正在进行Android项目的开发,其中涉及一个SQLite数据库。之前我使用Flutter创建了数据库,并使用以下代码获取用于创建数据库的路径。
// 获取适用于Android和iOS的目录路径,用于存储数据库。
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);
}
}
Flutter代码:
Future<Database> initializeDatabase() async {
// 获取适用于Android和iOS的目录路径,用于存储数据库。
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + '/ideaDatabase.db';
debugPrint('数据库路径:$path');
// 打开/创建位于给定路径的数据库
var ideaDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return ideaDatabase;
}
英文:
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.
// Get the directory path for both Android and iOS to store database.
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + '/ideaDatabase.db';
it points to path
/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
/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
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);
}
Code Flutter:
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;
}
答案1
得分: 1
注意:回答我自己的问题,不是一个完美的解决方案,而是我暂时使用的一个变通方法。如果您有更好的解决方案,请改进这个答案或添加一个新的答案。
与其使用应用程序目录文件夹(Flutter)来存储数据库,我正在使用"getDatabasesPath()"。这样一来,Android特定的代码和Flutter代码都指向Android中的同一个数据库文件夹(关于iOS我不确定)。
这将指向您的数据库位置:
/data/data/com.example.myprojet01/databases/ideaDatabase.db
这与Java中的SQliteOpenHelper类创建的默认数据库位置相同。
Future<Database> initializeDatabase() async {
// 获取用于在Android和iOS上存储数据库的目录路径。
// Directory directory = await getApplicationDocumentsDirectory();
String databasePath = await getDatabasesPath();
String path = databasePath + '/ideaDatabase.db';
// 在给定路径上打开/创建数据库
var ideaDatabase =
await openDatabase(path, version: 1, onCreate: _createDb);
return ideaDatabase;
}
这种方法适用于希望从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:
/data/data/com.example.myprojet01/databases/ideaDatabase.db
which is same as default databases location in created by SQliteOpenHelper class in java.
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;
}
This approach works if you want to access common database from both Android Specific code and Flutter code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论