Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet192/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   188   189   190   191   192   193   194   195   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

question_new
.
2. Adds an index to the 
question_id
 column of your 
question_new
 table.
3. Retrieves all the data from your original 
question
 table using a 
SELECT
 
statement and adds it to your 
question_new
 table using an 
INSERT INTO
 
statement.
4. Drops the original question table using a 
DROP TABLE
 statement.
5. Renames your 
question_new
 table to 
question
 using an 
ALTER TABLE
 with a 
RENAME TO
 statement.
Open the 
QuizDatabase
 class and modify it like this:
@Database(entities = [(Question::class)
, (Answer::
class
)], 
version = 4)//Changes the db version
 
abstract
 
class
 
QuizDatabase
 : 
RoomDatabase
() { 
  
companion
 
object

    
val
 MIGRATION_1_TO_2 = Migration1To2() 
    
val
 MIGRATION_2_TO_3 = Migration2To3() 
    
val
 MIGRATION_3_TO_4 = Migration3To4() 
//Adds a reference to 
your new migration
 
  } 
  
abstract
 
fun
 
questionsDao
()
: QuestionDao 
}
Just like before, you've changed the database version to 4 and created a reference to 
your new migration inside the companion object.
Saving Data on Android
Chapter 10: Migrations with Room
raywenderlich.com
183


Finally, add your new migration to your database by opening the 
QuizApplication.kt
 file and changing your database builder inside 
onCreate()
:
database = Room.databaseBuilder(
this
, QuizDatabase::
class
.
java

"question_database")
 
    .addMigrations(QuizDatabase.MIGRATION_1_TO_2, 
QuizDatabase.MIGRATION_2_TO_3, QuizDatabase.MIGRATION_3_TO_4) 
    .build()
Build and run your app, then press 
START
:
Cool!
You might have noticed that you now have three different migrations for four 
different versions of your database. If one of your users had the first version of your 
database installed and wanted to update the app to the latest version, Room would 
execute each migration one by one. Since four is still a relatively low number, the 
process should be quick, but imagine if you had 50 versions of your database! It 
would be much better to have a shortcut right?
Well, Room allows you to define a migration path that starts from and goes to any 
version of your database. To illustrate this concept, define a migration that goes 
from database version 1 to 4.
Saving Data on Android
Chapter 10: Migrations with Room
raywenderlich.com
184


Create a new class under the migrations package and name it 
Migration1To4

Replace everything inside with the following:
class
 
Migration1To4
 : 
Migration
(
1

4
) { 
  
override
 
fun
 
migrate
(database: SupportSQLiteDatabase)
 { 
    database.execSQL(
"ALTER TABLE question ADD COLUMN difficulty 
TEXT NOT NULL DEFAULT '0'"

    database.execSQL(
"ALTER TABLE question ADD COLUMN category 
TEXT NOT NULL DEFAULT 'android'"

  } 
}
The above simply executes two 
ALTER TABLE
 statements to add difficulty and 
category columns. Since these columns didn't exist in the version 1 database, we 
don't have to worry about the more complex SQL for the migration in the prior step.
Open the 
QuizDatabase
 class and add the following line to your companion object 
to create a reference to your new migration:
val
 MIGRATION_1_TO_4 = Migration1To4()
Now go to the 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   188   189   190   191   192   193   194   195   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish