Databases, Persistent Memory, Cloud-Based Real-Time Storage
This course explores a variety of features, including databases, persistent memory, and cloud-based real-time storage, and how you can use them when building your Android apps.
This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.
Since this is a beginner-level course, there are no requirements, but any previous experience with coding would be beneficial.
Hello and welcome to the section. In this section, we will learn how to work with persistent memory, which basically means we'll be able to store and retrieve data on our phone so we don't lose things. So far, we've worked with lists and other items which have been temporary, like lists of posts which got created when the app starts up and then we lost it once we closed or restarted the app. Now, we'll see how to store our data. So, user data that is created when creating, updating or deleting posts. So, we don't lose it if we close our app or restart or reinstall them. And we'll do this using a simple lightweight database called SQLite, and it's stored in your phone's memory.
So, the emulators memory in our case. But that's not all the options we have. We can store data online as well. When you're dealing with tons and tons of traffic and data like Facebook or Twitter, you 'd want to store the data on the cloud using services like Google's firebase or Amazon's web services and so on, and simply retrieve the data from there to display in the app. And this type of structured storage is called a database. A database usually includes a collection of tables with columns and rows. Think of Excel spreadsheets with each Excel file being a database and each tab or sheet within the file being a table. And for a blog application, a table could be posts, another table could be users who create posts and so on.
Now, let's look at the user's table. You'd have a table like this with column names as the attributes. So, you can think of each row as a user object. So, password would be a column name or an attribute, and then username and email would also be column names with each row below it representing each user. Now, one specially important column in each table would be one which is unique for each role, which can be used to identify each record, users in this case. And in this table's case, it would be the id column And this column is known as the primary key. So, very important concept to remember. Usually, this column would be a unique number associated with each row.
Now, the great thing about this structure and having multiple tables within a database with an identifying column like primary key is you can associate tables together. So, let's say you have a users table and posts table that are separate. Instead of saving all the user's data like name, email, password and all of that in the posts table here, you can instead have a separate table for users where you store user information and a table for posts, and simply associate a user with a post so you'd have a creator of the post. And let's say here we're trying to say that user with id 2, Evgeny, created post with id 1, title barbell. How do we create this link? Well, we can add a user id column in the posts table, and just store the authors user id in it.
So, here we store Evgeny's user id, which is 2, in the user id column for that post. Let's look at another. We can say Mashrur is the author of this post about singapor, and this one about powerlifting. To make this association, all you do is store Mashrur's primary key in those two rows. Here we see one in the user id column, which is the primary key for Mashrur. So, if we extract and look at the posts table, we 'll have the rows of data containing information we need about each post with the id being the primary key for each row of data, and would be storing the primary key for each author who created that post. And this user id attribute, which is the last column here on the right, this column of data has a special name as well, and this is called the foreign key.
This special name is given to it since this is used to link the two tables together and identify the author who created each post. So, a primary key in one table can be a foreign key in another table it's linked to. So, now that we have taken a basic look at the structure of databases and tables, let's look at how we can perform some basic database operations or functions on tables. And we'll use our posts table to explore this. The first operation that comes to mind is creating a new post which would be C for create. The next thing we can look at is reading data from the table. So, taking a look at an individual posts or multiple posts could be termed as a read operation. So, R for read. Updating information in the table. So, updating a post in our case, U for update and deleting a post from the table. So D, for delete. And these four create, read, update and delete make up the CRUD operations associated with databases.
Now, to perform these CRUD operations from your app, where you have your application code and then you have your data source or database which is somewhere else, either local storage on your phone or in the web somewhere, we can use SQL which stands for Structured Query Language. Since you're querying your data source, whatever it may be, for data. And SQL is very popular, has been around for decades, and lots of database servers and applications are available for it. Like Microsoft, SQL Server, PostgresSQL, Oracle, and a lightweight version that we'll use for our apps local storage called SQLite. So, to finish up our introductory look at databases and CRUD operations, let's quickly take a look at how these operations actually happen. And we'll use our posts table to visualize this. So, starting with create, to create a new post we'll simply add a new row to the table with title and description, and we'll program it as such that the id field which will be our primary key will be automatically filled in by our app.
To read a post, we simply look up a post by its id, and retrieve the information from the table to display it. And that would be if you wanted to see a specific post. But if you wanted multiple posts to display, you could also specify a criteria in your query to do so. To update a single post, you could look up the post by its id, and then on that row for the column you want to update, simply change the data there. So, here would update the description column for our post with id 1 with a new description. And lastly to delete a row, you simply remove the row from the table. Here if we wanted to delete the first post, would simply remove it and end up with the post table looking like this without that row. So, that covers our introductory look at databases and standard database operations that we can perform. In the next video, we'll dive right in and build a simple app, where we'll learn how to perform these operations using android studio and the built-in SQLite database. Hope you're excited. I'll see you there.
Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.