Query two columns in MySQL



    Python public recipes

    Query two colums from a MySQL table and return the result as an array. Each value in the array is a dictionary with the column name as a key.

    Library: mysql-connector-python

    # db is a connection to MySQL created with mysql.connector.connect()
    cursor = db.cursor()
    # Make sure we schedule only active projects
    cursor.execute("SELECT col1, col2 FROM table")
    for x in cursor.fetchall():
      result.append({"col1": x[0], "col2": x[1]})
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.