Print the results of consultation records with LIKE. mysql + python3

0

Hi, I would appreciate it if someone could help me with this problem. I want to print each of the results that this query would throw at me, as when it is executed in MySQL. What I want is for example to obtain all the records that contain the word "source" in the "Product" column, several products have it in the table and I would like to print all the results.

import MySQLdb

def consultar(Producto):

    try:
        db     = MySQLdb.connect("localhost","root","1234","pcsolution")
        cursor = db.cursor()
        sql    = ("SELECT * FROM productos WHERE Producto LIKE ='"+Producto+"';")
        cursor.execute(sql)
        for (Codigo,Producto,Unidades) in cursor:            
            return Producto,Marca,Unidades
            db.commit()
            db.close()
    except:
        return ("Problemas de conexión")

Producto,Marca,Unidades = consultar("fuente")
print (Producto)
print (Marca)
print (Unidades)
    
asked by Samuel 10.11.2017 в 10:10
source

1 answer

0

I do not understand so much about python, but about Mysql. In the section where you state the query you only need to include wildcard (wildcards). Here you can read about it. TL; DR: Add % characters to the beginning and end of the query parameter.

... where campo LIKE '%mi_parametro%';

Where the character % is a wildcard meaning "any character zero or more times".

Greetings.

    
answered by 30.12.2017 в 03:11