Saving Data on Android



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

QuizAplication.kt
 and modify your database builder inside 
onCreate()
:
database = Room.databaseBuilder(
this
, QuizDatabase::
class
.
java

"question_database")
 
    .addMigrations(QuizDatabase.MIGRATION_1_TO_2) 
//Only this 
line changes
 
    .build()
addMigrations()
 accepts one or more migration objects. Room will use these 
migrations to bring the database to the latest version.
Build and run your app and press 
START
 to verify that your migration works 
properly:
Sweet! It looks like your app works now.
Saving Data on Android
Chapter 10: Migrations with Room
raywenderlich.com
179


Next, imagine that you want to sort your questions not only by difficulty but also by 
a category, such as 
iOS
 or 
Android
. This way, you can expand the functionality of 
your app to display Android questions to Android developers and iOS questions to 
iOS developers.
Expanding the functionality of your app to support categories requires some changes 
in your database:
• A new property inside your question entity called 
category
 of type String.
• A new migration that goes from database version 2 to 3 and handles the new 
schema change.
• Adding the new migration to your database builder.
Start by opening the 
Question.kt
 file and adding a new 
category
 property to your 
class:
@Entity(tableName = "question", indices = 
[Index("question_id")
]) 
data
 
class
 
Question

  
@PrimaryKey(autoGenerate = true)
 
  
@ColumnInfo(name = "question_id")
 
  
var
 questionId: 
Int

  
val
 text: String, 
  
val
 difficulty: 
Int
 = 
0

  
val
 category: String = 
"android"
 
//Only this line changes
 
)
Now, create a new class under the migrations package and name it 
Migration2To3

Replace everything inside with the following:
class
 
Migration2To3
 : 
Migration
(
2

3
) { 
  
override
 
fun
 
migrate
(database: SupportSQLiteDatabase)
 { 
    database.execSQL(
"ALTER TABLE question ADD COLUMN category 
TEXT NOT NULL DEFAULT 'android'"

  } 
}
Here, you're creating a new 
Migration
 object that goes from database version 2 to 3. 
The query executes an 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   185   186   187   188   189   190   191   192   ...   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