Skip to main content

Adding new tasks to the database

Lesson 19 from: Developing Android Apps with Java

Tony Hillerson

Adding new tasks to the database

Lesson 19 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

19. Adding new tasks to the database

Next Lesson: Completing a task

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

Adding new tasks to the database

next step is adding tasks into the database. So we're already adding tasks from this ad task view into a well, we're actually adding them through the application, which is, um, which is good because then we all the changes that will be making at this point are in the application, the application class. So when we had a task now we don't just put it into the array. We need to also put it into the database, so we'll be doing that next. So let's look at another sequel statement. This is the insert statement. An insert statement follows this form insert into table name and then you put a list of columns and then you say values, which is a key word. And then you put the list of values at the end. After that, you could also use a where clause here. No, you can use where clauses making sense. Okay, so this is pretty much it. This is when an insert statements Looks like. So, for instance, insert into tasks. Name complete values get milk and false will make a new row in the task stable. Um, whe...

re the name will be get get milk and the complete will be false. And the I d will once again be auto implemented. Um, and automatically will be, um, the next number in that auto increment list. A number that's not already in the in the database table in secret light will take care of that for us. So we're gonna be using an insert statement and in the ad tasks step, um, that we're gonna be doing this, we're gonna be doing these steps. We're gonna change. Um, add task. I think we already implemented at task anything on the application, but we're gonna change it to to insert into the database. Um, the task that gets passed in. So let's get to you the application and have a look. So here is add tasks. I'm sorry. Add task. I already have that implemented. This is what is called by the ad task activity. When we click, what does it create? How did I say that? And task. Ah, look at task when that task is clicked, we finished this activity, but in the background there were calling at task. And this right now we just put ah the task into and here I get this boilerplate I think that I got rid of in the first. First. Ah, first class. I don't think that's actually needed. So in any case, we're We're gonna put this task into this a realist, and we still want to do that because we don't wanna There's no need to go through the whole step of of, ah, getting the data back out of the database when we already know what the data is. So we're just gonna We're still going to do what's online 66 which is put the task into the the task of race, and then the application will work. Continue to work as it did before. But before we do that, we're going to on a paste in some code here, we're gonna put the data into the database. So here is the code that will do that. Now, let's look at this. It's go through that. Give us a little formatting to put this in the middle of the screen. Okay, so here's the code that I just paste it in. So the first thing is, we're gonna create the an instance of this class called content values. So let me import that. So android or Java knows what we're talking about that is android content on content, values, content values is a way of referring. Teoh is an object that lets you refer to It's sort of like a heat a key value pair, but it's a little bit smarter underneath because it knows how to deal with. Um, well, I enter it knows how to deal with it, especially in the case of the database, to, ah, to getting the information back out of there, um, and put into a the, uh the answer query are pulled out of here inside of answer, query and put into the database. So it's kind of android special little thing there, but you can think of it just like a hash map, which is a key value pair type of a set up. So basically what we're doing is creating content values, and we're saying that the name is the task name. Whichever name we're putting in and the complete is the string value of the completeness of the task and notice that we're using the same, um, constants here. Task name and test complete to ah refer to the key portion and content values because that's going to be the column name. It's gonna go straight into the column name in the database. So this is like, this is like getting the data together in a hash map that you want to put into the database, keyed by the columns that you want to put them into. Then the next thing you do, you know, to make this even more explicit, let me. Ah, let me break this out like this. Cut out that statement, which was just sort of a shorthand way of doing it and make a a local variable Long I d equals database insert. Okay, so what's going on here is just focus on this portion. For right now, Data based out in certain is going to create an insert statement and then execute that database or execute that query against the database. Um, and the first argument here is, what table do you want to work on? The second argument. If I pop over the help again, hit F two on make this big enough to see Zoom in here a little bit. The, uh um, Here's the Here's the fun. Ah, Java Doc for this method, first argument is the table in the 2nd 1 Is this weird? No. Column hack, which I don't completely understand. But here's the description. Sequel doesn't allow inserting a completely empty row. So if initial values is empty, uh, which is, um I think what comes from this content values here, this column will explicitly be assigned and no value. Um, okay, so that's the value coming out of content values. I don't know. This is some sort of weird thing about that. Ah, that Java where? I mean that the android sdk has to use if you want to make something. Um, no, you can you can put in a string here of, uh see, I think this is the name of the column that you want to be. No. If the value of the content values is no. So it's maybe a list of columns. I don't know any way this this isn't very clear, but basically for your purposes, I think it was leaving. This knoll should be fine as long as you provide some sort of initial value inside the content, values and the content values goes in the third position here. This map, which is the ah, the map of columns to values, and then this will be inserted as a row into the database. And then the other important part is that the return value of this is the I d ah, that that secret light generated for that row. So that's what I'm doing here is I'm getting reference to that I d. And then the task now is gonna have that I d. And that's important because that task object that's local. If we want to update that later in this same session, we need to refer to it by I d So bad. Task needs to know what? It's ideas. So that's what I'm I'm setting the I d on the task after I do all that inserting into the database. So next up here is, um, but the task into the current tasks, and then we're off to the races. We should be We should have a task inserted into the database and shown in the air a list of all the tasks that we have inside the application. So let me run this and then our check our test is just gonna be that it doesn't break for now. Um, but that should be good enough to show that this is working. So at tasks will say get No, that's our standard. Hello world type. Get task. Add task. Now it's in the database. What I could do. Although I'm really scared too because of my problems with the emulators, I could close the emulator now. Um, although I won't. And then we start the emulator, and this task should still be there. So, um what? I guess I could do, though, just to see if it's still there is, um, exit out here to the desktop and hit the menu, go here to settings and go force closed the application, and then when it starts up again, then we consume We well know that that ah wasn't in memory that, um, that data wasn't memory. It'll be loaded out of the database. Sofa hit applications go to manage applications. Here. Go task manager. Click on that guy. Ah, force stop. That will kill the application and hopefully give me some indication that it actually did it. Uh, no, it doesn't giving medication. All right, so thanks for nothing, android, but I can come back here and was that you hold down the home key. I think you can get a task. A churn up. Looks like it's still running. Oh, no. Well s Oh, I don't know. It kind of took some time to start up there. I wonder, Does anybody else have an android phone? And they've done this, and they I kind of feel confident that we actually killed the application that time? Because I'm not No responses from our in Seattle audience. Okay, let me try this one more time. I've got kill it from the D D M s tab. Kill it from the DDM esta. That's thinking very good. Where you been taking this class? You know more than I do. Okay, so here's D d M s. Thanks, Tim. VMS again? We've looked at this perspective before. This shows us the running emulator or or if we are connected to a device, it'll show us the the environment running on that device. Um, here and said this the devices tat we could see that this is device, um, that were connected to its an emulator. And one of the processes running identified by the, uh, the package name, which you remember I said, is the unique identifier for a application is common. Riley Android, task manager. So that's the guy that is our application, and what I can do here is Click Stop, and now it's definitely gone. So if I come back to the emulator task manager, um, doesn't say anything interesting about that. But if I hold down the home key, head still shows up in here. I don't know. I don't know what that means. So I guess that this is a list of recently ren applications or something. I don't know. That's kind of confusing anyway. If I click it now, it looks like it's it's kind of starting up. So it wasn't running before, and now here it is back again. Well, here, let me try one more time. We'll check the this process I d 3 59 So if I kill that it's gone. The home button is apparently recently run APS not running APS. Okay, Useful? Useful to know, I thought it was like a running APS. So when I hold down the home button, go to recently run APS, that's interesting that it's in a different position here. Maybe that's just I've used it more now When I click task manager, it'll start up and it's not 3 59 anymore for the process ideas through 68. So we know it's a different running instance and get milk is there. So we know that the task list didn't come out of memory, came out of the database. So we were successful. Good. And thanks for everybody. Self on the android phone stuff. I don't actually have an android phone. Is that surprising? Yes, it is. Have a phone from a different Californian company. Oh, really way Think we think that your work should buy you an android? Phones? We haven't. We haven't laying around, but I just just seven use it. My daily knife life enough to, uh, Teoh have run into her to know that about that. That's that's good to know. I'm learning where everybody's learning today. That's that's why we have this so I can learn from you and you can learn from me. Someone in the chat room says boo Uh, how the data is persisted across restarts of the emulator. There are a couple of questions regarding that. Okay, well, it's written into the database file, so the database file is a file stored on the file system. And that's, however, sequel Light chooses the story to data it's inside of a file. So that file is there between runs of the application. And then when you start up that secret, wide open helper, remember we passed in the file name as one of the arguments. So we switched back to Java secretly Open Helper and the constructor. One of the arguments to super is the name of the database, and we passed tasks underscore TV That's equal light and that will be created into a file on the file system and that will persist between runs of the application. Cool. So we actually had a couple questions about that file name since you got it highlighted. I thought I would ask. Now, um, does it have to be? Doesn't have to have dot sequel light at the end? Or can you put anything there? It doesn't have fanciful light at the end. In fact, I think the convention is since we're I think the version is sick, like three of the convention is usually secret, like three, but it really doesn't matter. It's just a file file names or not as important as as Windows made them out to be on. Other applications are out on other operating systems. Okay? And we have a bunch of people asking if you have multiple APS say you had to task managers, maybe cult task Manager one in task manager, too. If you used the same name for the database, would they be sharing and database? Do they get a separate one? And can maps share databases so already addressed the collapse share database question a little bit, but just quickly, Um, no, I don't think that you can share the file. What would be better to do is a content provider. So if you look into a contract provider on on the docks, that's probably what you want to do instead and then have one of your APS actors a content provider. But the question about whether or not you can have to task manager applications is no, you can't, and that's because every application I'm opening up the manifest now, Android manifest. Every application that you install has to have once a switch with XML here, well, here on to stay in this. In this view, every application has to have a unique package name, so you can't install another application with the same package Name. And every unique application has its own, um, sandbox area of the file system that other applications can't access unless you go through some sort of sharing process, I think. But that's not that's not the right way to do what you want to do. If you want to share data between applications, the right way would be to do a content provider. Um, we have some comments that different APS can access each others files if they were signed by the same developer key. Okay, so that's that's the way I was gonna look into this afterwards. But, um, if we want to talk about this more in the forms can but yes, so there is a way to share the same file system, I guess. But again, you don't want toe share the same database file because especially if you're gonna be doing some work on the database in the background in one of those APS, you could run into concurrency issues there, which means like I'm inserting something into the database at the same time, another app is, and maybe it's the same data. Um, and it shouldn't be done that way. There should be one gatekeeper to the data that goes into their because you're not sure what the other applications doing. So yeah, like, say, I get a list of stuff out of the database. Here's a good example. I get a list of tasks out of the database, and then another application accesses the same database and to lead some of those tasks out from under me. And then when I go to interact with them in this other application that got the list in the first place, then they're not there. And that's kind of a surprise. So concurrency issues like that content providers the way to fix that kind of stuff. Okay, um and can you jump back to the code? We have some people who are asking about exception handling, and they're wondering if we should be adding exception handling or if their ways that it's being sort of handled internally. So if, for instance, the query returns something that is unexpected, um, well, you work in control of the data that's going into the database, so hopefully nothing gets in there. That's unexpected. Um that would be were it would be. I'd rather use some sort of a defensive coding type of Ah, such of, Ah, set up there to make sure that only data that you want is getting into the database. Um, but, I mean, this is if if values that you didn't expect or coming out here, um then you would probably get a runtime error elect if you if you are switched up a C like, for instance, when you're loading the tasks out of the database of you switched up the column names and figure that you're getting I d ah. When Instead you're getting name. Um, then you would have ah, casting or a type type here and job, and you get a runtime error, and then the app would blow up. So I guess you know you could weaken. I'm not sure what Ah, if any exceptions that the database query stuff throws, but they seem to all be run times. Otherwise, Eclipse would make me, um, it wouldn't compile. I would have to, you know, catch a couple exceptions. Okay? And we have a question from the audience. Ah, wondering if it's possible to control where the sequel databases stored. If you have to store it on the, um, on the phones rahm or if it can be stored on the S d card? Um, I don't know, that's a good question, but my guess is that sense you Well, so you don't have to use the sequel that open helper for one, because when you do it pretty much takes care of the storage. All you all you get to do is pass in ah, name. And I guess you know Well, let's look at the help. I don't know if that could be a path you're gonna pop up in the Java docks here. I don't know if that could be a path to something when they say the name to see if it says anything about that here. So name of the database file or no for an in memory database. So it doesn't say anything about being able to pass a path for the name. But again, there's nothing that says you have to use a secret open helper. You can, um, go and use the sequel light database. Um, I'm switching over the application here. Segal, I database is what you get out of the helper. It just manages the versions for you. So you could create a secret light database. Instance that pointed at whatever location you wanted. So, yes, you could store it on another SD card or something. All you need to do to get a reference there, probably. You probably just need to pass a file handle to this guy. Okay, we have another question from the audience. He's grabbing the mic. Of course, accessing that s D card requires something in the manifest to tell the the phone that it's gonna be doing that. So it's a good point if people want to share files between APS by all writing them on the card somewhere that they all know about, they'd have to have get permission from the user to do that in all APS cases, which might be a little bit quirky. So it's a good point. So I mean, if you want a ship, well, I mean, like, maybe like, you want to ship a database file that a nap lic ation could use or something like that. It's just a file on the on the file system. So ever the file is you can get access to it after the fact. So for that answers that question. And then I just have one more question that was ready to the code in this segment. Before we go on, if you can, can you scroll down a little bit? It was related to the I D. That is being returned. Um, and they were. They were asking if it's the row I D or the primary key idea If it's the same thing. Yeah, there's no such thing as a row idea. It's the I D column. Um, you know what? Let me Ah, because I never defined what that was. I guess it is just the primary key. The one of the the one that had to find is the primary key. Yeah, that's it. Sorry. I think that three really quickly, But But since I defined in the open Helper that the I. D column is the primary key, that's the one that secretly returns from that from any insert statement so that that can be managed right away without doing another query, which would be weird to try to figure out, you know, if I had the same name in there because I allow the same names. And I didn't say that there's a restriction there. I have to figure out which was the last one that was inserted with that name and then figure out what the I d was so secret Light as a convenience. Whereas not all databases do this, um, it will return the I d. From insert statements so that you can They're the primary key. Sorry. The primary key from the every insert statements so that you can work with that object with this unique identify, as you're expected to do right away.

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