 Hey everybody, it's Brian, and welcome to the 51st Qt tutorial. Today we're going to go down a different path here, and we're going to actually start with database programming. If you're not familiar with what a database is, you can go out and download Microsoft SQL Server Management Studio Express, along with SQL Express. That's if you're on Windows, otherwise you can download a whole slew of other free database systems. Just trying to keep it simple, I know everybody out there has got their own favorite database, but really all you need to know is a database is a file that holds information. And as you can see, we have our database server right here, localhost slash sql express. And in there we have a database called test, and in test we have a table called people. And if we open up people, you can see that we have a identity column, which just identifies the row. And we have a couple columns in here, ID, first name, last name. We just have Bob Hope and Will Smith at our little table called people. So that's kind of the crash course in databases. It's just a file that holds information. I'm not going to really cover a whole lot of SQL commands, because that's a tutorial in itself if you know anything about SQL. Really what I'm going to focus on is Qt and how to interface with a database using Qt. So now that you know what our database is and what it looks like, let's jump right on in here. Go file, new, Qt application. And we'll say dbapp, put it in the usual location. All right, now the first thing you need to do is open up your project file, and you need to add a reference to SQL. That way Qt knows you're going to be doing database programming. Save your project file, it'll rescan it, and see there it goes, scanning. And then we can start programming. So first thing we want to do here is make a few includes. So we'll say include, Qt SQL, and let's include Qt bug. Now the first thing you need to do when you're working with a database is actually make a connection to it. And that's what we're going to be covering in this tutorial. I will be doing a little bit of copying, pasting, because there's a lot of typing, and I want to go through this and explain everything rather than sit here and watch me do some typing. So Q string if I can spell it. And what I'm just going to do is make a few variables here, and this is going to be our host name that we're connecting to. Notice how I have an instance. This is the host name, and this is the instance, SQL express. Note the double slashes because if you just do a single slash, well, we all know what happens in C++. All right. And now I want to make the name of the database. And if you remember, our database is named test. And now we want to set the driver that Qt's going to use. By default, Qt has what's called a default connection. Get it by default. Qt has a default connection. Sorry, bad humor. Qt has a default connection, and that is what's used to connect to the database. You can use multiple connections, but we're not going to cover that in this tutorial. It gets a little involved. Just be aware that there is a default connection out there if I can type. In case you're wondering, no, I'm still not wearing my glasses, so my typing is a little atrocious here. And we just need to add a database driver. Q-O-D-B-C. That's kind of like the universal driver. I forgot an equal sign here, didn't I? Sorry about that. Now that we've got our database driver set for the default connection, what we need to do is update the options. So we say set connection options. And this is where I'm going to do a little copy and paste magic because it's going to be a lot of typing. And this is where I'm making my connection string. You can use a DSN, which we're not going to cover, but I'm not going to use a DNS. I'm going to use a connection string, and this is how you do it in Qt. You make a Qt string and you would fill it with that connection string. And we're just saying it's a trusted connection and we're giving it the arguments of server name and DB name. So when this prints out, it'll say server equals and then %1 will be converted to our server name, which is localhost slash SQL express. And database equal %2 is going to be converted to database equals and our database name is test. Now above I have a line commented out here that shows you what it would look like if you had a username and password, where it says UID equal SA or whatever your user account is and PWD equal and whatever your password is. Your connection string is typically the trickiest thing with working with a database connection. They read the documentation that came with your database and sometimes they have little utilities. For example, Microsoft SQL Server Management Studio Express, where you can actually extract the connection string and just copy and paste it in here. So little outside the scope of our tutorial, which is why I didn't want to spend a whole lot of time typing. And then we'll say DB set database name and we want to just set it to DSN, which is the string that we just made with our connection string in it. Now we want to actually open the connection here. So we'll say if DB open and we can just give it no parameters here. And here we want to say Qtabug opened and because we don't want to leave any dangling connections here, we should close the connection once we are done. Now let's just say something horribly went wrong here. We misspelled the server name, the connection string was wrong, something bad happened. Well, we want to know what happened and we want to be able to find out what happened. So we'll say Qtabug and we'll just say error equal and we get the DB last error and get the text of that error. That way we can figure out really what happened and what's going on here. The critical components here, you've got to have a server name and you've got to have some means of connecting to it, whether it's a DSN or a connection string. Typically with some database systems like SQL, you can have multiple databases per server and you can jump around from one database to another. So let's just save and run this and see what happens here. Server name is not declared in the scope. What did I do wrong here? Well, in the bet, I have a misspelled variable. Yes, who would have thought? DB name also from the looks of it. Sorry about that, folks. Compile and run. And sure enough it says opened. So it connected to our database, opened the connection and we didn't really do anything in this tutorial. We just instantly closed the connection. Now what I'm going to do here is just show you what happens if something goes wrong. We'll say vLocalhost. There's no such thing as vLocalhost. In case you're wondering what localhost is, I'm not sure if it's all systems, so I'm just going to say on Microsoft Systems, localhost translates to, and I'll actually put it in the comments here, 127.0.0.1, which is your local loopback interface, which just means your computer. That's all that means. It's the IP address of the local loopback. But we're going to do vLocalhost and there's no such thing. So this is going to generate an error. I just want to show you what happens if something bad happens and we can get the DB last error. So let's save and run. You notice how it's still thinking. It's going to sit here for a few seconds and eventually it'll time out and spit out this horrendously looking awful error message that means the sky is falling and the world as we know it is coming to an end. So let's just sit here for a second. There we go. There's the actual text of the error. So whenever you're working with a database, you're going to use the database get last error almost every time you want to do something major. The other thing you should note is if you forget to add SQL to your project, that you will have nothing but errors when you go to compile. So if you're starting a database project and you go to compile it and it just whips out all these bizarre errors about, you know, link not found, symbol missing, et cetera, et cetera, make sure you get SQL in your project file. All right. Well, that does it for this tutorial. This is Brian. Thank you for watching. I hope you found this educational and entertaining.