
It’s time for me to head to bed, but before I do, I wanted to share a quick win. I finally found a solution to a problem that had been giving me trouble for the past few hours. It involved using an SQL WHERE clause to take user input and execute a SELECT statement against the database.
Now that it’s working flawlessly, I couldn’t resist sharing it with you.
The code:
Actual code:
import mysql.connector
store_name = str(input("What is the store name? "))
try:
connection = mysql.connector.connect(host='localhost',
user='user',
password='ps',
database='db')
cursor = connection.cursor(prepared=True)
sql_query = """select * from sample_tbl where store_name = %s"""
tuple1 = (store_name,)
cursor.execute(sql_query, tuple1)
for tbl in cursor:
print(tbl)
connection.commit()
except mysql.connector.Error as error:
print("parameterized query failed {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
Description:
So, there are two parts that need your attention:
The query:
This is what it should be.
sql_query = """select * from sample_tbl where store_name = %s"""
And this is the mistake I made.
sql_query = """select * from sample_tbl where store_name = " + %s"""
The tuple:
This is what it should be. by the way, the comma at the end of the tuple still feels a bit weird… (I’m a Java developer lol)
tuple1 = (store_name,)
And this is the mistake I made. I forgot the comma.
tuple1 = (store_name)
And this is the executed result:
Afterthoughts:
Lately, I’ve been having some sleepless nights—courtesy of the programming languages that never cease to fascinate me. The more I uncover about each language, the more I realize how much there is still to learn. This paradox only deepens my curiosity and fuels my desire to explore, experiment, and grow within the ever-expanding world of STEM.

