Python Forum
Trouble retrieving dictionary from mysql.connector cursor
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble retrieving dictionary from mysql.connector cursor
#1
I'm working on a script to report disk space with raw data stored in a MySQL table. I'm trying to retrieve the results of a SQL query into a dictionary. Here's the relevant code:

try: dbh = mysql.connector.connect(user = DB_USER, password = DB_PASS, host = DB_INSTANCE, database = DB_DATABASE) cursor = dbh.cursor(cursor_class = MySQLCursorPrepared, dictionary=True) except mysql.connector.Error as err: print(f"Error connecting to instance {DB_INSTANCE}, database {DB_DATABASE}") if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: print("Bad username or password") exit(1) elif err.errno == errorcode.ER_BAD_DB_ERROR: print("Database does not exist") exit(1) else: print(err) exit(1) def check_output(upper, lower): query = 'select host,mount_point,date_time,allocation, (avail/allocation) * 100 "percent" ' + \ 'from space_usage su0 where su0.date_time = (select max(date_time) from space_usage su1 ' + \ 'where su0.host = su1.host and su0.mount_point = su1.mount_point) and ' +\ '((avail/allocation) * 100) < ? and ((avail/allocation) * 100) >= ? and ' +\ 'date_time >= date_sub(now(), interval 2 day) ' +\ 'order by host, mount_point' try: cursor.execute(query, (upper, lower)) except mysql.connector.Error as err: print(f"Error running query '{query}':\n{err}") exit(1) # dboutput = cursor.fetchall() if cursor: error = 1 print(f'The following filesystems are above {upper}% used:\n') print(header1) print(header2) for row in cursor: print(formatstr, (row['host'], row['filesystem'], row['mount_point'], row['allocation'], \ row['avail'], (row['avail']/row['allocation']))) print('')
But when I run it, I get this error (line numbers munged):
Output:
Traceback (most recent call last): File "./check_disk_usage", line xx, in <module> check_output(crit, 0) File "./check_disk_usage", line 37, in check_output print(formatstr, (row['host'], row['filesystem'], row['mount_point'], row['allocation'], \ TypeError: tuple indices must be integers or slices, not str
Reply
#2
check this: https://bugs.mysql.com/bug.php?id=92700
https://stackoverflow.com/a/52692714/4046632
It looks combination of MySQLCursorPrepared cursor to return dictionary is not supported
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks, I suspected it might be something like that. I changed the code to access the results as a list instead.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  MariaDB Connector/Python; version mismatch shopgeek 1 1,429 Feb-24-2025, 05:06 AM
Last Post: from1991
  [SOLVED] Retrieving the filename from a shell jehoshua 3 1,371 Nov-29-2024, 01:25 AM
Last Post: jehoshua
Question No disconnect method for snowflake.connector ? Calab 0 1,320 Jun-11-2024, 09:42 PM
Last Post: Calab
  Help with cursor in terminal river251 2 2,155 May-23-2024, 10:40 AM
Last Post: perfringo
  KeyError while retrieving ESPN data john317ab 2 3,872 Nov-29-2023, 09:07 PM
Last Post: john317ab
  Mysql and mysql.connector error lostintime 2 2,505 Oct-03-2023, 10:25 PM
Last Post: lostintime
  .get() not retrieving value? Sedos101 2 1,859 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  Cursor Variable inside Another Cursor . CX_ORacle paulo79 1 2,978 Apr-09-2022, 10:24 AM
Last Post: ibreeden
  [Solved] Retrieving a pdf from sqlite3 BigMan 4 5,331 Mar-12-2022, 01:56 PM
Last Post: deanhystad
  Mysql error message: Lost connection to MySQL server during query tomtom 6 24,120 Feb-09-2022, 09:55 AM
Last Post: ibreeden

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.