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 can I use Python and MySQL to generate a PDF?
- How can I connect Python to a MySQL database?
- How can I connect Python to a MySQL database using an Xserver?
- How can I connect Python and MySQL?
- How do Python and MySQL compare to MariaDB?
- How do I use Python to update multiple columns in a MySQL database?
- How do I connect Python with MySQL using XAMPP?
- How can I connect to MySQL using Python?
- How do I use Python to authenticate MySQL on Windows?
- How can I convert a MySQL database to a SQLite database using Python?
See more codes...