I am trying to get the individual field values from a mysql table. I am using a mysql stored procedure (get_data) and a python cursor and I can get the entire record, but I cannot figure out how to get the individual fields so that I can place them into variables.
for example, in the following I want to place Smith into the variable last_name, John into first_name, etc.
cursor = connection.cursor()
cursor.callproc('get_data', [9, ])
for result in cursor.stored_results():
print(result.fetchone()
Results:
(9, 'Smith', 'John', '', '(903) 777-5555. Box 99', 'Somewhere', 'TX', 75999 )
put the fetchone result into a variable and use index to get the value
example
aaa = result.fetchone()
print(aaa[0]) # 9
print(aaa[1]) # Smith
last_name = aaa[1]
first_name = aaa[2]
print(last_name, first_name) # Smith John
reply to comment
cursor = connection.cursor()
cursor.callproc('get_data', [9, ])
for result in cursor.stored_results():
print(result.fetchone()) # (9, 'Smith', 'John', '', '(903) 777-5555. Box 99', 'Somewhere', 'TX', 75999 )
no_id = result.fetchone()[0] # 9
last_name = result.fetchone()[1] # SMith
first_name = result.fetchone()[2] # John
print(f"FirstName: {first_name} | LastName: {last_name}")
result.fetchone()
returns tuple you can get each of fields by using its index
row = result.fetchone()
row[0] #9
row[4] #(903) 652-2038
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.