Saving Data on Android



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

RENAME TABLE

RENAME COLUMN
 
and 
ADD COLUMN
.
If you want to perform complex schema changes such as changing the type affinity 
of a column, you'll need to use more than one query. But don't worry, the following 
steps summarize the process:
1. Create a new temporary table with the new schema.
2. Copy the data from the original table to the temporary table.
3. Drop the original table.
4. Rename the temporary table with the same name as the original table.
To illustrate this process, imagine you want to modify the type affinity of the 
difficulty column to TEXT instead of INTEGER so that you can store the operating 
system that the question refers to.
To do this, open 
Question.kt
  and modify the category property:
@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: String = 
"0"

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

Modify everything like this:
class
 
Migration3To4
 : 
Migration
(
3

4
) { 
  
override
 
fun
 
migrate
(database: SupportSQLiteDatabase)
 { 
    database.execSQL( 
      
"CREATE TABLE question_new (question_id INTEGER NOT NULL, 
"
 + 
          
"text TEXT NOT NULL, "
 + 
          
"difficulty TEXT NOT NULL, "
 + 
          
"category TEXT NOT NULL, "
 + 
          
"PRIMARY KEY (question_id))"
 
    ) 
//1
 
 
    database.execSQL(
"CREATE INDEX 
index_question_new_question_id ON question_new(question_id)"

//
Saving Data on Android
Chapter 10: Migrations with Room
raywenderlich.com
182


2
 
 
    database.execSQL( 
      
"INSERT INTO question_new (question_id, text, difficulty, 
category) "
 + 
          
"SELECT question_id, text, difficulty, category FROM 
question"
 
    )
//3
 
 
    database.execSQL(
"DROP TABLE question"

//4
 
 
    database.execSQL(
"ALTER TABLE question_new RENAME TO 
question"

//5
 
 
  } 
}
Briefly:
1. Creates a new temporary table with the new schema called 

Download 19,28 Mb.

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