-2

Newbie here. Id like to ask What could possibly wrong with this code:

'SELECT * FROM A3A_SIS.customer_info WHERE cust_name LIKE %' +self.le_ci_search.text()+ '%'

This line returns an error of this:

TypeError: a bytes-like object is required, not 'tuple' I am trying to search a column name where theres a word lopez in it.

UPDATE #1:

I use this code as suggested:

def CustSearch(self):
        search_text = '%{}%'.format(self.le_ci_search.text())

        con = mdb.connect(user='root', passwd='password',
                        host='localhost', database='A3A_SIS')
        with con:
            cur = con.cursor()
            query = ('SELECT * FROM A3A_SIS.customer_info WHERE cust_name LIKE %s', (search_text))

            if cur.execute(query):
                QMessageBox.information(self, "Announcement.","Data was found!")
            else:    
                QMessageBox.information(self, "Announcement.","No data was found!")


        con.close()

I got this error:

Traceback (most recent call last):

File "/Users/anthonygaupo/Desktop/A3ASIS/A3A_Func.py", line 409, in CustSearch if cur.execute(query):

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute self.errorhandler(self, exc, value)

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler raise errorvalue

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute res = self._query(query)

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query rowcount = self._do_query(q)

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query db.query(q)

File "/Users/anthonygaupo/anaconda3/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query _mysql.connection.query(self, query)

TypeError: a bytes-like object is required, not 'tuple'

I am MYSQL workbench

0

1 Answer 1

1

You would need to put the search text, including the % characters, in quotes.

But you should not do this. Assemble the value outside of the SQL statement and use parameter substitution:

query = '%{}%'.format(self.le_ci_search.text())
cursor.execute('SELECT * FROM A3A_SIS.customer_info WHERE cust_name LIKE %s', (query,))

Edit

You're creating a single tuple and passing it to the cursor as the query. What I said to do is to create a string, and pass that plus the parameter to the cursor:

cur = con.cursor()
query = 'SELECT * FROM A3A_SIS.customer_info WHERE cust_name LIKE %s'
if cur.execute(query, (search_text,)):
    ...
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your reply. I am getting this error: TypeError: a bytes-like object is required, not 'tuple' I tried to put str(query) but doesn't work
Please edit your question with the exact code you tried and the full traceback. Also, which MySQL client library are you using?
please check sir. I already edited it. I am using mysql workbench
The code you've shown wouldn't give that error. But you haven't shown either the full code or the full traceback. Plus, "mysql workbench" is an app, not the client library you are using in Python.
Im sorry. im using MySQLdb. Edited again above.
|

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.