1

I am attempting to retrieve data associated given a particular last name. This last name, however, is stored within a listbox along with a first name and separator comma. As such, the name is retrieved from the listbox first by cursor selection, and only the last name is used to search by partitioning the rest:

lastname, sep, firstname = (self.patient_list.get(self.patient_list.curselection())).partition(',') 

Once this is done, I am trying to get a printout of the data rows for this chosen last name. However, I am coming across the issue:

TypeError: argument 1 must be a string or unicode object: got tuple instead

I am wondering how to proceed with this issue. I have attempted a few solutions, such as the tuple function within the argument in load_query, as well as str on lastname, but it is a string...

I am also wondering if it is even necessary to select from a PostgreSQL db by this method. Can I try binding a listbox element to a row of data in the db? Or avoid partitioning the element, since that seems to be of trouble perhaps...

Full code:

        def load_profile(self, event):
            conn = pg.connect(user='postgres',
                                    password='123!',
                                    host='localhost',
                                    port='5430',
                                    database='carepartnerdb')
            
            cur = conn.cursor()
    
            #gets lastname from listbox, removes all else past the comma
            #used to associate to DB 
            lastname, sep, firstname = (self.patient_list.get(self.patient_list.curselection())).partition(',')
    
            load_query = (""" SELECT * FROM profiles_table WHERE patient_lastname=%s """, lastname) 
            cur.execute(load_query)
            conn.commit()
    
            #data = cur.fetchall()
            #print(data) 
    
            cur.close()
            conn.close()
1
  • Start by breaking the code into multiple lines. First call self.patient_list.curselection() and save the result to a variable. Examine the variable to see if it's what you expect. Then, pass the result to self.patient_list.get. Is the result what you expect? Then, call partitiion, and again examine the result. One of those steps is likely doing something different than you are assuming it's doing. Commented Aug 31, 2020 at 22:09

1 Answer 1

1

load_query is a tuple, so cur.execute(load_query) will raise the exception as execute() expects a string (the query string) as the first argument.

You should change:

load_query = (""" SELECT * FROM profiles_table WHERE patient_lastname=%s """, lastname) 
cur.execute(load_query)

to:

load_query = "SELECT * FROM profiles_table WHERE patient_lastname = %s" 
cur.execute(load_query, (lastname,))

Also SELECT statement does not require commit().

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

3 Comments

Would this work? load_query = " SELECT * FROM profiles_table WHERE patient_lastname=%s ", (lastname,) and then cur.execute(load_query) or is it still a tuple
No it won't work. You can easily verify it by print(type(load_query)).
so basically, even when we dont say () but the values are separated by commas then it is a tuple?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.