Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet61/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   57   58   59   60   61   62   63   64   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

SQLiteOpenHelper

This will allow the class to utilize the Android SQLite APIs in a simpler way. Your 
class declaration should now look like this:
class
 
ToDoDatabaseHandler
(context: Context) : 
    SQLiteOpenHelper(context, DATABASE_NAME, 
null

DATABASE_VERSION) { 
...
Next up, you have to give the helper the schema data required to build the database.
Crea/ng the database
First, the database must be created if it does not already exist. This is automatically 
done by the 
onCreate
 method, which will run only when it needs to. If the database 
already exists, this method will not run.
Note
: When testing an app that utilizes SQLite, you must uninstall the app to 
delete the old database and reinstall the app to recreate it or completely clear 
the data of an app.
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
53


Add the following code to the 
onCreate
 method, in the 
ToDoDatabaseHandler
:
// 1
 
val
 createToDoTable = 
""" 
  CREATE TABLE 
$TABLE_NAME
  ( 
    
$KEY_TODO_ID
 INTEGER PRIMARY KEY, 
    
$KEY_TODO_NAME
  TEXT, 
    
$KEY_TODO_IS_COMPLETED
  LONG ); 
"""
 
// 2
 
db?.execSQL(createToDoTable)
The parts above:
1. Assign an SQL 
Create
 statement which creates the table described in the schema 
above, to the 
createToDoTable
 value.
2. Run the command using the database object provided by the 
SQLiteOpenHelper
.
The above method will be run anytime the app is run and the database is not found. 
Otherwise, the database exists, and there's no reason to recreate it.
One of the downsides of SQL, in general, is having to write the statements perfectly. 
Be very mindful when creating SQL statements using concatenated strings. It is very 
easy to make an error in doing so. It can be helpful to print the query using a 
Log
 
statement to check to see if the string is correct. The text of the query string is also 
displayed at the end of an error message in 
Logcat
 if the app crashes with an error. 
Additionally, you could use a local, or a Web SQL database terminal or environment, 
to try and validate the statement.
Now that the database has been created, you must add a mechanism for upgrading it. 
Add the following code into the 
onUpgrade
 method:
// 1
 
db?.execSQL(
"DROP TABLE IF EXISTS 
$TABLE_NAME
"

// 2
 
onCreate(db)
The above:
1. Executes the SQL statement to drop the old the table if it exists.
2. Calls onCreate again to re-create the database, this time with an upgrade.
Saving Data on Android
Chapter 3: SQLite Database
raywenderlich.com
54


The 
onUpgrade
 method is the one that the framework calls automatically when a 
different version of the DB is detected. This happens when an instance of the 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   57   58   59   60   61   62   63   64   ...   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