17 Creating an SQLiteOpenHelper
67
1
db.createTable(CityForecastTable.NAME, true,
2
Pair(CityForecastTable.ID, INTEGER + PRIMARY_KEY),
3
Pair(CityForecastTable.CITY, TEXT),
4
Pair(CityForecastTable.COUNTRY, TEXT))
• The first parameter is the name of the table.
• The second parameter, when set to
true
, will check if the table doesn’t exist before trying to
create it.
• The third parameter is a
vararg
of
Pair
s. The
vararg
type also exists in Java, and it’s a way to
pass a variable number of arguments of the same type to a function. The function will receive
an array with the objects.
The types are from a special Anko class called
SqlType
, which can be mixed with
SqlTypeModifier
s,
such as
PRIMARY_KEY
. The
+
operation is overloaded the same way we saw in chapter 11. This
plus
function will concatenate both values in a proper way returning a new special
SqlType
:
1
fun SqlType.plus(m: SqlTypeModifier) : SqlType {
2
return SqlTypeImpl(name, if (modifier == null) m.toString()
3
else "$modifier $m")
4
}
As you can see, it can also concatenate several modifiers.
But returning to our code, we can do it better. Kotlin standard library includes a function called
to
which, once more, shows the power of Kotlin to let us model our own language. It acts as an
extension function for the first object and receives another object as parameter, returning a
Pair
object with them.
1
infix fun
A.to(that: B): Pair = Pair(this, that)
Functions with one parameter that use the
infix
modifier can be used inline, so the result is quite
clean:
1
val pair = object1 to object2
And this, applied to the creation of our tables:
17 Creating an SQLiteOpenHelper
68
1
db.createTable(CityForecastTable.NAME, true,
2
CityForecastTable.ID to INTEGER + PRIMARY_KEY,
3
CityForecastTable.CITY to TEXT,
4
CityForecastTable.COUNTRY to TEXT)
This is how the whole method looks:
1
override fun onCreate(db: SQLiteDatabase) {
2
db.createTable(CityForecastTable.NAME, true,
3
CityForecastTable.ID to INTEGER + PRIMARY_KEY,
4
CityForecastTable.CITY to TEXT,
5
CityForecastTable.COUNTRY to TEXT)
6
7
db.createTable(DayForecastTable.NAME, true,
8
DayForecastTable.ID to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
9
DayForecastTable.DATE to INTEGER,
10
DayForecastTable.DESCRIPTION to TEXT,
11
DayForecastTable.HIGH to INTEGER,
12
DayForecastTable.LOW to INTEGER,
13
DayForecastTable.ICON_URL to TEXT,
14
DayForecastTable.CITY_ID to INTEGER)
15
}
We have a similar function to drop a table.
onUpgrade
will just delete the tables so that they are
recreated. We are using our database just as a cache, so it’s the easiest and safest way to be sure
the tables are recreated as expected. If we had important data to be kept, we’d need to improve
onUpgrade
code by doing the corresponding migration depending on the database version.
1
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
2
db.dropTable(CityForecastTable.NAME, true)
3
db.dropTable(DayForecastTable.NAME, true)
4
onCreate(db)
5
}
Do'stlaringiz bilan baham: