Snippets

Frederik Banke Python MySQL example

Created by Frederik Banke
import mysql.connector


def get_data_from_mysql(cnx, key):
    cursor = cnx.cursor(prepared=True)

    query = "SELECT field FROM items WHERE key = %s"

    cursor.execute(query, (key,))

    total = 0
    for row in cursor:
        total += row[0]

    cursor.close()

    return total

Comments (1)

  1. Linda Melson
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.