python-mysqlHow do I enable autocommit in Python MySQL?
To enable autocommit in Python MySQL, you need to use the autocommit
method of the MySQLConnection
object. This method takes a boolean value, True
to enable autocommit, or False
to disable it.
Example code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
mycursor = mydb.cursor()
mydb.autocommit = True
Code explanation
import mysql.connector
- imports the mysql.connector modulemydb = mysql.connector.connect(host="localhost", user="yourusername", passwd="yourpassword")
- connects to the MySQL databasemycursor = mydb.cursor()
- creates a cursor objectmydb.autocommit = True
- enables autocommit
Helpful links
More of Python Mysql
- How can I connect to MySQL using Python?
- How can I use Yum to install the MySQLdb Python module?
- How can I use Python to retrieve data from MySQL?
- How do I connect to a MySQL database using XAMPP and Python?
- How can I use Python and MySQL to generate a PDF?
- How can I connect Python to a MySQL database?
- How can I resolve the "no database selected" error when using Python and MySQL?
- How do I connect Python with MySQL using XAMPP?
- How do I use Python to query MySQL with multiple conditions?
- How do I use Python to authenticate MySQL on Windows?
See more codes...