python-mysqlHow can I grant privileges to a user in a MySQL database using Python?
To grant privileges to a user in a MySQL database using Python, you can use the MySQL Connector/Python library.
The following example code grants the SELECT privilege to the user_name user on the test_db database:
import mysql.connector
# Create connection
db = mysql.connector.connect(
host="localhost",
user="root",
password="password"
)
# Create cursor
cursor = db.cursor()
# Grant privilege
cursor.execute("GRANT SELECT ON test_db.* TO user_name")
The code consists of the following parts:
- Import the
mysql.connectorlibrary - Create a connection to the MySQL server
- Create a cursor object
- Execute the
GRANTstatement to grant the desired privilege
For more information, please refer to the MySQL GRANT documentation.
More of Python Mysql
- ¿Cómo conectar Python a MySQL usando ejemplos?
- How do I connect Python with MySQL using XAMPP?
- How can I use Python and MySQL to create a login system?
- How can I use Python and MySQL to generate a PDF?
- How do I use Python to connect to a MySQL database using XAMPP?
- How do Python and MySQL compare to MariaDB?
- How can I convert data from a MySQL database to XML using Python?
- How do I update a row in a MySQL database using Python?
- How do I set up a secure SSL connection between Python and MySQL?
- How can I use Python to retrieve data from MySQL?
See more codes...