Connecting Python To SQL Server, For Inserting Record

Before starting my article about connecting SQL Server to Python, I would suggest that new learners check the fundamental concept of Python from Python web site or any other websites suggested on Google. We can connect Python with various kinds of databases, including MySQL,SQL Server,Oracle,and Sybase etc.

So, in this tutorial, I will explain how we are able to connect with SQL Server. To connect with any data base, we mainly follow 4 steps.
  1. Importing the API module (similar to Namespace used in .Net).
  2. Establishing Connection with database using connect constructor.

    connect ( parameters... )

    It takes a number of parameters like-Driver, Server, Database, Uid, Pwd

    Driver- It mainly depends on the database you are using. According to the database, this driver will change.
    Server- This is the name of the server where the database exists.
    Database- Name of the database.
    Uid- This is the userId for your database.
    Pwd- This is the password for your database.

  3. Passing your Query in a command object.

  4. Creating a Cursor for executing the Query.
  5. Closing the Connection (optional).

Now, open Visual Studio. Create a New Project, and then go to Python Template as follows.

 

Now, go to Python Environment.



Check for Pipiodbc in Python Environment, as the following:



Install it or update it.

 

Now, open the Python application and write the following code.

  1. #importing module Like Namespace in .Net   
  2. import pypyodbc    
  3. #creating connection Object which will contain SQL Server Connection    
  4. connection = pypyodbc.connect('Driver={SQL Server};Server=Debendra;Database=CodeX;uid=sa;pwd=123')   
  5.     
  6. print("Connected.")    
  7.     
  8. #closing connection    
  9. connection.close()    
Now, I have the codex database.Here, I have tbl_employee.

 

Now, I will write the code to input the data from user and save the data in the database, as follows.
  1. #importing module    
  2.   
  3. import pypyodbc    
  4. connection = pypyodbc.connect('Driver={SQL Server};Server=Debendra;Database=CodeX;uid=sa;pwd=123')    
  5. print("Enter a Unique Id")    
  6. Id=int(input())    
  7. print("Enter first Name")    
  8. firstname=input()  
  9. print("Enter last Name")    
  10. LastName=input()  
  11. print("Enter Regno")    
  12. RegisterNo=int(input())   
  13. cursor = connection.cursor()    
  14. SQLCommand = ("INSERT INTO tbl_employee(id,firstName, LastName, EmployeeId) VALUES (?,?,?,?)")    
  15. Values = [Id,firstname,LastName,RegisterNo]   
  16. #Processing Query    
  17. cursor.execute(SQLCommand,Values)     
  18. #Commiting any pending transaction to the database.    
  19. connection.commit()    
  20. #closing connection    
  21. print("Data Successfully Inserted")   
  22. connection.close()    
Now, Run the application and input the data.

 

Press Enter to save the data in database.

 

Go to the database and check for the Record.

 

Thus, in this way, we can connect Python to the SQL Server. Hope this tutorial will help all those who are new to Python and trying to learn it.

Up Next
    Ebook Download
    View all
    Learn
    View all