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.connector
library - Create a connection to the MySQL server
- Create a cursor object
- Execute the
GRANT
statement to grant the desired privilege
For more information, please refer to the MySQL GRANT documentation.
More of Python Mysql
- How do I access MySQL using Python?
- How do I download MySQL-Python 1.2.5 zip file?
- How can I connect to MySQL using Python?
- How can I connect Python to a MySQL database?
- How can I use Yum to install the MySQLdb Python module?
- How do I use Python to connect to a MySQL database using XAMPP?
- How do I use Python to authenticate MySQL on Windows?
- How can I check the version of MySQL I'm using with Python?
- How do I use Python to select data from a MySQL database?
- How do I insert NULL values into a MySQL table using Python?
See more codes...