1

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()
1
  • @alecxe: well, not like 127.0.0.1 would be of any use in getting at OP's database... Commented Mar 22, 2016 at 21:17

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what rapid debugging is called. Nice spot.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.