Python MariaDB cant select items from table

Question:

I am currently attempting to read data out of a Table in my MariaDB database, but whenever I run the code it returns None, as if the table was empty, which it isnt. I used the SQL command provided by the database. Any ideas?

import sys

import mariadb as mysql
from pwd import pwd


def main():
    try:
        dbase = mysql.connect(
            user="???",
            password=pwd,
            host="???",
            port=???,
            database="market"
        )
    except mysql.Error as e:
        print(f"Error connecting to MariaDB Platform: {e}")
        sys.exit(1)
    print(dbase)
    cursor = dbase.cursor()
    products = cursor.execute("SELECT * FROM Products")
    print(products)


if 1 == 1:
    main()

Asked By: Snxnpy

||

Answers:

According to the mariadb documentation, the execute method on a cursor only executes the statement.

To get the results, you are interested in, you can use one of the methods fetchall, fetchone or fetchmany for instance.

For example:

cursor = dbase.cursor()
cursor.execute("SELECT * FROM Products")
products = cursor.fetchall()
print(products)

which should yield something like [(1, "product1", 54), (2, "product2", 33)] if, for instance the table Products has three columns: id, description and quantity, and 2 rows

Answered By: Florent MONIN
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.