0

i am retrieving some results from a mysql database and they come in a form separated by brackets. I want to remove the brackets, i have used the strip function but that will only work for when i have one result to display.

The code below will only remove brackets for search results that only have one element.

@QtCore.pyqtSignature("on_pushButton_clicked()")
def searchDBnumber(self):
    searchName = self.searchInput.toPlainText() 

    if len(searchName) != 0:
        searchForName = ("""SELECT number FROM test_table WHERE name =""" + "'"+ searchName +"'")



        cursor.execute(searchForName)

        result = cursor.fetchall()

        result = str(result).strip('[](),')
        self.number.setPlainText(result)
3
  • 2
    Try: ', '.join(map(repr, result)). Can include the output of print(result) in your question? Commented Jan 27, 2017 at 15:08
  • 1
    Don't ever do this: name =""" + "'"+ searchName +"'" when forming SQL queries. Here lies the path to lil Bobby Tables. Use placeholders and pass your arguments to cursor.execute. Commented Jan 27, 2017 at 15:15
  • About "the form", fetchall returns a list of tuples (usually). You should format that using str.join and str.format (for example) to suit your needs, as pointed out by @MYGz. Commented Jan 27, 2017 at 15:25

3 Answers 3

2

I think the simple way is using extend

result = []
for i in c.fetchall()
    result.extend(i)
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
1

After some playing around I found that the results you get back from a mysql query aren't string but tuples so simply trying to remove the brackets doesn't work.

Here's is my example of how I dealt with it, starting with the query:

mycursor.execute("SELECT name1, name2 FROM testdatabase where (name1 REGEXP '^test.*') ")

I can then loop through the results and print each one like this:

for result in query_results:
    print(result)

Which gives some results like this:

('test0', 'something0')
('test1', 'something1')
('testing0', 'somethingelse1')

But I wanted to separate each of those results into both fields so you can do something like this:

for result in query_results:
    part1, part2 = result
    print('{} - {}'.format(part1, part2))

which will give you output like this:

test0 - something0
test1 - something1
testing0 - somethingelse1

Rather than just printing these out the plan for my code is to create a json type dictionary that I can easily search.

Comments

0

If you want to simply remove all brackets from a result, you can use replace() method. The syntax is string.replace(string_to_replace, string_to_replace_with).

eg:

result = str(result).replace("()", "")

This will need to be done for each bracket type, I believe. So another round for "[]".

Note that after you remove all brackets, splitting a result set to individual rows might be hard if the brackets are the delimiters.

Comments

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.