Python MySQL

Python can be used in database applications.

MySQL is one of the most popular databases.

MySQL database

To be able to test the code examples in this tutorial, you should install MySQL on your computer.

Please download the free MySQL database here:https://www.mysql.com/downloads/

Install MySQL driver

Python requires a MySQL driver to access the MySQL database.

In this tutorial, we will use the driver "MySQL Connector".

We recommend that you install "MySQL Connector" using PIP.

PIP is likely already installed in the Python environment.

Navigate to the command line location of PIP and then type the following:

Download and install "MySQL Connector":

C:\...\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install mysql-connector

Τώρα, έχετε κατεβάσει και εγκαταστήσει το οδηγό οδήγησης MySQL.

Δοκιμή MySQL Connector

Για να ελέγξετε αν η εγκατάσταση ήταν επιτυχής ή αν έχετε εγκαταστήσει το "MySQL Connector", δημιουργήστε μια σελίδα Python που περιέχει τα εξής:

demo_mysql_test.py:

import mysql.connector

Εκτέλεση Παραδείγματος

Αν δεν υπάρχουν σφάλματα από τον παραπάνω κώδικα, τότε το "MySQL Connector" έχει εγκατασταθεί και είναι έτοιμο για χρήση.

Δημιουργία Σύνδεσης

Πρώτα δημιουργήστε τη σύνδεση με τη βάση δεδομένων.

Χρησιμοποιήστε το όνομα χρήστη και τον κωδικό πρόσβασης της βάσης δεδομένων MySQL:

demo_mysql_connection.py:

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword"
)
print(mydb)

Εκτέλεση Παραδείγματος

Τώρα, μπορείτε να ξεκινήσετε να χρησιμοποιείτε γράμματα SQL για να ερωτήσετε τη βάση δεδομένων.