 Okay, so working with Qt and databases is extremely friendly compared to other platforms. I kind of want to get away from talking about MySQL and MySQL workbench and all these third party technologies because really this is a Qt tutorial and we want to learn how to work with Qt. But we have to understand there's some things going on under the hood. We've got MySQL installed, we've got MySQL workbench installed, we've set up a database called void realms, we've set up a table called users. And in that table we have an ID, a name, and an age. And we have to use what's called SQL or structured query language to actually talk to this database. So you can see we have a select star from database tables and it just returns this record set. So we have an ID, name, age. We can play around with this a little bit and we can say, say I want to change the order of those columns. So I'm going to say ID, age, name. It's now ID, age, name where before these two are flipped around. And let's say I only want to see people that are older than 40. So this is basic SQL that's just structured query language. And this is a very simple query. I mean these things can get vastly complex. When in doubt, check Google. The thing I love, I mean absolutely love about MySQL workbench is that you can just right click on your table, copy to clipboard, or you can actually do send to SQL editor and you can do select, which is basically what we just did. Whenever you see select, you're basically viewing data. You can insert data. You can update existing data. And you can delete and you can do many other things. But it actually creates a query for you. So let's say I want to select all. So I'm just going to copy clipboard, go to a new line here. And that's what it'll give you. You notice how this looks vastly different than this. Basically, whenever you see these quotes, it's just making it a little friendlier on the database system. But it's saying this table, this column. So users ID, users name, users age from database.users. You got to really kind of specify that because this can have multiple databases in it. You see how we have that database? Claps that you can have many databases. So if two databases have the same table, you kind of want to avoid that collision. But they both work in the same manner. Go ahead and close that out. All right, so we've got a query that we're going to work with. Now let's see how we would actually execute this query in Qt. We've got a bare bones basic command line application. It's already got the SQL module installed. And we're just going to go ahead and start grinding through this. I'm just going to paste that query in here. First thing I'm going to do is you guessed it. We need some includes. May not use all of those. But I want to make sure I got them just in case. Right off the bat, we've got Qtabug, Qt SQL, or Qt SQL. Qsql database, Qsql query, plug-in loader, and Qvariant. We've talked about most of these. Database actually talks to the database where the query actually is what's used to talk to the database. Sounds a little confusing, but the database handles the connection that TCP or whatever connection you're using. And the query actually sends, you guessed it, the query back and forth. Don't want to spend a lot of time talking about structured query language. There's some really good tutorials out there. When in doubt, use the copy function out of MySQL that I showed you. All right, so Q info. Unfortunately, because databases are such an expansive topic, we could spend hundreds of hours just talking about databases and queries and how things are set up. So really, this kind of comes with a disclaimer that we're expecting you kind of know what you're looking for. Or you at least know how to kind of screw your way around through MySQL workbench and figure it out. Don't want to kind of just leave you twisting in the wind. Honestly, if you get stuck, just go out to the Voidrums Facebook group. We'd definitely help you with just about any query you want. Some of these can get really crazy. All right, so we're going to add a database. Now it's a little bit misleading. We're not actually adding another database to MySQL. We're adding a connection. All right, so we're saying MySQL, and we're just giving that connection a name. So this is what's called the default connection. So whenever we say db.connect, we're going to use this connection. We want to set the host name. We're just going to say localhost. And in the networking world, this is the same thing as our local loopback. And that's a little scary because localhost, if somebody was tampering with your computer, that actually could send you to a different address. But by and large, localhost translates to 127001 in the networking world or your local loopback, meaning your computer. And then we're going to set username. This is the user that we set up previously. In case you're wondering where I'm pulling that from, you go into MySQL Workbench, Users and Privileges. And it's just this user. We've given this user dba roles, which you wouldn't do in the real world, but for testing purposes, we've got that in there. Flip back here. You wouldn't really hard code the username and password in the application in the real world. You'd put this in some sort of configuration file or in queue settings. One caveat is you really should encrypt that password. So we're going to say, if we cannot open the database, then queue info, want to know, hey, couldn't do it. And we want to know why. Let's get the text representation of that last error. So this is going to be a pretty simple little program to follow. I realized that databases itself is kind of a complex topic. So I want this to be pretty easy to follow along with. So the structure is going to be pretty rudimentary. There we go. So we have, if we cannot open the database, do something otherwise, do something else. And we're just going to say, queue info, do stuff. Actually, I should probably say a little more professional here, connected. So we're connected. We're going to make a query. And this is where we're going to actually use this query that we generated in the MySQL Workbench. So I'm just going to copy that real quick. And we're going to say, queue SQL query. Remember the database handles the connection to the database, where the query is the actual question you're asking the database. So right here, we're just going to say, make a queue string, paste that in there. We know that this query works because we got it right out of MySQL Workbench. And we've actually tested this query to make sure it works. And one special note, we have actually ordered the columns ID, age, name. Take special note of that. We're going to talk about that in just a second. So once we have this, we need to actually execute that query. Meaning we want to say, hey, database, I have a question for you. And we're going to say, exec, and we want to exec. There we go. If we could not do that, we want to know why. And let's just grab this. I'll help if I put that in front of it. That would have been an error. That would have been a bad error. There we go. So we've got that last error. And we want the text representation of that error. Drives me nuts that Qt Creator does that. I really need to go in and change that setting. All right, so if, however, we are able to query, then we want to work with that query. We want to know, hey, we got a query. And we want to know how many results we got back. So we're going to get the size of the query. And you might be inclined to just say something like Qt Info Query. And we want to get the value at zero and just see what you're getting. You're going to run into some disastrous results though. So let's go ahead and run this. See what happens. And it says, Qsql query value not positioned on a valid record. So this is positioning. And so basically, you're getting a result set back from the database. And you need to kind of go through that result set and read them one at a time. And we're just going to say, while, we'll say query.next. So while we're able to iterate through that, we're going to actually get the records. And this is where that positioning is very, very critical. And when I say positioning, I'm talking about the position of the columns in our query. Notice how it's id age name, not name id age. So let's go here. We'll say id and then we want the query. We want the value at, and that's the actual position. Notice that's just an index. So under the hood, that's really just an array. And we want the to int. So we're going to just cast that over to int. And then we're going to say int. We want age equal queries value. And then we want the first position because we're moving forward in the array. And of course, to int. And let's go ahead and get the name. So we want the Q string representation. And we're going to do the to string. So really under the hood, what we're doing is we're just working with the Q variant type. So that's how we're able to do the to int and to string here. So now we've got the actual values. Let's go ahead and print those out on the screen here. Let's just throw this id out here. So we're just going to print out on the screen the id, the name and the age. Of course, once we've gone through this whole thing, we want to actually just close this down. So let's go in here. And let's say db close because we want to close that open resource. Qt will do it automatically for us after the program's done. But just want to make sure we're closing that connection. So it's not just sitting there. Save and run. And let's see what this looks like. And you can see, OK, opening database connected. Let's make a query. We got two results. Id1 is Brian, who's 44. Id2 is Tammy, who's 49. And then we've disconnected. Notice how we're missing Heather. We're missing Heather because of this where statement. If you already know SQL, you're just rolling your eyes because you could just simply do this. And there's Heather, who's 25. Let's just face it. Qt is extremely complex and has a massive learning curve. But once you learn it, you can do just about anything. Unfortunately, learning Qt is a challenge in itself. And if you've tried learning straight from the docs, you've probably become easily frustrated. While they do a really good job, they're arguably some of the best documentation in the world. They don't go that extra step and leave a lot of people guessing what to do. How do all these things interconnect? That's why I started developing videos. I've done videos not just on my own YouTube channel, which you're watching now, but also on the official Qt Studios channel. And I've started doing video courses out on udemy.com. Right now, I have the Qt Core series. It covers beginners intermediate advanced. So it'll take you straight from Hello World all the way up to building a complex, multi-threaded, encrypted TCP server. On top of that, if you don't want any of this, you can still join the Voidrom's Facebook group, which has a pretty flourishing group of developers. And we discuss everything, not just Qt. I hope to see you there.