diff --git a/samples/connect/python/linux/README.md b/samples/connect/python/pymssql/linux/README.md similarity index 100% rename from samples/connect/python/linux/README.md rename to samples/connect/python/pymssql/linux/README.md diff --git a/samples/connect/python/linux/sample_python_linux.py b/samples/connect/python/pymssql/linux/sample_python_linux.py similarity index 100% rename from samples/connect/python/linux/sample_python_linux.py rename to samples/connect/python/pymssql/linux/sample_python_linux.py diff --git a/samples/connect/python/mac/README.md b/samples/connect/python/pymssql/mac/README.md similarity index 100% rename from samples/connect/python/mac/README.md rename to samples/connect/python/pymssql/mac/README.md diff --git a/samples/connect/python/mac/sample_python_mac.py b/samples/connect/python/pymssql/mac/sample_python_mac.py similarity index 100% rename from samples/connect/python/mac/sample_python_mac.py rename to samples/connect/python/pymssql/mac/sample_python_mac.py diff --git a/samples/connect/python/pyodbc/linux/README.md b/samples/connect/python/pyodbc/linux/README.md new file mode 100644 index 00000000..616ee5ad --- /dev/null +++ b/samples/connect/python/pyodbc/linux/README.md @@ -0,0 +1,25 @@ +# Connect to SQL Database by using Python on Ubuntu Linux using pyodbc + +[Python code sample] (sample_python_linux.py) that runs on an Ubuntu Linux client computer. The sample and connects to Microsoft SQL Database by using the **pyodbc** driver. + + +## Requirements + + +- [Python 2.7.6](https://www.python.org/download/releases/2.7.6/). + + +### Install the required modules + + +Open your terminal and navigate to a directory where you plan on creating your python script. Enter the following commands to install **Microsoft ODBC Driver for SQL Server** and **pyodbc**. pyodbc uses the ODBC Driver to connect to SQL Databases. + + sudo su + wget https://gallery.technet.microsoft.com/ODBC-Driver-13-for-Ubuntu-b87369f0/file/154097/2/installodbc.sh + sh installodbc.sh + sudo -H pip install pyodbc + + +## Create a database and retrieve your connection string + +For simplicity we will create an Azure SQL Database for the sample. You can use the same sample for an on-premise SQL Server instance. See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an AdventureWorks database template. The samples shown below only work with the AdventureWorks schema. diff --git a/samples/connect/python/pyodbc/linux/sample_python_linux.py b/samples/connect/python/pyodbc/linux/sample_python_linux.py new file mode 100644 index 00000000..e858744a --- /dev/null +++ b/samples/connect/python/pyodbc/linux/sample_python_linux.py @@ -0,0 +1,23 @@ +import pyodbc +server = 'tcp:myserver.database.windows.net' +database = 'mydb' +username = 'myusername' +password = 'mypassword' + +#Connection String +cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) +cursor = cnxn.cursor() + +#Sample select query +cursor.execute("SELECT @@version;") +row = cursor.fetchone() +while row: + print row[0] + row = cursor.fetchone() + +#Sample insert query +cursor.execute("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP )") +row = cursor.fetchone() +while row: + print 'Inserted Product key is ' + str(row[0]) + row = cursor.fetchone() diff --git a/samples/connect/python/pyodbc/windows/README.md b/samples/connect/python/pyodbc/windows/README.md new file mode 100644 index 00000000..6e0940ba --- /dev/null +++ b/samples/connect/python/pyodbc/windows/README.md @@ -0,0 +1,29 @@ +# Connect to SQL Database by using Python on Windows using pyodbc + + +[Python code sample] (sample_python_windows.py) that runs on a Windows computer. The sample and connects to Microsoft SQL Database by using the **pyodbc** driver. + + +## Requirements + + +- [Python 2.7.6](https://www.python.org/download/releases/2.7.6/). +- [ODBC Driver 11 or 13 for SQL Server Windows](https://www.microsoft.com/en-us/download/details.aspx?id=50420) +- [Pyodbc](https://pypi.python.org/pypi/pyodbc/3.0.10) + +### Install the required modules + +Navigate to your Python directory where the pip installer is located. For example C:\Python27\Scripts>. + +####Install pyodbc + + pip install pyodbc + +*Note: Instructions to enable the use pip can be found [here](http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows)* + + +## Create a database and retrieve your connection string + +For simplicity we will create an Azure SQL Database for the sample. You can use the same sample for an on-premise SQL Server instance. See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an AdventureWorks database template. The samples shown below only work with the AdventureWorks schema. + +See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an **AdventureWorks database template**. The samples shown below only work with the **AdventureWorks schema**. diff --git a/samples/connect/python/pyodbc/windows/sample_python_windows.py b/samples/connect/python/pyodbc/windows/sample_python_windows.py new file mode 100644 index 00000000..e858744a --- /dev/null +++ b/samples/connect/python/pyodbc/windows/sample_python_windows.py @@ -0,0 +1,23 @@ +import pyodbc +server = 'tcp:myserver.database.windows.net' +database = 'mydb' +username = 'myusername' +password = 'mypassword' + +#Connection String +cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password) +cursor = cnxn.cursor() + +#Sample select query +cursor.execute("SELECT @@version;") +row = cursor.fetchone() +while row: + print row[0] + row = cursor.fetchone() + +#Sample insert query +cursor.execute("INSERT SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP )") +row = cursor.fetchone() +while row: + print 'Inserted Product key is ' + str(row[0]) + row = cursor.fetchone() diff --git a/samples/connect/python/windows/README.md b/samples/connect/python/windows/README.md deleted file mode 100644 index 488c3d0c..00000000 --- a/samples/connect/python/windows/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Connect to SQL Database by using Python on Windows - - -[Python code sample] (sample_python_win.py) that runs on a Windows computer. The sample and connects to Azure SQL Database by using the **pymssql** driver. - - -## Requirements - - -- [Python 2.7.6](https://www.python.org/download/releases/2.7.6/). -- [FreeTDS](https://github.com/brianb/FreeTDS) -- [Pymssql](https://github.com/pymssql/pymssql) - -### Install the required modules - -Install [pymssql](http://www.lfd.uci.edu/~gohlke/pythonlibs/#pymssql). - -Make sure you choose the correct whl file. - -For example : If you are using Python 2.7 on a 64 bit machine choose : pymssql‑2.1.1‑cp27‑none‑win_amd64.whl. -Once you download the .whl file place it in the the C:/Python27 folder. - -Now install the pymssql driver using pip from command line. cd into C:/Python27 and run the following - - pip install pymssql‑2.1.1‑cp27‑none‑win_amd64.whl - -Instructions to enable the use pip can be found [here](http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows) - - -## Create a database and retrieve your connection string - - -See the [getting started page](http://azure.microsoft.com/documentation/articles/sql-database-get-started/) to learn how to create a sample database and get your connection string. It is important you follow the guide to create an **AdventureWorks database template**. The samples shown below only work with the **AdventureWorks schema**. diff --git a/samples/connect/python/windows/sample_python_win.py b/samples/connect/python/windows/sample_python_win.py deleted file mode 100644 index 3cc10320..00000000 --- a/samples/connect/python/windows/sample_python_win.py +++ /dev/null @@ -1,32 +0,0 @@ -#Using the pymssql driver -import pymssql - -#Connect to your database. -#Replace server name, username, and password with your credentials -#Code is dependent on AdventureWorks database -conn = pymssql.connect(server='yourserver', user='yourusername@yourserver', - password='yourpassword', database='yourdatabase') -cursor = conn.cursor() - -#SELECT -#Execute a simple select statement. -cursor.execute('SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer;') -row = cursor.fetchone() - -#Print results from select statement. -while row: - print str(row[0]) + " " + str(row[1]) + " " + str(row[2]) - row = cursor.fetchone() - -#INSERT -#Execute an insert statement -cursor.execute("INSERT SalesLT.Product (Name, ProductNumber, Color, StandardCost, ListPrice, SellStartDate) OUTPUT INSERTED.ProductID VALUES ('Bike', 'B1', 'Blue', 50, 120, CURRENT_TIMESTAMP)") -row = cursor.fetchone() - -#Print the ID of the inserted row. -while row: - print "Inserted Product ID : " +str(row[0]) - row = cursor.fetchone() - - -