Saving the Data Entered with Print on the Console Screen to MS SQL Database - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Monday, May 29, 2023

Saving the Data Entered with Print on the Console Screen to MS SQL Database

In this article, we save the data we entered with the print command on the console screen with the Insert method into the MS Sql Database.

 

 


 

 

 

 

 

 

 

Figure 1

 


 

 

 

Figure 2

 

import pyodbc

con1=pyodbc.connect(

                      'Driver={SQL Server};'

                      'Server=sirius\SQLEXPRESS02;'

                      'Database=master;'

                      'Trusted_Connection=yes;'

                     

                   )

 

 

print("Enter a Staff Id")   

Id=int(input())   

print("Enter First Name")   

Firstname=input() 

print("Enter Last Name")   

LastName=input() 

print("Enter Contact")   

Contact=input() 

print("Enter Mail")   

Mail=input()

print("Enter Gender")   

Gender=input()

print("Enter Department")   

Department=input()

print("Enter Address")   

Address=input()

print("Enter PostalCode")   

PostalCode=input()

print("Enter City")   

City=input()

print("Enter State")   

State=input()

print("Enter Country")   

Country=input()

 

cursor = con1.cursor()   

SQLCommand = ("INSERT INTO dbo.staff(Id,Firstname,LastName,Contact,Mail,Gender,Department,Address,PostalCode,City,State,Country) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)")   

Values = [Id,Firstname,LastName,Contact,Mail,Gender,Department,Address,PostalCode,City,State,Country]  

 

#Processing query.

cursor.execute(SQLCommand,Values)    

 

#Committing any pending transaction to the database.

 

con1.commit()   

   

print("Data Successfully Inserted. \n Verinin basarili bir sekilde girisi yapildi.")

 

con1.close()   

 

 

Hope to see you in the next article. Happy coding.

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...