Pregunta

I need help understanding the following situation. I want to call a stored mysql procedure and store the output in a variable and then access the content to check if some text is present. I'm new to python and having difficulties understanding how I should do it. I absolutely need to call that procedure rather then do the mysql query show slave status;. But basically the function does that query.

Python 3.3.2+ (default, Feb 28 2014, 00:52:16) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>> conn = mysql.connector.connect(host = "xxxx", port = xxxx, user = "xxxx", password = "xxxx", database = "xxxx")
>>> cursor = conn.cursor()
>>> cursor.callproc('show_slave_status')
()
>>> for result in cursor.stored_results():
...     print(result.fetchall())
... 
[]

If I try to run the query instead of using cursor.callproc, I have the following error:

>>> cursor.execute("call show_slave_status")
Traceback (most recent call last):
  File "/usr/local/lib/python3.3/dist-packages/mysql/connector/cursor.py", line 508, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/usr/local/lib/python3.3/dist-packages/mysql/connector/connection.py", line 640, in cmd_query
    'Use cmd_query_iter for statements with multiple queries.')
mysql.connector.errors.InterfaceError: Use cmd_query_iter for statements with multiple queries.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.3/dist-packages/mysql/connector/cursor.py", line 512, in execute
    "Use multi=True when executing multiple statements")
mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements

and if I set multi=True it does not get any better.

here is the function output is I run the query in a mysql console

mysql> call show_slave_status;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
| Slave_IO_State                   | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File   | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
| Waiting for master to send event | localhost   | xxxx        |        xxxx |            60 | mysql-bin.000025 |             5782876 | relay-bin.000075 |       5782979 | mysql-bin.000025      | Yes              | Yes               |                 |                     |                    |                        |                         | xxxx%.%\_NUM\_MEM          |          0 |            |            0 |             5782876 |         5783275 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |             xxxx |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
1 row in set (0.10 sec)

and here is the procedure definition

mysql>  SHOW CREATE PROCEDURE show_slave_status\G
*************************** 1. row ***************************
           Procedure: show_slave_status
            sql_mode: 
    Create Procedure: CREATE DEFINER=`xxxx`@`localhost` PROCEDURE `show_slave_status`()
BEGIN show slave status; END
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.27 sec)
¿Fue útil?

Solución

You are doing it correctly using the stored_results() method. It's not necessary (or wise) to use multiple statements. Although we support it, it's better to do one by one.

Seeing that the result is empty, it looks like you are connecting to a master or a MySQL which has simply no slave status.

I have blogged about getting results after calling a procedure. We probably have to document this better.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top