9951 explained code solutions for 126 technologies


python-mysqlHow can I use Python to insert a timestamp into a MySQL database?


To insert a timestamp into a MySQL database using Python, you must first connect to the database using a Python library such as MySQLdb. Once you have established a connection, you can use the cursor.execute() method to execute an INSERT statement.

For example, to insert a timestamp into a MySQL table named timestamps, you could use the following code:

import MySQLdb

# Establish a connection to the database
db = MySQLdb.connect("host", "user", "password", "database")

# Get a cursor object
cursor = db.cursor()

# Execute the INSERT statement
cursor.execute("INSERT INTO timestamps (timestamp) VALUES (NOW())")

# Commit the changes to the database
db.commit()

# Close the connection
db.close()

This code will insert the current timestamp into the timestamps table.

The code above consists of the following parts:

  1. import MySQLdb: This imports the MySQLdb module, which provides access to the MySQL database.

  2. db = MySQLdb.connect("host", "user", "password", "database"): This establishes a connection to the database using the provided credentials.

  3. cursor = db.cursor(): This creates a cursor object, which is used to execute queries.

  4. cursor.execute("INSERT INTO timestamps (timestamp) VALUES (NOW())"): This executes an INSERT query, which inserts the current timestamp into the timestamps table.

  5. db.commit(): This commits the changes to the database.

  6. db.close(): This closes the connection to the database.

For more information, see the following links:

Edit this code on GitHub