Answer the question
In order to leave comments, you need to log in
What distributed databases exist for Android applications?
In android studio, I created a service and an application - these are two different projects (applications) that run on the same device. Both use a SQLite database, but if I understand correctly, each project has its own database, and I need the data to be shared. In this regard, two questions:
1. Did I understand correctly that the database created in the application (in sqlite) is local and other applications do not have access to its data?
2. Advise which database suits my case?
Answer the question
In order to leave comments, you need to log in
Yes, I understood correctly.
The correct way to fumble is Content Provider. You can screw StorIO to it to humanize the interface.
a local database can be made using the Room library (Sqlite is more convenient) and Dagger. Below is a simple working
href=" https://yadi.sk/d/9Y7JeDhSHNODTQ?w=1 "
sample application Room - local database, Dagger - connect any class
package com.example.myapplicationroom.data.db.dbInstance
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.myapplicationroom.data.db.dao.EmployeeDao
import com.example.myapplicationroom.data.db.entity.Employee
@Database(
entities = [
Employee::class],
version = 1
)
abstract class LocalDatabase: RoomDatabase() {
abstract fun getEmployeeDao(): EmployeeDao
companion object {
@Volatile
private var INSTANCE: LocalDatabase? = null
fun getDatabase(context: Context): LocalDatabase {
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
LocalDatabase::class.java,
"NoteDatabase"
).fallbackToDestructiveMigration().build()
// ^^^ Без миграции ^^^
INSTANCE = instance
return instance
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question