Saving Data on Android


En//es To create a table using Room you need to create an  entity



Download 19,28 Mb.
Pdf ko'rish
bet123/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   119   120   121   122   123   124   125   126   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

En//es
To create a table using Room you need to create an 
entity
, just like you did above. 
Entities in Room are defined by using a series of different 
annotations
 on classes. 
The following are the most common annotations that you will use when defining an 
entity:
@En/ty
The 
@Entity
 annotation declares that the annotated class is a Room entity. For each 
entity, a table is created in the associated SQLite database to save your data. To add 
the annotation, simply use the following approach:
@Entity
 
data
 
class
 
Movie

    
//...
 
)
The above code would create a 
Movie
 table in your database. By default, Room 
always uses the name of your class as the table name, but you can use the 
tableName
  
property of the 
@Entity
 annotation to set a different name like shown below:
@Entity(tableName = "movies")
 
data
 
class
 
Movie

    
//...
 
)
Now that the class is declared as an entity, you can use it in Room's setup, which 
you'll see in a bit.
Saving Data on Android
Chapter 6: Entity Definitions
raywenderlich.com
108


@PrimaryKey
The 
@PrimaryKey
 annotation allows you to define a primary key for your table. It is 
very important to remember that each entity in Room 
must have
 at least one field 
defined as primary key:
@Entity
 
data
 
class
 
Movie

    
@PrimaryKey
 
var
 id: 
Int

    
var
 title: String?, 
    
var
 description: String?, 
    
var
 releaseDate: String?, 
    
var
 rating: 
Float
 
)
You can also let Room generate the primary keys automatically for you, using the 
autogenerate
 property of the 
@PrimaryKey
 annotation:
@Entity
 
data
 
class
 
Movie

    
@PrimaryKey(autogenerate = true)
 
var
 id: 
Int

    
var
 title: String?, 
    
var
 description: String?, 
    
var
 releaseDate: String?, 
    
var
 rating: 
Float
 
)
In the above code, the id is the primary key for your Movie table, and it is going to be 
automatically generated by Room incrementing the value each time by one 
(1,2,3,4,5,...). It is important to remember that if you set 
autogenerate
 to 
true
, the 
type affinity for the field must be 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   119   120   121   122   123   124   125   126   ...   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