Eva
Eva

Reputation: 13

Connect MySQL with Python (GUI)

I'm trying to connect a MySQL database with python GUI. But this part of code returns a empty set. I'm not sure what's wrong with my code because there are no error codes... Please help!

def ButtonPressed (self):
    print("Finding Parts!")
    self.ProdNum = self.aVar.get()
    print("Entry text was:", self.ProdNum)

    self.db = mysql.connector.connect (user='ezhu', password='<password>', host='127.0.0.1', database='centricsit_prices')
    self.query = ("SELECT sd, sy, price FROM css_hp WHERE prod_num = '%s'")
    self.cursor = self.db.cursor()
    self.cursor.execute (self.query, (self.ProdNum))
    self.results = self.cursor.fetchall()
    print (self.results)
    self.cursor.close()

Upvotes: 1

Views: 468

Answers (1)

alecxe
alecxe

Reputation: 473863

You are missing the comma:

self.cursor.execute (self.query, (self.ProdNum, ))
                                          HERE^

It is quite important since the query parameters are expected to be passed as an iterable. Comma would make it a tuple. Without a comma, you are passing query parameters as a string, which is also an iterable, hence your query is parameterized with a first character of self.ProdNum, hence nothing matched by the select query.

Upvotes: 1

Related Questions