You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/mysql/connect-python.md
+75-84Lines changed: 75 additions & 84 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,86 +7,81 @@ ms.service: mysql
7
7
ms.custom: mvc
8
8
ms.devlang: python
9
9
ms.topic: quickstart
10
-
ms.date: 12/02/2019
10
+
ms.date: 01/09/2020
11
11
ms.custom: seo-python-october2019
12
12
---
13
13
14
14
# 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.
16
16
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.
18
18
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)
23
23
24
24
## 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:
26
26
27
27
> [!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
+
44
40
```bash
45
-
sudo pip install mysql-connector-python-rf
41
+
pip install mysql-connector-python
46
42
```
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/).
59
45
60
46
## 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
-

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
+

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
+

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
81
76
import mysql.connector
82
77
from mysql.connector import errorcode
83
78
84
79
# Obtain connection string information from the portal
85
80
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>'
90
85
}
91
86
92
87
# Construct connection string
@@ -129,20 +124,20 @@ else:
129
124
## Read data
130
125
Use the following code to connect and read the data by using a **SELECT** SQL statement.
131
126
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.
133
128
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.
135
130
136
-
```Python
131
+
```python
137
132
import mysql.connector
138
133
from mysql.connector import errorcode
139
134
140
135
# Obtain connection string information from the portal
141
136
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>'
146
141
}
147
142
148
143
# Construct connection string
@@ -178,20 +173,18 @@ else:
178
173
## Update data
179
174
Use the following code to connect and update the data by using an **UPDATE** SQL statement.
180
175
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.
182
177
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
186
179
import mysql.connector
187
180
from mysql.connector import errorcode
188
181
189
182
# Obtain connection string information from the portal
190
183
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>'
195
188
}
196
189
197
190
# Construct connection string
@@ -222,20 +215,18 @@ else:
222
215
## Delete data
223
216
Use the following code to connect and remove data by using a **DELETE** SQL statement.
224
217
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.
228
219
229
-
```Python
220
+
```python
230
221
import mysql.connector
231
222
from mysql.connector import errorcode
232
223
233
224
# Obtain connection string information from the portal
0 commit comments