Python mysql socket returns tuple

I connect to the mysql database through the mysql connector and run a simple query to pull out a list of identifiers. I need to iterate over this list and pass them to another code. For some reason, I get a list of tuples. Is this expected behavior? If not, what am I doing wrong? Here is a snippet of my code:

import mysql.connector
conn = mysql.connector.connect(host='127.0.0.1', database='t', user='r', password='pwd')
cursor = conn.cursor()
query = ( "select id from T where updated < '%s'" % (run_date) )
cursor.execute(query)
for row in cursor:
   print (row)

cursor.close()

I get the following response (from the INT field to d / b):

(Decimal('991837'),)
(Decimal('991838'),)
(Decimal('991839'),)
(Decimal('991871'),)
(Decimal('991879'),)
(Decimal('991899'),)
(Decimal('992051'),)
(Decimal('992299'),)
(Decimal('992309'),)
+4
source share
2 answers

Yes, this is the expected behavior. Using the cursor as an iteration is basically equivalent to looping over it using the method fetchone(). From the documentation forfetchone() (highlighted by me):

, . , , MySQL, Python. , ;

+2

,

cur = db.cursor( buffered=True , dictionary=True)

:

{'Decimal' : '991837'}

, , , ,

import mysql.connector
conn = mysql.connector.connect(host='127.0.0.1', database='t', user='r', password='pwd')
cursor = conn.cursor()
query = ( "select id from T where updated < '%s'" % (run_date) )
cursor.execute(query)
for row in cursor:
   print (row['Decimal'])

cursor.close()

, , 2 , . , , .
Python :)

0

Source: https://habr.com/ru/post/1628016/


All Articles