Android SQLite Database Tutorial 📱 Complete 1-HOUR SQLite Android Tutorial | Kotlin & Android Studio

preview_player
Показать описание

In this video, you will learn everything about the Android SQLite Database with a great examples.

Using this Android SQLite database tutorial I will show you how to perform all the crud operations like Create, Insert, Update, and Delete.

In-depth Android SQLite Database Tutorial
SQLite database is an opensource, lightweight, no network access, and standalone SQL database that stores data to a text file on a device. SQLite database supports all the relational database features. SQLite is a build-in database implementation that comes with Android. You don’t need to establish any connections like JDBC, ODBC, or any other external connection for it, like what you will need to do in java applications.

In Android, whenever an application needs to store a large amount of data the SQLite is the most preferred. It is better than any other repository systems like SharedPreferences or saving data in files. For many applications in Android, SQLite is the backbone of the app, whether it’s used directly or via some third-party wrapper.

SQLite Database Package

Three main components are used to handle SQLite operations in Android:

SQLiteOpenHelper Class
The SQLiteOpenHelper class is used to determine the name and version of the database used in this class. It is responsible for opening the database if it exists, creating if it does not exists and upgrading if required.

There are the onCreate() and onUpgrade() methods.

onCreate() invoked if the database doesn’t exist, meaning the very first time dealing with the SQLite database. Moreover, the onUpgrade() method is used to update the database schema to the most recent or, let’s say, the existing without losing the data:

SQLiteDatabase in Android
The base class of the SQLite database class is SQLiteDatabase.

Android has its implementation to perform CRUD (Create, Read, Update, Delete) operations.

The SQLiteDatabase have methods such as insert(), update(), delete() and also execSQL() which are used for the execution of the database.

Cursor
The query function will return the cursor object, which results from a query. One record is equal to the row of the query results.

In Android, the cursor can perform buffer query results efficiently as it does not need to load the data into the memory.

The most commonly used methods of the cursor are getCount(), moveToFirst(), moveToNext(). The getCount() used to get the number of records from the query results. The moveToFirst() used to move to the first record of the line. And the moveToNext() used to move to the next line.

If you need additional details from our SQLite android tutorial, you can have a look at the official documentation of Android SQLite Database.

For using the methods insert() and update(), you need to define the object ContentValues in terms of KEY and VALUE to the table. The key represents the identity column of the table. And the value represents the record of the table of the column.

The query can be built in the SQLite database with the methods such as rawQuery() and query() using the SQLiteQueryBuilder class. The rawQuery() method accepts an SQL statement as input.

The query() method has a structured interface in terms of parameters to specify the SQL query. The SQLiteQueryBuilder class allows you to create SQL queries.

Example of Query()

1
Example of rawQuery()

Cursor cursor = getReadableDatabase();
rawQuery("select * from EmployeeTable);
1
2
Cursor cursor = getReadableDatabase();
rawQuery("select * from EmployeeTable);

#SQLite #androidSQLite #androidSQLiteTutorial

Check out the article including the code:

tutorialsEU offers you free video tutorials about programming and development for complete beginners up to experienced programmers.
This includes C#, Unity, Python, Android, Kotlin, Machine Learning, etc.
Рекомендации по теме
Комментарии
Автор

Yay! I learnt something new today thanks mate.
Completed!!!!

TheImaginativeSachin
Автор

I am here July 2021
Gonna comeback here after some years. You'll be part of it man.
Thank you and keep making this type of videos you are awesome!

jaygarzon
Автор

Great vid bro, like always, vielen dank!

MateusMeurer
Автор

Thanks, great job! I tried to insert the data after following the video but it say unfortunately the app has stopped. What could be the problem and possible solution please?

peteroyelegbin
Автор

Question: Should the "set visibility to true" come as a last statement after the update of the RV is done, instead of being the first?
Anyway: Great explanations, very detailed. Thank you. I will recommend. :-)

metanormal
Автор

Great job, excelent tutorial. Danke!!

mxhotta
Автор

i love your work buddy!
but i got a querry, what can i do so that even if the number of records exceed 9 i can still add more records?

davidkitonga
Автор

Thanks a lot. My projet is a lot better now!

LaurinusPonpon
Автор

nice easy explanation tutorial is less so thanks for making Kotlin video ...it is possible data store in mobile in automatics daily convert in pdf and email on particular email when mobile is connected to internet ...i thought for this visitor list entry list in society ....Thanks

prakashanuvadia
Автор

Using volley to sync with mysql, both ends destination table a sync_status column for sync

n.w.aicecube
Автор

Hey, the code shows no error but still when i press the button that leads to insertion of values into the table,
i am getting a result of -1 from the db.insert() method . Can smone pls help me?

sharanpreetchadha
Автор

Hey guys this video was awesome, But I want to really understand SQLite a little bit more.
Any other video worth mentioning to watch?

TheImaginativeSachin
Автор

It works well, but when I change the add Record Form to a new activity, same parameters. Just made this small change. Then the app crashs.

I dont change anything in the .kt files, (only import the new activity in MainActivity.kt, in order to avoid red flags.

I check no red flags anywhere...after moved the add record form into a new activity and it crashes. the app dont even open.

maxhooker
Автор

Please can you tell me how can i store and retrieve rich text (bold/italic/underlined) in sqlitedb

siddharajsinhrathod
Автор

Where is the code of activity_main? There a lot of pieces missing, like images, contents of build.gradle, imports of MainActivity, ItemAdapter etc. can you just upload this to github or something, or should I buy the course to have access to full code? I have learnt something anyway just by watching the video, thank you.

macaronivirus
Автор

able to use until this point" Output after inserting the employee record, " but toast out didnt appear after press the button

micahspecial
Автор

How about SQLite in a desktop Kotlin application? Suppose I want to access a SQLite database from Windows on my local hard drive. Is this code translatable to the desktop? "Room" is only usable inside an Android Context, so it cannot be used on the desktop. That's why this code is valuable if it can be used in a Windows program :-)

GrooveMan
Автор

Really good, have you explain with this example but how use webservice....sry my english

jorge
Автор

Why don't u use $ sign to insert variable in the string of creating table

kingfisher
Автор

how to store database on external? like Facebook, Twitter. Use for online and export to BI.

MukkiBassist