 Hey everybody, this is Brian and welcome to the 52nd Qt tutorial with C++ and QA programming. We're going to continue our little journey here into the world of database programming and to save time. I have kind of cleaned up the code from the last tutorial. Real quickly I'll just walk you through it. We just make a server name and a database name and then we add the database driver. I'm using QODBC, set the connections and then we set a string, which is our connection string to our database here. And then we set the database name to the connection string for the Qt default database. And then we're simply saying if db.open, in other words if we open the connection, we're just going to print out open otherwise we're going to print out an error message. And we instantly close the connection. And just to prove this whole thing works, let's just run it and open. So if you haven't watched the 50th video, I recommend you go out there and watch that. But if you already know all this, feel free to keep watching. So today we're going to cover how to make a query. And it's actually very simple. I was very surprised when I was learning how to do this in Qt. How easy it was. Some languages just make it like ice skating uphill. It doesn't need to be that hard. So let's just dive right in here. Oops, SQL query. And we'll call this Qry. And if you're wondering what a query is, it is when you go into your database and you want to extract information out of here. So we'll just say script table and select. And that's what a SQL query looks like. Select and then your fields from the table. And you can put different things like where id equals a certain number. But if you execute this, this is what it returns. It returns the information from the database. If you don't know SQL, I'm not going to teach it in this tutorial. I may in later tutorials, but I would recommend you go out and just Google Beginner's Guide to SQL. Or you're going to be horrendously frustrated at a loss trying to figure this out. All right, so let's just jump right back in here. And we'll say if query.exec. And this is where you would give it your query string. And we would say else. And let's just throw a few key to bugs in here for good measure. That way we know what's going on with the program. So closing. And let's just get the error text here. And if you remember from our previous tutorial, this is how you get the error message out, the db.last error. And db is our database object. All right, so now we need to get our query string. And fortunately, we can just copy and paste this right out of here. And I'm just going to say select star. Just to save some screen real estate here. It does the exact same thing. Now that we've executed our query, what we need to do is get the records out of there. So we will say while qry next. And what this will do is advance at a record. It starts off without a record. And it says query.next means move to the first record if there is one. Otherwise it just says there's no records. And what we're going to do here is we are going to print out the first name. So let's just very simply do a qdebug query value. And then we want the field. And you notice from our field list, let's jump back here, it's zero base. So id would be zero. First name would be one. Last name would be two. So we want the number one column, which is actually the second one. So it's first name. I know that's confusing. It's just zero base like any other array you're used to. So we'll say one, two string. And we're just going to print out the first name of the people in the database here. So let's save and run. And if all goes well, sure enough, open, bob, will, and closing. So that is the bare basics guide to how to do a query. As you expected, it gets a little more advanced. And we're going to be covering those in later tutorials. But I just really wanted to slowly ease you into database programming with Qt. Because database programming in itself gets a little complex. And working with two new things at once may be a little much for some people. I know for me it's just an uphill battle. But if you're comfortable with SQL, and you're fairly familiar with Qt, and you've been following along with tutorials, it should be fairly easy. So let's recap here before we close this out. We are just setting our variables, setting the connection options, making connection string. And then we're setting the database name to the connection string. We're saying if db.open, then we're going to create a query. And we're going to execute the query with our query string, which we're just selecting star from the people table. And then we're going to say while query.next, meaning while it has a next record, just go through them all and spit them out. We're saying query.value and the field number to string. Pretty simple. Well, this is Brian. Thank you for watching. I hope you found this video educational and entertaining. And we'll be covering this more in depth in later tutorials, but wanted to take it slow at the beginning.