How can I display a value without using square brackets, commas or parentheses?
P粉726133917
P粉726133917 2023-12-31 11:35:12
0
2
471

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)

P粉726133917
P粉726133917

reply all(2)
P粉156532706

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:

for x in myresult:
  for y in x:
     print(x)

Or, if you're sure it's a single element:

for x in myresult:
   print(x[0])
P粉883973481

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 using x[0]. like this:

for x in myresult:
  print(x[0])

Alternatively, you can pass each element of the tuple as an argument to print() using the * operator. like this:

for x in myresult:
  print(*x)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!