-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.kt
60 lines (45 loc) · 1.57 KB
/
database.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.example.finalcoursework2
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class League(
@PrimaryKey(autoGenerate = true) var idLeague: Int = 0,
val strLeague: String?,
val strSport: String?,
val strLeagueAlternate: String?,
)
package com.example.finalcoursework2
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@Dao
interface LeagueDao {
@Query("select * from League")
suspend fun getAll(): List<League>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(vararg league: League)
// Insert one league without replacing an identical one
@Insert
suspend fun insertLeague(league: League)
@Delete
suspend fun deleteLeague(league: League)
// Query to find a league by its name
@Query("SELECT * FROM League WHERE STRleague LIKE :name")
fun findByLeague(name: String): League?
@Query("DELETE FROM League")
suspend fun deleteAll()
// Custom query to save data to database
@Query("INSERT INTO League (strLeague) VALUES (:data)")
suspend fun saveDataToDatabase(data: String)
@Query("SELECT * FROM League WHERE strLeague LIKE '%' || :leagueName || '%'")
suspend fun searchLeaguesByName(leagueName: String): List<League>
}
package com.example.finalcoursework2
import androidx.room.Database
import androidx.room.RoomDatabase
@Database(entities = [League::class],version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun leagueDao(): LeagueDao
}