How To Connect To SQL Server In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going guys in this video i'm going to show you how to connect to microsoft sql server using python all right so this is a question i get pretty often asking how do we connect to microsoft sql server using python all right so here i already have my vs code open as well as my macro cell sql server management studio all right so first we need to figure out the uh server name from microsoft sql server database in my sql server management studio i'm going to type select to add symbols server name then run this statement to get the server name to my database then i'm going into my vs code or my code editor then i'm going to import the pypy odbs library and this is the library used to work with the odbc connection manager and you may see a different library called pyodbc essentially those two libraries are almost identical except that some of the usage a little bit different and i'm going to name the module odbc and you can install the library using the command piping store p y p y odbc next we need to create the driver name and the server name as well as the database name so for the driver name it's going to be sql server and for the server name it's going to be the server name that we retrieve from this server name statement so i'm going to copy my server name and assign the name to server name vehicle they will need to figure out which database want to access in my database i have several databases and i want to connect to my jj database so i'll create another variable called database actually let's do database name and this is going to be jj now we need to create a connection string to pass to the odbc py priority bc library to set up the connection and let's name the variable connection string i'm going to make the f string and wrap the string or the connection string with uh double quotations all right so the first part you need to provide is the driver and i'm going to insert three sets of curly brackets then i'm going to pass the driver name variable let me should i insert the semicolon at the end the next property is going to be the server i'll just going to be the server name and here i'm going to insert one set of curly bracket then i'm going to insert the driver name vehicle i'm going to insert a semicolon at the end and the last item is going to be the database name and the property name is going to be database and again i'm going to insert one set of click bracket i'm going to insert the variable name and click bracket again and this step is pretty important we need to make sure that we set the trust connection value to true actually not true to yes if we log in with a username and a password then we will need to add two more properties one is the uid property and it's going to be your user name and for the password is going to be pwd and because for my login i don't need to provide the password in the username so i'm going to leave those two on the top and i'll come out these two lines and once i created my connection string i can now connect to my sql server database by referencing the odbc module the connect and i'll pass the connection string and i'll name the output connection c-o-n i'll print the connection string all right so i'm going to uh press f5 to run the script and here's my connection string object output all right so this is going to be everything i'm going to show you in this video and hopefully you guys found this video useful and as always see you guys watching i'll see you guys in the next video
Info
Channel: Jie Jenn
Views: 140,756
Rating: undefined out of 5
Keywords: sql server, python sql server, python connect to sql server
Id: g69lFxZdcVQ
Channel Id: undefined
Length: 4min 41sec (281 seconds)
Published: Sun Feb 06 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.