Table of contents

  1. Python MariaDB - Select Query using PyMySQL
  2. Select iframe using Python + Selenium
  3. Python MariaDB - Insert into Table using PyMySQL

Python MariaDB - Select Query using PyMySQL

To perform a SELECT query on a MariaDB database using Python, you can use the PyMySQL package. Here's a step-by-step guide on how to execute a SELECT query:

  1. Install PyMySQL (if not already installed):

    pip install PyMySQL
  2. Import PyMySQL in your Python script:

    import pymysql
  3. Establish a connection to the MariaDB database:

    connection = pymysql.connect(host='hostname',
  4. Create a cursor and execute the SELECT query:

        with connection.cursor() as cursor:
            # Write the SQL query
            sql = "SELECT * FROM your_table_name"
            # Execute the query
            # Fetch all the records
            result = cursor.fetchall()
            for row in result:

Here is a full example demonstrating how to connect to a MariaDB database and retrieve rows from a table:

import pymysql.cursors

# Define your connection parameters
hostname = 'localhost'
username = 'your_username'
password = 'your_password'
database = 'your_database'

# Connect to the database
connection = pymysql.connect(host=hostname,

    with connection.cursor() as cursor:
        # SQL query string
        sql_query = "SELECT * FROM table_name WHERE column_name = %s"
        cursor.execute(sql_query, ('value',))

        # Fetch all the rows
        rows = cursor.fetchall()

        for row in rows:
            # Assuming you're fetching a table with columns 'id' and 'data'
            print(f"id: {row['id']}, data: {row['data']}")
except pymysql.MySQLError as e:
    print(f"Error: {e}")


In the above example, make sure to replace hostname, username, password, database, table_name, column_name, and the value with your actual database connection details and query parameters.

Also, the use of %s in the SQL query is a placeholder for parameters that you want to pass to the query. This is a good practice to avoid SQL injection. The cursorclass=pymysql.cursors.DictCursor argument is used so that the fetched results are returned as dictionaries with column names as keys, which makes it easier to access the data by column name.

Select iframe using Python + Selenium

To select and interact with an iframe using Python and Selenium, you can follow these steps:

  1. Import the necessary libraries:

    Make sure you have the Selenium WebDriver installed and import the required modules.

    from selenium import webdriver
    from import By
    from selenium.webdriver.common.keys import Keys
    from import WebDriverWait
    from import expected_conditions as EC
  2. Create a WebDriver instance:

    Initialize a WebDriver instance (e.g., Chrome or Firefox).

    driver = webdriver.Chrome()  # You can use other WebDriver options (e.g., Firefox, Edge, etc.)
  3. Navigate to a web page with an iframe:

    Load the webpage that contains the iframe.

  4. Switch to the iframe:

    You can switch to an iframe using the switch_to.frame() method, which takes either the iframe's index or the iframe's element as an argument.

    If you know the iframe's index (0 for the first iframe, 1 for the second, and so on), you can use:


    If you have access to the iframe's element, you can use:

    iframe_element = driver.find_element(By.TAG_NAME, 'iframe')

    Replace 'iframe' with the appropriate HTML tag or attribute that identifies the iframe.

  5. Interact with elements inside the iframe:

    Once you've switched to the iframe, you can interact with the elements inside it as if they were part of the main page.

    element_inside_iframe = driver.find_element(By.XPATH, '//your/xpath')
    element_inside_iframe.send_keys("Text to enter in the iframe element")
  6. Switch back to the main content:

    After you've finished interacting with the elements inside the iframe, you can switch back to the main content using driver.switch_to.default_content().

  7. Close the WebDriver:

    Finally, make sure to close the WebDriver when you're done.


These steps should allow you to interact with iframes on a webpage using Python and Selenium. Just ensure that you switch back to the default content when you're finished with the iframe, as the WebDriver operates in the context of the current frame.

Python MariaDB - Insert into Table using PyMySQL

PyMySQL is a pure Python MySQL client library that provides a way to connect to MySQL databases and perform various operations including data insertions. Though it's primarily for MySQL, it can also be used for MariaDB as MariaDB is a fork of MySQL and maintains compatibility with MySQL's APIs.

Here's a step-by-step guide to insert data into a MariaDB table using PyMySQL:

1. Install PyMySQL:

First, you need to install the PyMySQL library. You can do this using pip:

pip install pymysql

2. Code to insert data:

Here's a simple example of inserting data into a MariaDB table:

import pymysql

# Database connection parameters
db_params = {
    'host': 'localhost',
    'user': 'your_username',
    'password': 'your_password',
    'database': 'your_database_name'

# Connect to the database
connection = pymysql.connect(**db_params)

    with connection.cursor() as cursor:
        # Create an insert statement
        sql = "INSERT INTO `your_table_name` (`column1`, `column2`, ...) VALUES (%s, %s, ...)"
        # Data to be inserted
        data = ("value1", "value2", ...)
        # Execute the query
        cursor.execute(sql, data)
        # Commit the transaction


Replace placeholders like 'your_username', 'your_password', 'your_database_name', 'your_table_name', and column and value placeholders with your actual database details and data.

Note: Always make sure you use parameterized queries (like with the %s placeholders shown above) to prevent SQL injection attacks.

3. Batch Inserts:

If you have multiple rows to insert at once, you can use the executemany() method for efficiency:

with connection.cursor() as cursor:
    sql = "INSERT INTO `your_table_name` (`column1`, `column2`) VALUES (%s, %s)"
    # List of data to be inserted
    data = [
        ("value1_1", "value2_1"),
        ("value1_2", "value2_2"),
        # ... (more data)
    cursor.executemany(sql, data)

This method is more efficient than looping through and inserting one row at a time because it reduces the number of separate communications with the database server.

More Python Questions

More C# Questions