Skip to content

Commit

Permalink
Added python using SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
robmollerau committed Sep 13, 2021
1 parent 698ed4a commit c1a01a9
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions sqlBasicSelect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#
# This script uses pyodbc ODBC driver available from Microsoft at:
# https://docs.microsoft.com/en-us/sql/connect/sql-connection-libraries?view=sql-server-ver15#anchor-20-drivers-relational-access
#
# You can also install from the command prompt by typing: pip3 install pyodbc

import pyodbc

# Connection to server via ODBC
# Python cannot pass by reference, hence return value is a tuple containing Success and connection handle
def ConnectToServer( p_sqlConnection : type ):

sqlLogin : str = ''
sqlPassword : str = ''
sqlServer : str = 'localhost\\sqlserver'
sqlDatabase : str = 'test'

# Get SQL login name
sqlLogin = input( 'Login: ' )

# Get SQL password
sqlPassword = input( 'Password: ' )

try:
p_sqlConnection = pyodbc.connect( 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + \
sqlServer + ';DATABASE=' + sqlDatabase+';UID=' + sqlLogin + ';PWD=' + sqlPassword )
#sqlCursor = p_sqlConnection.cursor()

except:
print( 'Error: Could not connect to ' + sqlServer )
return( False, p_sqlConnection )

return( True, p_sqlConnection )

# Select customer table
def selectCustomers( p_sqlConnection : type ) -> bool:

sqlQuery = 'select * from test.dbo.tb_customer'

try:

sqlCursor = p_sqlConnection.cursor()
sqlCursor.execute( sqlQuery )

for row in sqlCursor.fetchall():
print( row )

except:

print( 'Error: Could not retrieve custommers' )
return( False )

return( True )

# Starting point for standalone apps
if __name__ == '__main__' :

sqlConnection : type = None

# Connect to server - result is a tuple passing back connection reference
# as Python always passed parameters by value
resultTuple = ConnectToServer( sqlConnection )

# First element is result, second is connection handle
if resultTuple[ 0 ]:
print( 'Connected to database' )

# Select some data from a table
if selectCustomers( resultTuple[ 1 ] ):
print( 'Customers selected' )




0 comments on commit c1a01a9

Please sign in to comment.