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.connectorlibrary - 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 can I connect Python to a MySQL database?
- How can I create a web application using Python and MySQL?
- How can I convert a MySQL query result to a Python dictionary?
- How do I fix a bad MySQL handshake error in Python?
- How can I create a Python MySQL tutorial?
- How do I use the REPLACE INTO statement in Python with MySQL?
- How do I use a Python MySQL refresh cursor?
- How can I host a MySQL database using Python?
- How do I use Python and MySQL to get the number of rows?
- How do I access MySQL using Python?
See more codes...