MySQL select with where

import mysql.connector

pid=input(“Enter Product ID: “)

dbase=mysql.connector.connect(user=”root”,password=”root”,database=”testdb”)

cursor=dbase.cursor()

sqlCriteria=”select * from testdb.product where Product_Id=’%s'”

params=(pid)

try:

       print(sqlCriteria % params)

       cursor.execute(sqlCriteria % params)
       products = cursor.fetchall()

       for product in products:
              print(product[0],product[1],product[2],product[3],product[4])

except:

       print(‘DB Problem.’)

finally:
       cursor.close()
       dbase.close()


Output

Enter Product ID: B100
select * from testdb.product where Product_Id=’B100′
B100 Mouse Hardware 230.0 2019-09-09

Published by Python programming examples for beginners

Abhay Gadkari is an IT professional having around experience of 20+ years in IT industry. He worked on web technologies and databases with Insurance and ERP projects.