bscharan
bscharan

Reputation: 157

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

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

Upvotes: 2

Views: 3409

Answers (1)

bscharan
bscharan

Reputation: 157

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.

Upvotes: 1

Related Questions