Skip to content

Commit 004064e

Browse files
authored
Merge pull request #99891 from v-thepet/mysql
Refresh mysql/connect-python.md
2 parents 5d5638c + 0599c2d commit 004064e

File tree

3 files changed

+75
-84
lines changed

3 files changed

+75
-84
lines changed

articles/mysql/connect-python.md

Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,86 +7,81 @@ ms.service: mysql
77
ms.custom: mvc
88
ms.devlang: python
99
ms.topic: quickstart
10-
ms.date: 12/02/2019
10+
ms.date: 01/09/2020
1111
ms.custom: seo-python-october2019
1212
---
1313

1414
# Quickstart: Use Python to connect and query data with Azure Database for MySQL
15-
This article demonstrates how to use [Python](https://python.org) to connect to an Azure Database for MySQL. It uses SQL statements to query, insert, update, and delete data in the database from Mac OS, Ubuntu Linux, and Windows platforms.
15+
This quickstart demonstrates how to use [Python](https://python.org) to connect to an Azure Database for MySQL. You can use SQL statements to query, insert, update, and delete data in the database from Mac OS, Ubuntu Linux, and Windows platforms.
1616

17-
This topic assumes that you are familiar with developing using Python and that you are new to working with Azure Database for MySQL.
17+
This article assumes that you're familiar with developing using Python, but you're new to working with Azure Database for MySQL.
1818

19-
## Prerequisites
20-
This quickstart uses the resources created in either of these guides as a starting point:
21-
- [Create an Azure Database for MySQL server using Azure portal](./quickstart-create-mysql-server-database-using-azure-portal.md)
22-
- [Create an Azure Database for MySQL server using Azure CLI](./quickstart-create-mysql-server-database-using-azure-cli.md)
19+
## Create an Azure Database for MySQL
20+
Create an Azure Database for MySQL server and database by following the instructions at
21+
- [Create an Azure Database for MySQL server using Azure portal](quickstart-create-mysql-server-database-using-azure-portal.md) or
22+
- [Create an Azure Database for MySQL server using Azure CLI](quickstart-create-mysql-server-database-using-azure-cli.md)
2323

2424
## Install Python and the MySQL connector
25-
Install [Python](https://www.python.org/downloads/) and the [MySQL connector for Python](https://dev.mysql.com/downloads/connector/python/) on your own machine. Depending on your platform, follow the steps in the appropriate section below.
25+
Install Python and the MySQL connector for Python on your computer by using the following steps:
2626

2727
> [!NOTE]
28-
> This quickstart uses a raw SQL query approach to connect to MySQL to run queries. If you are using a web framework, use the recommended connector for those frameworks. For example, [mysqlclient](https://pypi.org/project/mysqlclient/) is suggested for use with Django.
29-
>
30-
31-
### Windows
32-
1. Download and Install Python 3.7 from [python.org](https://www.python.org/downloads/windows/).
33-
2. Check the Python installation by launching the command prompt. Run the command `C:\python37\python.exe -V` using the uppercase V switch to see the version number.
34-
3. Install the Python connector for MySQL from [mysql.com](https://dev.mysql.com/downloads/connector/python/) corresponding to your version of Python.
35-
36-
### Linux (Ubuntu)
37-
1. In Linux (Ubuntu), Python is typically installed as part of the default installation.
38-
2. Check the Python installation by launching the bash shell. Run the command `python -V` using the uppercase V switch to see the version number.
39-
3. Check the PIP installation by running the `pip show pip -V` command to see the version number.
40-
4. PIP may be included in some versions of Python. If PIP is not installed, you may install the [PIP](https://pip.pypa.io/en/stable/installing/) package, by running command `sudo apt-get install python-pip`.
41-
5. Update PIP to the latest version, by running the `pip install -U pip` command.
42-
6. Install the MySQL connector for Python, and its dependencies by using the PIP command:
43-
28+
> This quickstart uses a raw SQL query approach to connect to MySQL. If you're using a web framework, use the recommended connector for the framework, for example, [mysqlclient](https://pypi.org/project/mysqlclient/) for Django.
29+
30+
1. Download and install [Python 3.7 or above](https://www.python.org/downloads/) for your OS. Make sure to add Python to your `PATH`, because the MySQL connector requires that.
31+
32+
1. Open a command prompt or `bash` shell, and check your Python version by running `python -V` with the uppercase V switch.
33+
34+
1. The `pip` package installer is included in the latest versions of Python. Update `pip` to the latest version by running `pip install -U pip`.
35+
36+
If `pip` isn't installed, you can download and install it with `get-pip.py`. For more information, see [Installation](https://pip.pypa.io/en/stable/installing/).
37+
38+
1. Use `pip` to install the MySQL connector for Python and its dependencies:
39+
4440
```bash
45-
sudo pip install mysql-connector-python-rf
41+
pip install mysql-connector-python
4642
```
47-
48-
### MacOS
49-
1. In Mac OS, Python is typically installed as part of the default OS installation.
50-
2. Check the Python installation by launching the bash shell. Run the command `python -V` using the uppercase V switch to see the version number.
51-
3. Check the PIP installation by running the `pip show pip -V` command to see the version number.
52-
4. PIP may be included in some versions of Python. If PIP is not installed, you may install the [PIP](https://pip.pypa.io/en/stable/installing/) package.
53-
5. Update PIP to the latest version, by running the `pip install -U pip` command.
54-
6. Install the MySQL connector for Python, and its dependencies by using the PIP command:
55-
56-
```bash
57-
pip install mysql-connector-python-rf
58-
```
43+
44+
You can also install the Python connector for MySQL from [mysql.com](https://dev.mysql.com/downloads/connector/python/). For more information about the MySQL Connector for Python, see the [MySQL Connector/Python Developer Guide](https://dev.mysql.com/doc/connector-python/en/).
5945

6046
## Get connection information
61-
Get the connection information needed to connect to the Azure Database for MySQL. You need the fully qualified server name and login credentials.
62-
63-
1. Log in to the [Azure portal](https://portal.azure.com/).
64-
2. From the left-hand menu in Azure portal, select **All resources**, and then search for the server you have created (such as **mydemoserver**).
65-
3. Select the server name.
66-
4. From the server's **Overview** panel, make a note of the **Server name** and **Server admin login name**. If you forget your password, you can also reset the password from this panel.
67-
![Azure Database for MySQL server name](./media/connect-python/azure-database-for-mysql-server-overview-name-login.png)
68-
69-
## Run Python code
70-
- Paste the code into a text file, and then save the file into a project folder with file extension .py (such as C:\pythonmysql\createtable.py or /home/username/pythonmysql/createtable.py).
71-
- To run the code, launch the command prompt or Bash shell. Change directory into your project folder `cd pythonmysql`. Then type the python command followed by the file name `python createtable.py` to run the application. On the Windows OS, if python.exe is not found, you may need to provide the full path to the executable or add the Python path into the path environment variable. `C:\python27\python.exe createtable.py`
72-
73-
## Connect, create table, and insert data
74-
Use the following code to connect to the server, create a table, and load the data by using an **INSERT** SQL statement.
75-
76-
In the code, the mysql.connector library is imported. The [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function is used to connect to Azure Database for MySQL using the [connection arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and method [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) executes the SQL query against MySQL database.
77-
78-
Replace the `host`, `user`, `password`, and `database` parameters with the values that you specified when you created the server and database.
79-
80-
```Python
47+
Get the connection information you need to connect to Azure Database for MySQL from the Azure portal. You need the server name, database name, and login credentials.
48+
49+
1. Sign in to the [Azure portal](https://portal.azure.com/).
50+
51+
1. In the portal search bar, search for and select the Azure Database for MySQL server you created, such as **mydemoserver**.
52+
53+
![Azure Database for MySQL server name](./media/connect-python/1_server-overview-name-login.png)
54+
55+
1. From the server's **Overview** page, make a note of the **Server name** and **Server admin login name**. If you forget your password, you can also reset the password from this page.
56+
57+
![Azure Database for MySQL server name](./media/connect-python/azure-database-for-mysql-server-overview-name-login.png)
58+
59+
## Run the Python examples
60+
For each code example in this article:
61+
62+
1. Create a new file in a text editor.
63+
1. Add the code example to the file. In the code, replace the `<mydemoserver>`, `<myadmin>`, `<mypassword>`, and `<mydatabase>` placeholders with the values for your MySQL server and database.
64+
1. Save the file in a project folder with a *.py* extension, such as *C:\pythonmysql\createtable.py* or */home/username/pythonmysql/createtable.py*.
65+
1. To run the code, open a command prompt or `bash` shell and change directory into your project folder, for example `cd pythonmysql`. Type the `python` command followed by the file name, for example `python createtable.py`, and press Enter.
66+
67+
> [!NOTE]
68+
> On Windows, if *python.exe* is not found, you may need to add the Python path into your PATH environment variable, or provide the full path to *python.exe*, for example `C:\python27\python.exe createtable.py`.
69+
70+
## Create a table and insert data
71+
Use the following code to connect to the server and database, create a table, and load data by using an **INSERT** SQL statement.
72+
73+
The code imports the mysql.connector library, and uses the [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function to connect to Azure Database for MySQL using the [arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and the [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) method executes the SQL query against the MySQL database.
74+
75+
```python
8176
import mysql.connector
8277
from mysql.connector import errorcode
8378

8479
# Obtain connection string information from the portal
8580
config = {
86-
'host':'mydemoserver.mysql.database.azure.com',
87-
'user':'myadmin@mydemoserver',
88-
'password':'yourpassword',
89-
'database':'quickstartdb'
81+
'host':'<mydemoserver>.mysql.database.azure.com',
82+
'user':'<myadmin>@<mydemoserver>',
83+
'password':'<mypassword>',
84+
'database':'<mydatabase>'
9085
}
9186

9287
# Construct connection string
@@ -129,20 +124,20 @@ else:
129124
## Read data
130125
Use the following code to connect and read the data by using a **SELECT** SQL statement.
131126

132-
In the code, the mysql.connector library is imported. The [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function is used to connect to Azure Database for MySQL using the [connection arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and method [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) executes the SQL statement against MySQL database. The data rows are read using method [fetchall()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html). The result set is kept in a collection row and a for iterator is used to loop over the rows.
127+
The code imports the mysql.connector library, and uses the [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function to connect to Azure Database for MySQL using the [arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and the [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) method executes the SQL query against the MySQL database.
133128

134-
Replace the `host`, `user`, `password`, and `database` parameters with the values that you specified when you created the server and database.
129+
The code reads the data rows using the [fetchall()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html) method, keeps the result set in a collection row, and uses a `for` iterator to loop over the rows.
135130

136-
```Python
131+
```python
137132
import mysql.connector
138133
from mysql.connector import errorcode
139134

140135
# Obtain connection string information from the portal
141136
config = {
142-
'host':'mydemoserver.mysql.database.azure.com',
143-
'user':'myadmin@mydemoserver',
144-
'password':'yourpassword',
145-
'database':'quickstartdb'
137+
'host':'<mydemoserver>.mysql.database.azure.com',
138+
'user':'<myadmin>@<mydemoserver>',
139+
'password':'<mypassword>',
140+
'database':'<mydatabase>'
146141
}
147142

148143
# Construct connection string
@@ -178,20 +173,18 @@ else:
178173
## Update data
179174
Use the following code to connect and update the data by using an **UPDATE** SQL statement.
180175

181-
In the code, the mysql.connector library is imported. The [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function is used to connect to Azure Database for MySQL using the [connection arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and method [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) executes the SQL statement against MySQL database.
176+
The code imports the mysql.connector library, and uses the [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function to connect to Azure Database for MySQL using the [arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and the [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) method executes the SQL query against the MySQL database.
182177

183-
Replace the `host`, `user`, `password`, and `database` parameters with the values that you specified when you created the server and database.
184-
185-
```Python
178+
```python
186179
import mysql.connector
187180
from mysql.connector import errorcode
188181

189182
# Obtain connection string information from the portal
190183
config = {
191-
'host':'mydemoserver.mysql.database.azure.com',
192-
'user':'myadmin@mydemoserver',
193-
'password':'yourpassword',
194-
'database':'quickstartdb'
184+
'host':'<mydemoserver>.mysql.database.azure.com',
185+
'user':'<myadmin>@<mydemoserver>',
186+
'password':'<mypassword>',
187+
'database':'<mydatabase>'
195188
}
196189

197190
# Construct connection string
@@ -222,20 +215,18 @@ else:
222215
## Delete data
223216
Use the following code to connect and remove data by using a **DELETE** SQL statement.
224217

225-
In the code, the mysql.connector library is imported. The [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function is used to connect to Azure Database for MySQL using the [connection arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and method [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) executes the SQL query against MySQL database.
226-
227-
Replace the `host`, `user`, `password`, and `database` parameters with the values that you specified when you created the server and database.
218+
The code imports the mysql.connector library, and uses the [connect()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysql-connector-connect.html) function to connect to Azure Database for MySQL using the [arguments](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) in the config collection. The code uses a cursor on the connection, and the [cursor.execute()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html) method executes the SQL query against the MySQL database.
228219

229-
```Python
220+
```python
230221
import mysql.connector
231222
from mysql.connector import errorcode
232223

233224
# Obtain connection string information from the portal
234225
config = {
235-
'host':'mydemoserver.mysql.database.azure.com',
236-
'user':'myadmin@mydemoserver',
237-
'password':'yourpassword',
238-
'database':'quickstartdb'
226+
'host':'<mydemoserver>.mysql.database.azure.com',
227+
'user':'<myadmin>@<mydemoserver>',
228+
'password':'<mypassword>',
229+
'database':'<mydatabase>'
239230
}
240231

241232
# Construct connection string
-2.17 KB
Loading
-3.83 KB
Loading

0 commit comments

Comments
 (0)