Connect To Database

5 minutes
Share the link to this page
Copied
  Completed
Learn how to connect to a database.

Transcript

Hello, in this Python database programming tutorial, we are going to cover connecting to a database, can we set it up? How do we connect? Okay, honestly, is pretty simple. One thing I want to mention is that I'm using x amp for the database connection, and I will be managing it by the PHP, my admin GUI, I covered this in the previous tutorial, they can use exam, potentially for a server back within, you know, an older tutorial than that, just in general, for Python related stuff, I'll actually be using the ID to be actual, you know, run the code, I just prefer that for Python over using, you know, the browser for example, it's just easier, but I'll just be connecting to the my SQL database even though this isn't, you know, necessarily hooked up to it. It is you know, it is as well. So my SQL database long with us started, that's fine and you're all good to go.

The reason that you have Apache web servers remotely thinking is whatever the dominant I don't need it to connect to a database via Python is just so can actually access this interface. If I don't have the web server enabled on active themes, this GUI interface so hard actually know visually see what's happening, but cams accuracy via the Python code. Okay, that's out of the way. Now, let's just start coding. So, first thing we want to do is create a connection to a database, called a DB is the database object that we'll be using the DB equals pi minus QL. And another thing I want to mention is I've seen stories online that had that use PI, my SQL, and a lot of them have it Capital One's like another one work for me It never has.

So I just want to mention that so if you Come across into Taurus flight days all working for you. Let's do it all lower case, or wise Earth is not working the way I'm doing it, try doing it the other way around. If you have any, any issue as usual, reach out and assist. So dotnet for pretty simple. And it takes four parameters. First be the server name, next being the username, password and the database.

So like the actual database you're connecting to. So the server name for my notices I just put nope notes or whatever your server information, put the correct server name there. And I'll show you so it says the root at low close and no closing the server name. root is the user and then the password a document password seconds is a local service. No really first. Obviously if it's online and you know the website or the application is accessible by the public.

Definitely have a secure password on there. But I don't have one other connection to a database called users. It could be whatever you want. I've already got one created, it actually created for a different purpose of God here. So I'll be using this. So now if we go back, what we're going to do is then to cursor like so.

And what this does is prepares a cursor object, which we can use later on with the current method. And we'll show you what that right now. So instead of you know, doing this every single time, it just simplifies it to this, that that's all it is. Huge. So we are going to execute a query. And what we're going to do is just essentially, select the version get the version of our database select version.

Oh, he did see this the way you put like your my SQL query, for example, a lot of select or create or update or insert, that sort of stuff, who we'll be covering that in future videos, so feel free to check them out. So, data now, data equals fetch. One. What we're doing here is we're just fetching a single row. Because you know, there could be multiple pieces of pieces of information like selected from the database, you're on the first row, this is how you do it. Again, we'll cover this in more depth on how to do it over the videos.

And I was gonna print out the database version of the data. And you saw is good practice to close the connection afterwards. Were we in Python or another language like PHP? Save that if I go to Rimando, hopefully we get no errors. Oh, we don't and we got this 10 point 1.22 that Maria dB. So if we go to PHP myadmin as you can see Maria dB, and definitely will protect Maria dB, if you don't know is just a fork of my SQL.

So it is like for my SQL well, so that's it for this video. Thanks for watching. In other database videos, we'll be looking at creating tables and all of that really cool stuff about, you know, interacting with databases. Thanks for watching, and I'll see you soon

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.