How to execute a query using Python function

Asked

Viewed 73 times

0

I set up a function that connects me to the BD and returns me a query:

def consult(self, query):
    try:
        conn = mariadb.connect(
            user="XXXXX",
            password="XXXXX",
            host="192.168.000.000",
            port=3306,
            database="data_base"
        )
    except mariadb.Error as e:
        print(f"Error connecting to MariaDB Platform: {e}")

    cur = conn.cursor()
    cur.execute(query)
    return cur

And in another function has a query that I want to execute:

def query(self):

    stringQuery = """
    SELECT
    l.chave
    FROM inventario i
    INNER JOIN licencas l ON l.id = i.idLicenca
    WHERE i.id = 1728 
    """
    cur = self.consult(stringQuery)

    chave = str(cur)
    for y in ['\n', ',', '/', "'", '(', ')']:
        chave = chave.replace(y, "")
    
    print(chave)

However this error is returned: '<mariadb.connection.cursor Object at 0x0000028137FA59A0>'

I tried to modify the execution function, but I got no result.

  • This is not an error, it is the cursor object you received as a return. To get the results of this query you will need to iterate over the cursor: for row in cur: ...

  • Wow, it worked. I can’t believe I slugged into this. Thanks!!!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.