1

I am trying to call a MySQL stored procedure that returns a single row, I can get the values back back using cursor.callproc() and cursor.stored_results(), but I can't get name of the field back 'fileHash'

If I call the SP directly I get results such as:

mysql> call GetAllFileHashesForDirectory('/myroot/');
+------------------+
| fileHash         |
+------------------+
| testa            |
| testb            |
+------------------+
2 rows in set (0.00 sec)

But, when I try and get the field name I get the following instead of 'fileHash':

(u'@_GetAllFileHashesForDirectory_arg1',)

The python code I use to get the value is:

self.__mySQLCursor.column_names

Do I need to specify the name in a stored procedure? I am really confused.

0
0

You can put logic in the stored procedure to SELECT 'fileHash' and then UNION that with the query that returns the other values from the other query in the stored procedure and ensure the very first row returned is fileHash

SELECT 'fileHash'
UNION 
SELECT <column> FROM <table>

Output

+------------------+
| fileHash         |
+------------------+
| fileHash         |
| testa            |
| testb            |
+------------------+
1

sorry for the late answer, but I really do not think the chosen answer is good advice, the python mysql module has a method for this already, no need to do weird manips in the ddb. For this, just do:

cursor.callproc('sql_procedure', arguments)
for colid in cursor.stored_results():
    columnsProperties = (colid.description)
    print ([column[0] for column in columnsProperties]) 

you get all properties of the column in colid.description, the first property is the filename which is what you're after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.