2014-03-05 6 views
2

다음 상황을 이해하는 데 도움이 필요합니다. 저장된 mysql 프로 시저를 호출하고 변수에 출력을 저장 한 다음 내용에 액세스하여 텍스트가 있는지 확인하려고합니다. 저는 파이썬에 익숙하지 않고 어떻게해야하는지 이해하는 데 어려움을 겪고 있습니다. 필자는 mysql 쿼리 show slave status; 대신에이 프로 시저를 호출해야합니다. 하지만 기본적으로 함수는 해당 쿼리를 수행합니다. cursor.callproc의 결과에 액세스하는 Python 3.X

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()) 
... 
[] 

내가 대신 cursor.callproc를 사용하는 쿼리를 실행하려고하면

, 나는 다음과 같은 오류가 있습니다

>>> 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 

을 내가 multi=True를 설정하면 그것은 더 나은하지 않습니다.

여기 여기에 기능 출력 내가 MySQL의 콘솔

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) 

에 쿼리를 실행하고하면 제대로 stored_results() 방법을 사용하여하고있는 프로 시저 정의

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) 
+0

'SLAVE STATUS 표시'는 순전히 인쇄용이므로 'callproc'이 볼 수있는 모든 항목을 반환하지 않습니다. 테이블에서 행을 반환하는 프로 시저를 변경하여 검사 할 수 있습니다. –

+1

@JonClements : SHOW SLAVE STATUS (다른 SHOW 문과 마찬가지로)는 MySQL 서버로 전달되어 SELECT와 같은 결과 집합을 반환합니다. 대부분의 SHOW 문은 기본적으로 INFORMATION_SCHEMA에서 선택하는 바로 가기입니다. – geertjanvdk

답변

2

입니다. 여러 문장을 사용하는 것은 필요하지 않습니다. 우리가 그것을지지하지만, 하나씩하는 것이 낫습니다.

결과가 비어있는 것을 보면 단순히 슬레이브 상태가없는 마스터 또는 MySQL에 연결 한 것처럼 보입니다.

I have blogged about getting results after calling a procedure. 아마도이 점을 더 잘 문서화해야합니다.

+0

답장을 보내 주셔서 감사합니다. @geertjanvdk. 비록 이것이 내가 성취하고자하는 것과 여러분의 솔루션 사이의 불일치라고 생각합니다. 'import mysql.connector'를 사용하고 싶습니다.로드 된 상태로 코드를 실행하려고하면 다음과 같이됩니다 :'AttributeError : 'MySQLCursor'객체에 'call'' 속성이 없습니다. –

+0

괜찮습니다.'cursor.callproc ("show_slave_status")', 곧 업데이트 될 것입니다. –

+0

Big up @geertjanvdk. 내 주요한 문제는'print (result.fetchall())'를 사용하는 것인데,이 경우에는 작동하지 않는 것 같습니다 ... 왜 th00, n00b가 너무 많은지 모르겠습니다. –