top of page
back.jpg

Python MySQL Database Operations Example with mysql.connector



import mysql.connector

try:
    # Establish a connection to the MySQL database
    con = mysql.connector.connect(host="localhost", port=3306, user="username", password="password", database="Education")

    # Create a cursor to execute SQL queries
    cursor = con.cursor()

    # Insert records into the "student" table
    cursor.execute("INSERT INTO student VALUES('Eliza', 104)")
    cursor.execute("INSERT INTO student VALUES('Kattie', 105)")
    cursor.execute("INSERT INTO student VALUES('Jack', 106)")

    # Execute a SELECT query to retrieve all records from the "student" table
    cursor.execute("SELECT * FROM student")

    # Print the results of the SELECT query
    for r in cursor:
        print(r[0], r[1])

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

except mysql.connector.Error as e:
    print("Connection Unsuccessful:", e)

finally:
    # Close the cursor and connection in the finally block to ensure proper cleanup
    cursor.close()
    con.close()

1 view0 comments

Recent Posts

See All

What is Flaky test? Steps to resolve Test Flakiness

A flaky test An automated test in a software testing environment that may produce inconsistent results, sometimes passing and sometimes failing, even when applied to the same version of the software u

Retrieve data from a JSON file

To retrieve data from a JSON file using Java and Selenium, you can use a library like Gson or Jackson for handling JSON parsing. { "username": "Tester1", "password": "Test1234", "url": "https://demoqa

Java JDBC Database Operations Example

package com.seleniumExamples2024; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Database

bottom of page