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
- How do I connect Python with MySQL using XAMPP?
- How do Python and MySQL compare to MariaDB?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect Python and MySQL?
- How can I use Python and MySQL to generate a PDF?
- How can I connect to MySQL using Python?
- How can I connect Python to a MySQL database?
- How can I use Python to interact with a MySQL database using YAML?
- How can I connect Python to a MySQL database using an Xserver?
- How do I connect to a MySQL database using XAMPP and Python?
See more codes...