I am at my wits end with a problem I cannot resolve - when trying to change the schema/data of a table in my database. I am pre-populating the database with Rooms .createFromAsset method.
Current schema and data (working) - as shown in DB Browser - this is the database used to Pre-Populate the apps database.
As I build the database with this code:
val instance = Room.databaseBuilder(
context.applicationContext,
MetarDatabase::class.java,
"metar_database"
)
.createFromAsset("database/metar_database.db")
.fallbackToDestructiveMigration()
.build()
And this data class:
#Entity(tableName = "airport_table")
data class Airport(
#PrimaryKey(autoGenerate = false)
val icao : String,
val name : String?,
val municipality : String?,
val scheduled : Boolean,
val iata : String?
)
It successfully pre-populates the database into the app: as seen on Database Explorer:
The problem is this: If I try and change the schema at all: adding columns, removing columns (done by changing a csv file and importing into a new table in DB Browser, then re-naming to airport_table and adding one to the database version), it does not load the data.
Eg: (Does NOT pre-populate):
#Entity(tableName = "airport_table")
data class Airport(
#PrimaryKey(autoGenerate = false)
val icao : String,
val name : String?
)
I get NO errors apart from "E/libc: Access denied finding property "ro.serialno"" which I don't think is relevant.
In addition - changing this one table means that my other table does not pre-populate.
However the empty database has still been made successfully, and I can add to it within the app.
Please help me - I've looked around stack overflow for similar questions. Some are similar, but involve getting anything to happen at all - my problem is it only works with one exact schema that I happen to succeed with whilst testing - but I want another schema.
Thanks a lot,
Liam
If you intend to export a file from the Room DB or access information using a database browser then use .setJournalMode(JournalMode.TRUNCATE) first. To ensure all data is fully preserved. I guess this is one of the possible causes of this error. Hope can help you
val instance = Room.databaseBuilder(
context.applicationContext,
MetarDatabase::class.java,
"metar_database"
)
.createFromAsset("database/metar_database.db")
.fallbackToDestructiveMigration()
.setJournalMode(JournalMode.TRUNCATE) // Add this line
.build()
After a long time I realised the problem.
I thought I did not need migration as I only wanted to change the pre-populating database, and not migrate anything currently on the device. However the use of destructive migration also deletes your pre-populating data when you add one to the schema number.
So simply add an empty migration and it works
val migration1: Migration = object : Migration(5, 6) { override fun migrate(database: SupportSQLiteDatabase) {}}
Thanks very much to myself for sorting my problem.
Related
I have an application with prefilled database (I use Room, Kotlin). Database class:
#Database(
entities = [FurnitureModel::class, ImageModel::class],
version = Database.VERSION,
exportSchema = true
)
abstract class Database : RoomDatabase() {
abstract val furnitureDao: FurnitureDao
abstract val imageDao: ImageDao
companion object {
const val NAME = "application-data"
const val VERSION = 3
#Volatile
private var instance: Database? = null
fun getInstance(context: Context): Database {
synchronized(this) {
var inst = instance
if (inst == null) {
inst = Room.databaseBuilder(
context.applicationContext,
Database::class.java,
"$NAME-local.db"
)
.fallbackToDestructiveMigration()
.createFromAsset("$NAME.db")
.build()
instance = inst
}
return inst
}
}
}
}
It worked fine. Once I decided to update some data inside database. No stucture changes, just adding and changing some data. For versions, I changed constant VERSION from 3 to 4. When I run the application, I've got empty database. As I understand, it is possible to get empty database with fallbackToDestructiveMigration in few situations. I could made some mistake somewhere in data. To get more details I decided to run application without preinstalled previous version. Clean install, if I can say like this. To do it, I wiped data for Virtual device in AVD Manager, removed build directories in application and app directories and run 'Invalidate cache/restart...' in Android Studio (I know that there is no need to do it). But the result is always the same: trying to install my application I always get error
Caused by: java.lang.IllegalStateException: A migration from 3 to 4 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
As I understand, the error must not be shown when you install the application for the first time. But it is shown. I tried to create new virtual device and run app there, but the error is still there.
What should I do to forse Android Studio to forget that the application was installed before?
Thank you.
UPD1: I don't need to keep any data, actually I want to delete all data from previous versions of application. But after wiping data it still works as if I didn't wipe it. This is the problem.
RESULT: I found that the problem was in prefilled database. Unfortunatedly I can't find that is exactly wrong with database. I disabled creating database from assets, run application so that Room created bew empty database, copied the database from emulator and compared each table with my database. No any difference. So to solve the problem I just moved all data in each table to the same table in new empty database (using DataGrid). After running the application new database the problem disapeared.
I've recentyly started using Room and I would like to know if when inserting or updating an Entitiy there's any way to check if the value is null, and in that case, do not insert it /update it.
What I want to do is something like
#Entity
data class Person{
#PrimaryKey
val id: Int,
val name:String
val surname:String
}
Would it be possible in a simple way to perform an #Update operation for those fields which are not null? and those which are null keep them as tey are?
For example in an update perhaps I might have informed the id and the name, but in another update I might have informed the id and the surname. So what I want is to merge the information, but if possible without having to make a select query to check the values stored.
I've read the following post, but my doubt then it would be, is it possible to define an #Entity, with all the fields defined as the one I mentioned before and then have other entities to just update some fields, something like:
#Entity(tableName = "person")
data class PersonUpdateSurname{
#PrimaryKey
val id: Int,
val name:String
}
#Entity(tableName = "person")
data class PersonUpdateSurname{
#PrimaryKey
val id: Int,
val surname:String
}
Is there a way to tell Room which is the original table structure?
Is there a way to tell Room which is the original table structure?
This question is not clear. Maybe there is some misunderstanding you have.
Try to follow next schema:
There should be only one Person-related data class annotated with Room-annotations - #Entity, #PrimaryKey and so on. In your case it is Person class.
All the rest mentioned auxiliary classes should be just POJO (plain data classes), since they are not being persisted. In your case - PersonName and PersoneSurname (with the fields you described but without Room's annotations).
In DAO use entity-parameter in #Update:
#Update(entity = Person::class)
fun updateName(personName: PersonName)
#Update(entity = Person::class)
fun updateSurname(personeSurname: PersonSurname)
In your Repository call method what you need. If you want to update only name - you use method updateName() and instance of PersonName class as a parameter, for only surname's update - method updateSurname() and instance of class PersonSurname.
I made a simple example app with using Room and Flows:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val build = Room.databaseBuilder(this, FinanceDatabase::class.java, "database.db")
.fallbackToDestructiveMigration()
.build()
GlobalScope.launch {
build.currencyDao().addCurrency(CurrencyLocalEntity(1))
val toList = build.currencyDao().getAllCurrencies().toList()
Log.d("test", "list - $toList")
}
}
}
#Entity(tableName = "currency")
data class CurrencyLocalEntity(
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "currencyId")
var id: Int
) {
constructor() : this(-1)
}
#Dao
interface CurrencyDao {
#Query("SELECT * FROM currency")
fun getAllCurrencies(): Flow<CurrencyLocalEntity>
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun addCurrency(currency: CurrencyLocalEntity)
}
#Database(entities = [CurrencyLocalEntity::class], version = 1)
abstract class FinanceDatabase : RoomDatabase() {
abstract fun currencyDao(): CurrencyDao
}
I want to use toList() function as in code above but something gets wrong and even Log doesn't print. At the same time using collect() works fine and gives me all records.
Can anybody explain to me what is wrong? Thanks.
There are a couple things wrong here but I'll address the main issue.
Flows returned by room emit the result of the query everytime the database is modified. (This might be scoped to table changes instead of the whole database).
Since the database can change at any point in the future, the Flow will (more or less) never complete because a change can always happen.
Your calling toList() on the returned Flow will suspend forever, since the Flow never completes. This conceptually makes sense since Room cannot give you the list of every change that will happen, without waiting for it to happen.
With this, I'm sure you know why collect gives you the records and toList() doesn't.
What you probably want here is this.
#Query("SELECT * FROM currency")
fun getAllCurrencies(): Flow<List<CurrencyLocalEntity>>
With this you can get the first result of the query with Flow<...>.first().
Flow in Room is for observing Changes in table.
Whenever any changes are made to the table, independent of which row is changed, the query will be re-triggered and the Flow will emit again.
However, this behavior of the database also means that if we update an unrelated row, our Flow will emit again, with the same result. Because SQLite database triggers only allow notifications at table level and not at row level, Room can’t know what exactly has changed in the table data
Make sure that the same doa object you are using for retrieving the list, is used for updating the database.
other than that converting flow to livedata is done using asLivedata extension function
For me below solution works for updating the view with database table changes.
Solution: Same Dao Object should be used when we insert details into the room database and get information from DB.
If you are using a dagger hilt then
#Singleton annotation will work.
I hope this will solve your problem.
**getAllCurrencies()** function should be suspend.
Please check the syntax to collect List from Flow:
suspend fun <T> Flow<T>.toList(
destination: MutableList<T> = ArrayList()
): List<T> (source)
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/to-list.html
I am trying to have this new feature working but I keep getting an empty database. There is not much documentation and the articles on medium are not detailed enough.
Using the debugImplementation com.amitshekhar.android:debug-db:1.0.6 plugin to look into my database and manually add an entry to it.
After doing so I download the database.
The file itself is not a *.db file but I change the name and put the .db extension (I have tried both with and without).
I add it to my assets folder and on my databasebuilder I add a .createFromAsset with the path so that I can use the database.
When restarting the app (deleting and reinstalling) I find that is it not pre populated.
What exactly am I doing wrong?
This is how I create the database. There is nothing special with it.
single(createdAtStart = true) {
Room.databaseBuilder(
get(),
CountryDatabase::class.java,
"country_database"
).createFromAsset("database/country_database.db").build()
}
single(createdAtStart = true) {
get<CountryDatabase>().countryDao()
}
#Database(entities = [LocalCountryData::class], version = 4)
abstract class CountryDatabase : RoomDatabase() {
abstract fun countryDao(): CountryDao
}
#Entity(tableName = "countries")
data class LocalCountryData(
#PrimaryKey
val country_name: String,
val country_short_code: String,
val regions_name: String,
val regions_short_code: String
)
When restarting the app I find that is it not pre populated.
createFromAssets will only be invoked and copy the database from the database folder of the assets folder if there is no existing database.
Rather than restarting the App, you should either :-
delete the App's data or
uninstall the App
and then run the App.
I am using Room Database from the new Architecture components in my project.
I am adding some data through dao, but when trying to retrieve it I am not getting it. Can you please suggest me how to check whether the insert was successful or not? Below are the codes to help you understand the problem.
adding to database, I checked with debugger, this statement executed successfully.
appContext.db.rallyDAO().addVehicleListItem(vehicle)
Getting null from database on this statement after insert.
val v = appContext.db.rallyDAO().getVehicleListItem(it.vehicleID)
RoomDatabase
#Database(entities = arrayOf(Rally::class, Route::class, CheckPoints::class, Vehicles::class, VehicleListItem::class), version = 1)
abstract class TSDRoom: RoomDatabase() {
public abstract fun rallyDAO():RallyDAO
}
Inside DAO
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun addVehicleListItem(vehicleListItem:VehicleListItem)
#Query("select * from vehicles_submitted where vehicle_id LIKE :vehicleID")
fun getVehicleListItem(vehicleID:String):VehicleListItem
VehicleListItem Entity
#Entity(tableName = "vehicles_submitted",
foreignKeys = arrayOf(ForeignKey(entity = Rally::class,
parentColumns = arrayOf("rally_id"),
childColumns = arrayOf("rally_id"))))
class VehicleListItem {
#PrimaryKey
#ColumnInfo(name = "vehicle_id")
#SerializedName("vehicle_id")
var vehicleID : String = ""
#ColumnInfo(name = "driver_id")
#SerializedName("driver_id")
var driverID : String = ""
#ColumnInfo(name = "vehicle_name")
#SerializedName("vehicle_name")
var vehicleName : String = ""
#ColumnInfo(name = "driver_name")
#SerializedName("driver_name")
var driverName : String = ""
#ColumnInfo(name = "driver_email")
#SerializedName("driver_email")
var driverEmail : String = ""
#ColumnInfo(name = "rally_id")
#SerializedName("rally_id")
var rallyID: String = ""
#ColumnInfo(name = "is_passed")
#SerializedName("is_passed")
var isPassed = false
#ColumnInfo(name = "passing_time")
#SerializedName("passing_time")
var passingTime:String=""
}
The problem was in the thread.
Room doesn't allow you to run database queries in main thread. The call to insert method was inside of a try-catch block, and I was ignoring the exception.
I fixed it, and here's how it reads right now.
doAsync {
val addedID = appContext.db.rallyDAO().addVehicleListItem(vehicle)
Logger.d("vehicle_lsit_item","Inserted ID $addedID")
}
Also, I reviewed the documentation, the insert method may return a Long (or List of Long in case of List passed to insert). I also changed the signature of insert, and here is the modified code
#Insert(onConflict = OnConflictStrategy.REPLACE)
fun addVehicleListItem(vehicleListItem:VehicleListItem):Long
P.S. I am using anko for doAsync
You can use Stetho to see your DB and Shared pref file in chrome dev tool
http://facebook.github.io/stetho/
EDIT: AS has a built-in DB tool now
An option you may consider, if you have not access permission issues, is to navigate directly into your sqlite instance of the device and query the tables directly there.
If you use the emulator intehgrated with Android Studio or a rooted phone you can do the following:
adb root
adb remount
cd data/data/path/of/your/application/database
sqlite3 mydb.db
Then you can query your tables
There are two cases regarding insertion.
Dao methods annotated with #Insert returns:
In case the input is only one number it returns a Long, check whether this Long equals One, if true then the Item was added successfully.
#Insert
suspend fun insert( student: Student): Long
In case the input is varargs it returns a LongArray, check whether the size of varargs equals the LongArray size, if true then items were added successfully
#Insert
suspend fun insert(vararg students: Student): LongArray
There are multiple ways you can test that.
As #Ege Kuzubasioglu mentioned you can use stetho to check manually (Need minor change to code).
Pull database file from "data/data/yourpackage/databases/yourdatabase.db" to your local machine and use any applications to read the content inside the database. I personally use https://sqlitebrowser.org/.
Pulling database file can be done either using the shell commands or use "Device File Explorer" from android studio.
Write TestCase to see if it is working. Here is an example of test case from one of my projects.
// Code from my DAO class
#Insert(onConflict = OnConflictStrategy.REPLACE)
public abstract Long[] insertPurchaseHistory(List MusicOrders);
//My Test Case
#Test
public void insertPurchaseHistoryTest() {
// Read test data from "api-responses/music-purchase-history-response.json"
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream("api-responses/music-purchase-history-response.json");
// Write utility method to convert your stream to a string.
String testData = FileManager.readFileFromStream(inputStream);
// Convert test data to "musicOrdersResponse"
MusicOrdersResponse musicOrdersResponse = new Gson().fromJson(testData,MusicOrdersResponse.class);
// Insert inmateMusicOrders and get the list of
Long[] rowsInserted = tracksDao.insertPurchaseHistory(musicOrdersResponse.getmusicOrders());
assertThat(rowsInserted.length,Matchers.greaterThan(0));
}
You can use Android Debug Database to access your application's databases via a web browser. Android Debug Database exposes the databases via an embedded web server.
Use it as follows:
Include it as debugImplementation dependency in your app's build.gradle so that it will only be included in debug build and not in release build:
debugImplementation 'com.amitshekhar.android:debug-db:1.0.3'
Start the debug build of your app
The embedded web server launches automatically and announces its address and port in the logs:
D/DebugDB: Open http://XXX.XXX.X.XXX:8080 in your browser
If you are running the app over USB, setup portforwarding:
adb forward tcp:8080 tcp:8080
If you are not running the app over USB, your Android phone and workstation need to be in the same network and your workstation should be able to ping the Android phone.
Finally, open the link from the logs in the browser.
Note that it works just fine without rooting the device as the web server runs inside your app context.
One of the easiest way is:
First download DB Navigator as plugin in Android Studio(For more visit: https://plugins.jetbrains.com/plugin/1800-database-navigator).
Go to view> Tools Window> Device File explorer and download the instance of your DB.
Open DB Browser(From left Pane window) and browse your DB instance you have downloaded. Setup the connection and you are good to browser you database.
For more info please refer to the link provided above.
I make like this(I use coroutines suspend):
class SaveVehicle(val repository: RepositoryInterface) {
suspend fun invoke(vehicleListItem: VehicleListItem): VehicleListItem =
with(vehicleListItem) {
also {
repository.saveVehicle(vehicleListItem)
}
}
}
Kotlin Doc
inline fun <T> T.also(block: (T) -> Unit): T
Calls the specified function block with this value as its argument and returns this value.
also is good for performing some actions that take the context object as an argument. Use also for actions that need a reference rather to the object than to its properties and functions, or when you don't want to shadow this reference from an outer scope.
You can check my example in GitHub: