Skip to main content

Loading tasks from the database

Lesson 18 from: Developing Android Apps with Java

Tony Hillerson

Loading tasks from the database

Lesson 18 from: Developing Android Apps with Java

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

18. Loading tasks from the database

Lessons

Class Trailer

Day 1

1

Overview: What is Android?

19:56
2

Eclipse and SDK setup and build a Red Flashlight app

38:05
3

Ad a button to a Green Flashlight

21:06
4

Add the Green Flashlight

18:48

Day 2

5

Week 1 - Layout and Build a Task Manager App

04:10
6

Displaying a list of tasks

40:44

Lesson Info

Loading tasks from the database

we need to load the tasks out of the database. Let's start with that. So right now Ah, when the application starts, we load all the tasks that that the application knows about. Display them in the list and ah, nothing is actually going to change their We're gonna have the same exact behavior. But in this case, we're gonna have to deep down in their re factor to get the list or compose a list from the database instead of from by creating a new list just a blank list to start with, which is what we did before. So let's talk about what the sequel light Open Helper is going to do to help us there. This is a flow chart that talks about what a sequel light opened helper will do given a couple different states. So when you construct a secret laid open helper class, when you create a new class ah, it Well, the arguments that it takes are the file name for the database. You could have a couple different databases in your application, and it takes a version number. The version number helps the s...

equel light open. Help her decide if anything has changed about the database and if it needs to be upgraded, So in the case of that, the file ah doesn't exist. This equally open helper will create the database file for you, and then it will call back to a method called on Create on your Instance of the class and after on create is done, then it assumes that the database is ready, and in the case that the fellow does exist, then it will check the version number. And if the version number has changed since the last time this sequel, Light Over Helper, has looked at the local database. So this will be in the case where you've shipped a new update to your application and user is downloaded it and runs it for the first time and then this for the first time since the upgrade and then circulate open Helper opens up looks at the version number and sees that it's changed. So then it will call out to a helper method called on update, and you can deal with any changes to the structure of the database, any changes that you need to make to existing data to fit that in structure and then, in that case, the database, we will be ready. And then in the most common case, probably the file will exist, and the version number will not have changed. In that case, the physical a database will be ready as well. And that's what the sequel wide open helper helps you do. Get through a couple of different those of those common cases that you have to deal with. So the first steps that we're gonna need to take, ah, when we create the database is too Ah, use a sequel statement that is called a create statement. So let me kind of give you an idea of what that looks like to create a table with sequel, you use a create statement and create statement is just simply create table, table name. And then, um, instead of currencies, list of columns and their definitions of their types. An example here is this is what we're gonna be using in our code create table tasks that will Chris. This will create a table that we call tasks and it will have an I D. And it will have a name. It will have a complete and will distort that as text because there's no ah Boolean in, ah sequel light. We could use an energy with zero or a one if anybody's familiar with sequel sequel is set up. But what I'm actually going to do is store a string Ah, and have ah have Java interpret that string for us into a Boolean value. So, um, if anybody, if any of the people that were not familiar with sequel are also not familiar with the kind of the concept of a database basically think of like an Excel spreadsheet, that's, ah, that's a good place to start, although it's it's better than it's better at doing. A lot of things in an Excel spreadsheet would be, but it's it's do you think of columns and rows? And what we're defining here is a list of columns which are, um, which good court of sort of correspond to attributes on an object and then each one of the rose in the database are going to be specific instances of the type that the table holds. So in this case will have a table called tasks, and the columns that will that will store data about for those tasks are um, and I d, which will uniquely uniquely identify each task and then the name, which will be the name of the task that we displayed inside the lists. And then whether or not it's complete. And then each row will get out of the database and turn into a task object. And that's kind of a common pattern. And what's called object relational mapping? Um, which there's a lot of software that takes care of. But in our case, we're just gonna hand roll our own little solution where we pull pull data out of the database and put data into the database explicitly based on attributes on objects like the task object. Um, one more bit of information I may need to explain here is what's going on in that I d line. Um, So what you need inside of a database commonly is a way to uniquely identify Ah, particular row, um, compared to other rose in the database. So we have here, um ah, the most common way of doing that, which is by creating what's called a primary key and the primary key. Um, the database will manage to make sure that there is no duplicates so that this is just an I. D. Some number. It really doesn't matter what it is. Um, but it needs to be unique, unique to each row. So the database is gonna make sure because that's a primary key, that there's never a duplicate put into the database, and then the auto increment they're just means that it's gonna take care of creating the I d for us and making sure it's not unique in a serial manner. So the first time we insert one, it'll probably end up being one for that I d. The next time we insert a task, it'll probably end up being too. And if we deleted, too, um, it maybe to the next time we insert a task, um, or maybe three. But in any case, it's up. Take it up to the database to deal with that. So we don't have to deal with that in our code, which is nice. And then at the end is a ah is a little phrase not know which makes. Make sure that the database will not allow that I d to ever be No. So, uh, this may be new for a lot of people that maybe was a kind of a long explanation. I don't know if there is there. Is there any questions about creating database tables? The only question I got related to this was somebody was curious why the complete column was not a Boolean instead of just yes, explain that there's no there's no bullying type for a sequel. Light. Um, I could have started his imager and either done a 01 and then translated that. But I just found it easier to store. It is a string and allow job it to get it. Um ah, there's a method on bullying, I think that will interpret a string as a Boolean. And then the other thing somebody noted that not know might not strictly be necessary because it might be that by default Obrycki's. But that might be something that you just wanted to call out so that we all know that by default, it's not know. Yeah, that's a good point that that may be the case. Is there an o. R M for Android? And I hear there's one on the IPhone. Yeah, the IPhone has core data. No, there's not anything provided by Google for the IPhone or for the Android Ah, framework. Um, and I don't know if something like Hibernate will work or if there's like, like a light hibernate or something. That's a good That's a good topic for the form. Okay, cool. All right. Now, once we've created a table, at some point, we will be putting data into the table that we want to get out. And when that's the case, then we use the most common sequel statement that you'll probably run into, which is the select statement. So let me talk through that really quickly. Eso select statement is how you get a row or a set of rose out of a database table or set of tables. And the way you do that is by selecting, um, you are issuing a select statement, which, which is composed of select keyword and then a list of columns from from keyword and then a table name or list of table names and then optionally inside brackets. I I used brackets to mean optional um optionally aware statement, which allows you to make, um, um statements about how you want certain things to be so in this example select statement here. I just have a select star, which is a special, um, sort of a keyword type, I guess. Or an operator? Maybe. I don't know. I guess what with the technical term there, but is But it's a ah, a way to say, just select everything. So select everything from tasks that will select all columns from tasks. Um, but what I could add add there is, say, select everything from tasks where complete equals true and then that will only select the completed tasks or were complete equals false. Where and that will get all the tasks that are not complete. So that's a pretty simple statement. It's the one you that you probably use the most and status equal. Okay, so let's get into some code. What we're gonna do is go through these steps. We're going to create a sequel light open Helper to manage the database or help us manage the database. Then we will implement the on create call back inside that helper, and that's where we will create the task table. And then inside the task manager applications on create call back is where we will populate the array lists that we that we already use to hold all the tasks that we have. I said, I'm gonna switch over to Eclipse, and we'll get started. Okay, So the first step is to create the sequel Light Open Helper. I'm going to put this in the tasks package because it will. This will be we only have one table in our application, but I could have multiple tables, but ah, and then I would organize things just like I'm doing right now around the objects that go in that in that, um, sort of a set up. Oops. I don't want to create a file. Sorry. Step back here. So I'm gonna create the sequel. I'd open helper in the tasks package by right, clicking the package, going to new going to class, and I will call this the Tasks sequel. Light Open helper. Super Class is secret Late, Open helper, which is in the Androids, Dave. A sequel, eight package. And then when I click finish, I think it will create a few methods for me. Okay, there we go. Michel. Line numbers here. Okay, So we're extending a secret light open helper, and, um, since we've generated this Ah, class from eclipse. It will give us a couple different of the methods that her. Since this is an abstract classical, it open helpers, abstract. Ah, there two methods that we need to implement, which is on create and on upgrade. And then it looks like a clips has a complaint about something. Let's figure out what that is. So it doesn't like not having a constructor. So as I hope her over this problem here, underline in red. Yes. Actually, Tony, I was gonna remind you to make your font bigger. Sorry. Yeah, I guess one of the things I did to try to make my STK work is changed. Workspaces, which didn't actually help but did changed my thought. So here we go. Text fought at it. 18. Okay. All right. Now, can people see? Yes, that looks a lot better. All right, cool. Good. Sorry about that. Okay, So, um, since there is a there is not a no arguments helper or I'm sorry. No argument, Constructor, we need to add our own constructor. So I over over here and I can click this little quick help guy, and this will create a constructor for us. So Here's the arguments that it takes. Ah, the constructor for Secret Open Helper takes a context and it takes the name, which is the name of the database and a cursor factory, which we're not going to use here. That's more for a more advanced implementation. And then it takes a version so that those air those kind of go back to the things I talked about in the slides. But what I'm gonna do here is just get rid of all these, um arguments because my secret wide open helper will not need to know about these things. I will. I will contain those all inside of, um, the call to super instead. So the name it's gonna be the name of the database, like I said. And let's just keep that inside of a constant. So I'm gonna pasting Acosta here. This is a nice way to manage when you have to keep strings instead of java, um, to manage, kind of making sure that the strings don't change all over the place. Instead, we use this constant books that final string DB name, and that's what we'll use to name the database, and we'll pass that up to the super constructor. So that will be the name of the tasks database locally. Um, the next thing it wants to know is what is my cursor factory, which is again, is more of advanced thing, and I don't really need one here. I can allow it to create its own. And then the last argument is version. Someone would create a public static final. It constant here, and we'll say that the version is one. So now that I when I pass this up to the super constructor, um, inside of here, all have to do in later implementations. If I ever changed the database, the structure, the database, then I'll just come here. And I will say that the version is too. And the next time this secret over and helper runs, it will know that the version number has changed from what it knows about the local database. And then, in that case, it will run the on go up on upgrade call back. But if the version number is, um, one or if the version number has not changed or the database does not exist that it knows it's starting from scratch, and then it will call on Create. So what we want to do on create, uh, this extra comment stuff here? So what we want to do on create is create the table. So let's say, um, drop and create. Actually, that's completely unnecessary. I've got that in my code. Um, and it's not gonna worry about that. I want to set create tables instead. So I'm gonna call this method, create table and pass in the instance of the database, and then that method doesn't exist. So I'm actually gonna pace that in here, and then we'll have a look at what we do. Okay, So, uh, hopeful everybody's following along the first time we use the secret wide open helper. The database file will not exist, and it will call on create. And then we will call over to our little helper method here, create table, uh, which I placed it in his create tables of I delete this. Then it will. It will. You will find that. And then we're expecting an argument of sequelae database. And so the way that I execute any arbitrary sequel against this data bassist by calling Execs sequel or execute sequel on that database instance. And what we're gonna do here is is ah, use that create table syntax or the great table statement to create a table. And what I want to do also is, since I'm going to be referring to the name of this table throughout, um, this secret, wide open helper and possibly one other class, I want to make sure that nobody messes up the name of the table. I want that to be a constant as well. So I'm gonna hover over here an eclipse and say, Create constant tasks table. But I do. I don't want it to be well, so this little red box here means there's gonna be private. That's fine. I'll just go and fix that. So when I click this, it will create a constant, and then you could see an eclipse. That it's, um the constant it just created is no longer a problem. And it's surrounded by a box, and what I can do is tab out of that and it will tab up to the code that had just generated So hit tab. And then I'm appear at the top, and I could make choices about some of the optional things that that eclipse generated for me. So I don't want this to be a private static variable. I want this to be a public variable, and string is fine. Task stable was the name that I want and I can tab in here at the end and make this string, and I'll call this tasks because the table will just be tasks. And then now you can see everything is still surrounded by a box and I can hit Tab, and it will have me back down to Teoh where I started off. So you can see that I also need to do this for these other guys. Um, again, I'm creating these as constant so that it's never It's everything reversed to these variables. And then I don't have to worry about anybody getting the string names wrong. So I'm gonna paste in code at the top That will create these other constants. So these correspond to the task I d column. The task name column in the task Complete calm. And now this creates statement should look as it ah as we expect it to. So, um, once this translates into a string which it will inside of here. Once we've interpreted these variables than it will be create table tasks followed by a Perrin and then the task Ah, I d Which is going to be I d, which is Inter manager Primary key, Otto and commit, not know and then task name which will be name and test complete, which will be complete. And now we will have after this create table runs for the first time a table to store tasks in with that structure And you can see there's one warning up here. I think that's because that cursor factory got imported just gonna get rid of that. So So everything is running clean, no warnings. And now we have the first iteration of our sequel, Light Open Helper. And again, this on upgrade is gonna be called whenever anything changes. But right now, this is currently unused and will be making use of that in later classes. All right, so now we've gotten one step out of the way, and that's to create this open helper. Now let's look at how we will use the open helper and that will be inside of the task manager application. The first thing that we need to do is to load the tasks when the task manager application starts up. So I'm gonna open that by opening a class are opening a type in eclipse, which is command shift in Mac. And I think option shift in Windows T for type open type, and that will open any Java classes by the name of their class or the name of their type. That's we want task manager application. I could go down and that will open on bar task manager application. Okay, so, um, everybody remembers what this is, right? Ask a question if you don't, But this is the application that runs, um, and stands in for our application, and there is one instance of it, and every activity can get instance of it. And so this is kind of like the way that will encapsulate any creating tasks. So that, um well, this is kind of where we're doing our reef acting are re factoring. Sorry. So when I say add task or when I say get current tasks or something else from some other piece of code, I don't really care from that code. Ah, whether that's coming out of an in memory array or whether that's coming out of an in memory array that was constructed from a database table at some point in time. So that's a good encapsulation. That's a good practice, Teoh, to keep, um, parte secret code, um, from knowing about the implementation details that other parts of your code are responsible for So we can make changes like, um, changing this array list of tasks to be, um, from something that's just created the first time it's run to being populated from a database and nobody else needs to worry about how that happened. So this is the point where we start Teoh, get everything set up this on create method. So the first thing I need to do um that is a change is I'm gonna keep this current tasks. That's fine. This array list will work the same way I did before, And I still want to do this if the current tasks are No, um, I'm not sure if that check is actually needed, because on create, it should always be that that case. Okay, um so in case ah, what I want to do is construct that secret light open helper. So I could do that by tasks. Secret late, Open helper, hit control space. And it will complete that for me. Help her. I need to get an instance of the secret laid open helper. So this is just a standard creation which will take a context. And I can say this because application is also a constant context, just like in activity is and I haven't helped her. And so I haven't run any of that. I didn't run on create. I didn't run anything special. All I need to do is is in Stan. She ate the secret open helper, and it will go through that flow chart that I showed you before. And this lies it will go through this process just when I construct it. So that's all taken care of for me. All I need to do is stand. She ate that. And now, um, after that, I can get access to the newly constructed sequel light database invariable. And I'll keep an instance of that. I'll keep a reference to that on the application. So now I can get that off of the helper, get rideable database, so I could also just get the database for reading. But in our case, we also want to write to it at some point. So we will calls get sequel light are getting rideable database and that will give us a rideable instant something weaken weaken make inserts into. And now I have Ah, this variable that I want to create a field from salt over here, Hover over here, create field database. And now I have a sequel, a database field for the application. So what I want to do, instead of just blindly creating this array list from scratch, is I'll call a method called load tasks over over here, creating message. Now, load tasks will do something like this. Let me paste in some code, and then we'll talk it through because there is a bit of, ah bit of a process to this. This is loading every every row of tasks from the database and creating a new task object at run time. Okay, So pasted in some code here, it looks like I'm coming off the screen here. Me minimize this. I see. Everything should be almost everything over. There is a lot more there. Okay, let me form at this for you. a little bit. Okay. All right, let's talk this through. So the first thing we need to do is just what we did before is create an array list which will hold all the tasks just like the implementation already does. But instead of, um, just stick sticking with a blank array list. We're gonna reiterate over all the tests that come out of the database and create a task object and put that into the realist. So the way it rating over a list of data from the database is done is with a cursor, so you can see that's, ah, class that the clips doesn't know about right now. But when I import Andrey database cursor now will have, um, a cursor class that I could work with. You can see that's an interface that allows random reading right access to a result set returned by a database query. So, um, I also need to and this is this is the benefit here of creating those Constance I can refer to the tasks table and the task I d in the name and the complete and all that stuff inside this stringer Ray, which is the second argument so I could refer to those those columns in that table named by the constants instead of the string values. And then I don't need to worry about this changing. So, um, these air not they don't know clips. Doesn't know where these guys are coming from, so I can fix that by saying import static. Ah, What was that? Secretly open Helper package, which is calm. A rally. Andrew, task manager tasks that secret Open. Help her that star. Now, I have access to all those variables locally, and those are no longer a problem. Task stable task I detest. Name has complete. Okay, So the arguments to this query, our first of all, what table do you want to call this query on task table? I want I want to get information out of the task table. Then, uh, then I want to pass a string array, which is all the columns that I want to select information about. So the format this a little bit groups so that we can read a little bit better in context. The first argument again, his table second argument is all the columns that we want to get out. So we want to get the I D name and complete from each task. And then there's a whole bunch of other stuff that, um, we will probably look at it later. Episodes. I think there's a couple more of these that are used, but basically this corresponds to like, Do you want anywhere statements? Do you want to order by anything? Um, that kind of stuff. You could look at the signature for query. Well, it here, let's look at its here. So these basically corresponded parts of a sequel. Quarry. Um, first argument against the table. Second arguments, list of columns, then selection. Not quite sure what that is. So here, when I hover over the the dock, the help in our hover over a method and eclipse, it will give me this job a doc pop up. And if I hit F two that I could get focused on here, and I can open this window a little bit and then read all about thes arguments. So first parameter is me. Zoom in a little bit. Here. First parameters table Second is column selection is a filter declaring which rose to return. Okay, so this is the where clause This is your sequel Where Klaus is this Ah, this third argument, then selection are eggs. You could pass in arguments to get interpolated into the where clause. Then there's a group by clause, which is a advanced sequel statement thing and in a having clause, which is an advanced sequel statement thing. And then there's an order by which we're actually using so any escape and then zoom out of this and then we'll look at the order by The last thing we want here is to order all the tasks by, um whether or not they're complete. So this will put all of the complete tasks. Um ah group together and then we'll order by test name. So it's in alphabetical order, just cause, uh, things will be a little bit more tighty now, once that quarry executes, ah, we will have a cursor object in this tasks. Cursor and a cursor is something like an it aerator in Java. But, um, it allows a few more things. One thing that we need to do. The first thing is moved to the first item in the cursor, and then well, then we'll be ready to move through the cursor, which again is as a way to navigate through a list of results set that comes back from the database. And what we want to do is first of all, check if the task cursor is after last. If that is true, then there are no results returned. So once we moved to first and then we're if if once we move to first, we are after the last result, then there is no results. So we don't want to do any of this and we just move on and close the cursor, which is what we do finally, anyway, then inside of there is a little do while loop with the the bottom checking wild statement which which moves us to the next item in the cursor. So we're already on the first item when we start off. So we want to have a bottom checking while loop which will move us to the next item and return whether or not it actually made that move. And it will be false if it hits the last one in the list. So this is this is a way to loop through every item in the cursor, do something if we're if we actually found an item at that point. And what we're gonna do inside of here is get information out of the cursor and put it into a tasks object which we've declared here and which we will in Stan. She ate right here because we made a a constructor on our task object. If you remember last time, which took a name which allows us to create a task on passing a name. Um, so the way that information is is had from the cursor is to tell what java type you want and what position in the row that you want to get it from. And that is correspond correspondent to the list of of columns that you that you set up in this string array here the second argument to the quarry. So position zero, we have I d. That position one we have thea name and at Position two is the bullying, and I just parse the boolean value out of the string. That makes it, um that's one easy way to do it. You could also try toe financial that out of ah, one or zero imager. Um and then I'll create the task and put that information of the tasks and finally admit into the ray list. So there's one thing that we need to now add as a concept to task, which is an I. D. S. I'm gonna create a method, said I d on type task and it will create that for me inside of the task object. You can see it or I'm sorry, the task definition could see it created that. And ah, what I want to do inside of set I d is said this that I d equals I d And that will fail because there's no such thing is this that I d saw hover over here and say Create field I D and type task that I could tab up there and see what it created for me for a bit untidy. What I actually want is a long instead because, well, actually, what did I say? I said get anterior. So we're expecting it to be an int. Um, I think it may be the case that we want to start out with a long instead. Let me see if there is a get long, and that is because the, uh, secret light will allow us if there's a get long change this too long and then said, I d will need to change that as well TV a long. Okay, So the reason for that is I think, um, the, uh, energy her inside a sequel Light can be bigger than energy or can be in Java. So I want to make sure that there's enough space and there's no resolution lost their when I get that idea. Okay, so now I have an I d. Long and I have a way to set I D. Um, But I need to hover over here and say, Well, I guess it doesn't give me that that option to create a getter and setter. But I could go to source and I need to create a weight. Teoh, get the I d back out. So I could go to source over I or I'm sorry. Underneath overriding getting implement methods, I can say, get general generate getters and centers and shoes. I d. It will create a get I d ah getter method for me and put that up here at the top of the file. And now I'm gonna tidy this up a little bit grab this guy, but it here now, I have a way to get the I d set the I d on task. And when the application starts up, it will get all the information out of the database, get all the tasks, and it will, um, put them into the ray lists, and this list will function as before. So if I run this, we should see everything working as expected. Okay, good. So we start out with no tasks because there's no tasks in the database, and it didn't crash. So looks like everything is working. Right. Okay, so that was kind of a long segment there, but let's talk through what we did really quickly. First of all, we created a sequel, Light Open Helper Ah, which manages the database for us and manages versions for us. And we created a database for our store tasks in with the create statement. And then we used a select statement to get data out of the database inside of the application, and we did all that and beyond create kicked it off from there. You know, the task manager application. So are there any questions about those steps. Yes, we actually a ton of questions. Um, so, uh, somebody's asking what does help her dot Get rideable database return. When you were calling out to the helper on the task manager application, I I'm going to where we were actually called credible database. We put it into this variable in this variable is of type sequelae database. So that's the sequelae database rapper class. An instance of that guy. That's where the databases getting created. If that file does not exist as we looked at it in your flow chart, yeah, it's getting created somewhere in when this constructor is called. And that somewhere is if we look inside here, I can hover by the way, I can put my pointer on this class hit F three and it will jump to that class and eclipse, which is a nice, nice thing. So that's all happening up here in the Super in the abstract sequel. I'd open help her class. That's where it's going through that flow chart or that decision tree and saying doesn't exist if not created and so on. And we had somebody who asked why some of the database code is in the secret light open helper. And then other parts of the code are in the application, the task manager application. And my guess is that it's the application specific stuff that's in the application, and it's the more generic code that's in the helper. Well, it's more like the secret Open Helper is responsible for managing the structure of the database. And right now I've I've made the the responsibility of the application to get to deal with the data in the database. So that's how the responsibility breaks down right there. If they If this person is, as I suspect, having a little bit of, ah feeling sort of wary about having, UM, sequel embedded in the in the Java code, Um, that's that's one problem that could be fixed by, like, an over em or some other type of solution where we put that sequel somewhere else. I didn't want to go to that extreme length for just the small application. Um, that's so that's one thing that they could be feeling bad about. Another thing is the task manager application having sequel statements in it. It could be another problem that they have so we could create another class, which is responsible for actually interacting with the database. And then that would make the sort of the delineation of responsibility a little more clear between the application and some other class that's responsible for for getting data in and out of the database. And that would be totally fine with me, too. Would probably be a good change to make. But here for this class, it's easier travel code in one place. So hopefully that's a good answer. Okay, got it. Um, we had a couple people ask about the version number and whether that version number is for the APP version or the database version. Or maybe both it. Yeah, basically, both is a good answer. It's It's arbitrary. It's whatever number you want to make it, you can start off 1000. Um, all that you need to do is make sure it changes, and, you know, I'm not sure if it needs to be greater than it used to be. If I would just fail on the side of safety and make it greater than it used to be, Um, and that is just you making greater if your app changes or if the database version changes well, there there, one of the same. You can't You can't change the database without shipping the app. Or, I mean, you can't change the database on on a user's device, which is when all this matters without without shipping a new app. So, yeah, it's it's kind of one in the same, but that that version number doesn't have to correspond anything in the manifest or anything like that. It's an arbitrary number that you should increment, um, serially or or at least increasing, increasing the value of it. Okay. And then we've had a couple of people ask about the import static, and one person was having a problem with the import static. And I don't remember where you did that. Oh, I was in task manager application. I want to get I have these constants task ideas. One task name is another. Their defined on on T secret light Open helper as public variables, but also static variables. So what I can do, um, to get all of these Basically, it's it was added a Java one five, maybe 16 I'm not sure about that. Um, but I can import. Here's the Here's the statement Imports static. That file, um, that class dot star. So it's beyond the class. You're usually importing a class. Right, But, um, so that so that ah, job. It knows how to call it from the classic chur in. But if I import static, um, the class dot star Then I will get all the static variables, static methods and stuff. Um, the compiler will will. Well, let me do this without name spacing. These constants, basically, hopefully that makes sense. That's that's sort of a later addition to java. Um, a couple of people asked about the strings and why we aren't putting some of these strings into the XML. Ah, well, there's the XML. Is there basically for localization. I see. Okay, so, yes. So we don't need to worry about the table names being German. We just need to worry about the things that show up for users being okay. That makes total sense. Yep. Okay, um and then one person asked about if you could go over task Hirscher. They were confused about exactly what the cursor is and how it works. Sure. So, luckily, I'm on that code right now. Um, the cursor is a way to to reiterate over or move through a result set. Ah, that comes back from a query on the database. So that means like, if I say, get all tasks and there's three tasks than I love three rows that come back from the database, the cursor will know how to move through those that results set, um, and let me get access to each one of the rose individually. And the way that works is there's a an A p I that looks kind of like this moved to first, will move me to the first row so I can start off with some data, and then I can query the cursor to get at its current position. Um, basically, data that was in that row as a Java type so I can get, like, long I could get into whatever gets string. Um, and then I query that was positional e. So Ah, I'm getting this the first position or the position zero. I'm expecting that to be a long and then the second position. I'm expecting to be a string and the third position I'm expecting to be a string, and those are the types and getting them out of the right position. And the position is defined up here. When I make my query, I say, here are the columns that I want and they will come back in this same order. So then this corresponds right here. So task I d was the in position one, and I get that out of the first position here. Task name was at position two and I get that out here and so on. So I know that a single row I can get all the information out of a certain row by position, Um, from the cursor and then later on down here in this at the end of the while loop are at the bottom of the while. Loop. When I say move to next. And now it's on the next row and the whole thing happens one more time. And this will keep on happening until I moved to next. Returns false and it returns false. When it's at the end of all of the results that have brought back does that Hopefully that makes sense. It does to me. Hopefully it answered that person's question. So we have a couple of questions from the chat room that Ruth would like to ask. Um, yes, I'm at first. We had a lot of questions regarding the database and accessing the same database from multiple APS. Is this possible? Especially in the mechanism that we've fused now? And a couple of people have asked to go through the database query call once again. Sure, so accessing the database from more than one app. I don't think that's possible because, um, basically that's a file, and the file is is stored in a private sort of sandbox area for each app. I think there may be a way to get access. I don't know. There may be a way to get access if you have, like, a a set of APS that you create and you explicitly grant permission to the file system for But you wouldn't want to do that. I mean, um, although that's not really a problem with concurrent access to the database, because you can only be working with one at a time. Um, I guess you may be doing some stuff in the background in one of the apse with the database. While the user is using the same database from another app, but I don't think that you'd ever want to do that. But, um, what you really want to do what your goal is probably, um, to show to share data between APS and then you didn't You shouldn't worry about the database. Your That's kind of an implementation detail. At that point. What you should do instead is, um, implement what's called a content provider. Um, so look that up on the on the docks and you can look into that further, and then one of your applications will be a a provider of of a certain type of content, and then your your other applications can consume that. And other applications that you didn't create can also consume that you can make that a p I available to other applications. And that's I think what you're actually wanting to do. We're not actually gonna go over a content provider in any of these classes. That's kind of Ah, it's a It's a good thing to look into those. This basically this query is a select statement. So let me go look at select statement one more time and here and the slides. So the select statement form is select the list of calling names from a table name and then their conditions where those are optional. And then there are other things you can say, like group things together based on these criteria, Um, and then having is another as another phrase that you can use to help work with, um, some aggregate functions that you can also perform, like average, like give me the average of these numbers where the average is greater than 10 or something and that you use a having clause for that. And then at the end of all that, you can order things in a certain order. And that's both an order by statement. So I could I could put it all these on here, but these But I know this is in a secret course. I didn't go that far into sequel, but look at this format here. So we've got select from where and then I didn't put it here. But we also have order by and then look at this code. And this fall is the exact sort of format here. So of Line 32 is saying select. Actually, I'm sorry, it's it's transposed. One step here. So the first thing is, from what table? So this is actually the from clause right here, so I could say, like, oops. And then down here with this. Let then there's a bunch of things like, um, where would go here? Um, I'd say where and then I would use question marks for each one of the values that I wanted to put in the wear. And I could have those interpolated from other variables from an array right here. Um, I'm just kind of blowing over that, because we're not getting too far into it. But you can always look at the docks or go to the forums. Um, and then here would be my having cloth over my group by Klaus. Here is a having clause and then last thing is the order. So that right here is order by, um, what? Complete on then? Name. So here's the sequel statement as I've created it, um, in Java instead of sequel. So hopefully that makes sense.

Ratings and Reviews

a Creativelive Student
 

I thought everything on this site was free and now they want 99 dollars for a course

a Creativelive Student
 

very good

Student Work

RELATED ARTICLES

RELATED ARTICLES