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
@@ -68,6 +68,19 @@ Get the connection information you need to connect to Azure Database for MySQL f
68
68
69
69
:::image type="content" source="./media/connect-python/azure-database-for-mysql-server-overview-name-login.png" alt-text="Azure Database for MySQL server name 2":::
70
70
71
+
## Running the Python code samples
72
+
73
+
For each code example in this article:
74
+
75
+
1. Create a new file in a text editor.
76
+
2. 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.
77
+
1. SSL is enabled by default on Azure Database for MySQL servers. You may need to download the [DigiCertGlobalRootG2 SSL certificate](https://cacerts.digicert.com/DigiCertGlobalRootG2.crt.pem) to connect from your local environment. Replace the `ssl_ca` value in the code with path to this file on your computer.
78
+
1. Save the file in a project folder with a *.py* extension, such as *C:\pythonmysql\createtable.py* or */home/username/pythonmysql/createtable.py*.
79
+
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.
80
+
81
+
> [!NOTE]
82
+
> 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`.
83
+
71
84
## Step 1: Create a table and insert data
72
85
73
86
Use the following code to connect to the server and database, create a table, and load data by using an **INSERT** SQL statement.The code imports the mysql.connector library, and uses the method:
@@ -76,29 +89,23 @@ Use the following code to connect to the server and database, create a table, an
76
89
-[cursor.close()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-close.html) when you are done using a cursor.
77
90
-[conn.close()](https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-close.html) to close the connection the connection.
78
91
79
-
> [!IMPORTANT]
80
-
> - SSL is enabled by default. You may need to download the [DigiCertGlobalRootG2 SSL certificate](https://cacerts.digicert.com/DigiCertGlobalRootG2.crt.pem) to connect from your local environment.
81
-
> - Replace the `<mydemoserver>`, `<myadmin>`, `<mypassword>`, and `<mydatabase>` placeholders with the values for your MySQL server and database.
82
-
83
92
```python
84
93
import mysql.connector
85
94
from mysql.connector import errorcode
86
95
87
96
# Obtain connection string information from the portal
@@ -144,6 +151,35 @@ Use the following code to connect and read the data by using a **SELECT** SQL st
144
151
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.
145
152
146
153
```python
154
+
import mysql.connector
155
+
from mysql.connector import errorcode
156
+
157
+
# Obtain connection string information from the portal
Use the following code to connect and update the data by using an **UPDATE** SQL statement. The code imports the mysql.connector library, and uses [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.
161
202
162
203
```python
204
+
import mysql.connector
205
+
from mysql.connector import errorcode
206
+
207
+
# Obtain connection string information from the portal
print("Something is wrong with the user name or password")
226
+
elif err.errno == errorcode.ER_BAD_DB_ERROR:
227
+
print("Database does not exist")
228
+
else:
229
+
print(err)
230
+
else:
231
+
cursor = conn.cursor()
232
+
163
233
# Update a data row in the table
164
-
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (200, "banana"))
234
+
cursor.execute("UPDATE inventory SET quantity = %s WHERE name = %s;", (300, "apple"))
165
235
print("Updated",cursor.rowcount,"row(s) of data.")
236
+
237
+
# Cleanup
238
+
conn.commit()
239
+
cursor.close()
240
+
conn.close()
241
+
print("Done.")
166
242
```
167
243
168
244
## Step 4: Delete data
169
245
170
246
Use the following code to connect and remove data by using a **DELETE** SQL statement. The code imports the mysql.connector library, and uses [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.
171
247
172
248
```python
249
+
import mysql.connector
250
+
from mysql.connector import errorcode
251
+
252
+
# Obtain connection string information from the portal
0 commit comments