python-mysqlHow can I use Python MySQL libraries to develop software?
Python can be used to develop software that interacts with a MySQL database. To do this, you need to use one of the Python MySQL libraries. Examples of such libraries are mysql-connector
, MySQLdb
, and PyMySQL
.
Below is an example using the mysql-connector
library:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="user",
passwd="passwd"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
Output example
('information_schema',)
('mydatabase',)
('mysql',)
('performance_schema',)
('sys',)
The code above:
- Imports the
mysql.connector
library - Connects to the database
- Creates a cursor
- Executes a SQL query
- Iterates over the result set and prints each row
Helpful links
More of Python Mysql
- How do Python and MySQL compare to MariaDB?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect Python to a MySQL database?
- How do I connect Python with MySQL using XAMPP?
- How can I use Python to retrieve data from MySQL?
- How can I install the MySQL-Python (Python 2.x) module?
- How do I use Python to query MySQL with multiple conditions?
- How can I connect to MySQL using Python?
- How can I connect Python and MySQL?
- How can I use the MySQL Connector in Python?
See more codes...