I just want to print the value without any brackets, commas or parentheses. I'm using MySQL with python and mysql.connector.
When I run this code, I get "('esrvgf',)". But I just want "esrvg".
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="root", password="password", database ="mydatabase" ) cursor = mydb.cursor() sql = "select nick from users where ipaddress = '192.168.1.4'" cursor.execute(sql) myresult = cursor.fetchall() for x in myresult: print(x)
By using .fetchall( ) According to the documentation, you will not return a single element, even if there is only one element:
Therefore, please consider using:
Or, if you're sure it's a single element:
cursor.fetchall()
Returns a list of tuples (see this question), not a string. If you try to print a tuple, Python will add parentheses, if you try to print a list, Python will add parentheses. All you need to do is print the first element usingx[0]
. like this:Alternatively, you can pass each element of the tuple as an argument to
print()
using the*
operator. like this: