PageRenderTime 103ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/resources/lib/mysql-connector-python/docs/mysql-connector-python.html

https://github.com/wtmmac/mythbox
HTML | 2302 lines | 2295 code | 7 blank | 0 comment | 0 complexity | b7e5095c4d72b94bace63b59d34bdc5a MD5 | raw file
Possible License(s): Unlicense, AGPL-1.0, BSD-2-Clause, GPL-2.0, MIT, Apache-2.0
  1. <html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>MySQL Connector/Python</title><link rel="stylesheet" type="text/css" href="mysql-html.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1">
  2. <script language="javascript" type="text/javascript">
  3. function addOnload(theFunc)
  4. {
  5. var previous = window.onload;
  6. if (typeof window.onload != 'function')
  7. {
  8. window.onload = theFunc;
  9. }
  10. else
  11. {
  12. window.onload = function()
  13. {
  14. previous();
  15. theFunc();
  16. }
  17. }
  18. }
  19. addOnload(function()
  20. {
  21. var base = new Date(1355755772*1000);
  22. var now = new Date();
  23. var diff = ((now-base)/1000)/(24*3600);
  24. if (diff > 90) {
  25. var nodes = document.getElementsByClassName('titlepage');
  26. nodes[0].innerHTML = '<p style="border: 5px #ff0000 solid; padding: 5px; margin 5px">' +
  27. 'This copy of the manual is more than 90 days old. We encourage you to download a ' +
  28. 'new version from <a href="http://dev.mysql.com">dev.mysql.com/doc</a>.</p>' + nodes[0].innerHTML;
  29. }
  30. });
  31. </script>
  32. <noscript></noscript>
  33. </head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div lang="en" class="book"><div class="titlepage"><div><div><h1 class="title"><a name="connectors"></a>MySQL Connector/Python</h1></div><div><div class="abstract"><p class="title"><b>Abstract</b></p><p>
  34. This manual describes how to install, configure, and develop
  35. database applications using MySQL Connector/Python, a
  36. self-contained Python driver for communicating with MySQL
  37. servers.
  38. </p><p>
  39. Document generated on:
  40. 2012-12-17
  41. (revision: 33584)
  42. </p></div></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="preface"><a href="#preface">Preface and Legal Notices</a></span></dt><dt><span class="chapter"><a href="#connector-python">1. MySQL Connector/Python</a></span></dt><dt><span class="chapter"><a href="#connector-python-versions">2. Connector/Python Versions</a></span></dt><dt><span class="chapter"><a href="#connector-python-install">3. Connector/Python Installation</a></span></dt><dd><dl><dt><span class="section"><a href="#connector-python-installation-src-unix">3.1. Installing Connector/Python Source Distribution on Linux, UNIX, or OS X</a></span></dt><dt><span class="section"><a href="#connector-python-installation-src-windows">3.2. Installing Connector/Python Source Distribution on Microsoft Windows</a></span></dt><dt><span class="section"><a href="#connector-python-verification">3.3. Verifying Your Connector/Python Installation</a></span></dt></dl></dd><dt><span class="chapter"><a href="#connector-python-examples">4. Connector/Python Coding Examples</a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_example_connecting">4.1. Connecting to MySQL Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_ddl">4.2. Creating Tables Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_cursor_transaction">4.3. Inserting Data Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_cursor_select">4.4. Querying Data Using Connector/Python</a></span></dt></dl></dd><dt><span class="chapter"><a href="#connector-python-tutorials">5. Connector/Python Tutorials</a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_tutorial_CursorBuffered_GiveRaise">5.1. Tutorial: Raise employee's salary using a buffering cursor</a></span></dt></dl></dd><dt><span class="chapter"><a href="#connector-python-connectargs">6. Connector/Python Connection Arguments</a></span></dt><dt><span class="chapter"><a href="#connector-python-reference">7. Connector/Python API Reference</a></span></dt><dd><dl><dt><span class="section"><a href="#connector-python-apiref-errors">7.1. Errors and Exceptions</a></span></dt><dd><dl><dt><span class="section"><a href="#connector-python-apiref-errorcode">7.1.1. Module <code class="literal">errorcode</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-error">7.1.2. Exception <code class="literal">errors.Error</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-warning">7.1.3. Exception <code class="literal">errors.Warning</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-interfaceerror">7.1.4. Exception <code class="literal">errors.InterfaceError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-databaseerror">7.1.5. Exception <code class="literal">errors.DatabaseError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-internalerror">7.1.6. Exception <code class="literal">errors.InternalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-operationalerror">7.1.7. Exception <code class="literal">errors.OperationalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-programmingerror">7.1.8. Exception <code class="literal">errors.ProgrammingError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-integrityerror">7.1.9. Exception <code class="literal">errors.IntegrityError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-dataerror">7.1.10. Exception <code class="literal">errors.DataError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-notsupportederror">7.1.11. Exception <code class="literal">errors.NotSupportedError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-custom-error-exception">7.1.12. Function <code class="literal">errors.custom_error_exception(error=None,
  43. exception=None)</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLConnection">7.2. Class <code class="literal">connection.MySQLConnection</code></a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_MySQLConnection">7.2.1. Constructor <code class="literal">connection.MySQLConnection(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_close">7.2.2. Method <code class="literal">MySQLConnection.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_config">7.2.3. Method <code class="literal">MySQLConnection.config(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connect">7.2.4. Method <code class="literal">MySQLConnection.connect(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_commit">7.2.5. Method <code class="literal">MySQLConnection.commit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cursor">7.2.6. Method <code class="literal">MySQLConnection.cursor(buffered=None, raw=None,
  44. cursor_class=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_change_user">7.2.7. Method <code class="literal">MySQLConnection.cmd_change_user(username='',
  45. password='', database='', charset=33)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_debug">7.2.8. Method <code class="literal">MySQLConnection.cmd_debug()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_init_db">7.2.9. Method <code class="literal">MySQLConnection.cmd_init_db(database)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_ping">7.2.10. Method <code class="literal">MySQLConnection.cmd_ping()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_info">7.2.11. Method <code class="literal">MySQLConnection.cmd_process_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_kill">7.2.12. Method <code class="literal">MySQLConnection.cmd_process_kill(mysql_pid)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_quit">7.2.13. Method <code class="literal">MySQLConnection.cmd_quit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query">7.2.14. Method <code class="literal">MySQLConnection.cmd_query(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query_iter">7.2.15. Method <code class="literal">MySQLConnection.cmd_query_iter(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_refresh">7.2.16. Method <code class="literal">MySQLConnection.cmd_refresh(options)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_shutdown">7.2.17. Method <code class="literal">MySQLConnection.cmd_shutdown()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_statistics">7.2.18. Method <code class="literal">MySQLConnection.cmd_statistics()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_disconnect">7.2.19. Method <code class="literal">MySQLConnection.disconnect()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_rows">7.2.20. Method <code class="literal">MySQLConnection.get_rows(count=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_row">7.2.21. Method <code class="literal">MySQLConnection.get_row()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_info">7.2.22. Method <code class="literal">MySQLConnection.get_server_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_version">7.2.23. Method <code class="literal">MySQLConnection.get_server_version()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_is_connected">7.2.24. Method <code class="literal">MySQLConnection.is_connected()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_isset_client_flag">7.2.25. Method <code class="literal">MySQLConnection.isset_client_flag(flag)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_ping">7.2.26. Method <code class="literal">MySQLConnection.ping(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnectoin_reconnect">7.2.27. Method <code class="literal">MySQLConnection.reconnect(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_rollback">7.2.28. Method <code class="literal">MySQLConnection.rollback()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_charset_collation">7.2.29. Method <code class="literal">MySQLConnection.set_charset_collation(charset=None,
  46. collation=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_client_flags">7.2.30. Method <code class="literal">MySQLConnection.set_client_flags(flags)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_autocommit">7.2.31. Property <code class="literal">MySQLConnection.autocommit</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_charset_name">7.2.32. Property <code class="literal">MySQLConnection.charset_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_collation_name">7.2.33. Property <code class="literal">MySQLConnection.collation_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connection_id">7.2.34. Property <code class="literal">MySQLConnection.connection_id</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_database">7.2.35. Property <code class="literal">MySQLConnection.database</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_warnings">7.2.36. Property <code class="literal">MySQLConnection.get_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_raise_on_warnings">7.2.37. Property <code class="literal">MySQLConnection.raise_on_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_host">7.2.38. Property <code class="literal">MySQLConnection.server_host</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_port">7.2.39. Property <code class="literal">MySQLConnection.server_port</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_sql_mode">7.2.40. Property <code class="literal">MySQLConnection.sql_mode</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_time_zone">7.2.41. Property <code class="literal">MySQLConnection.time_zone</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_unix_socket">7.2.42. Property <code class="literal">MySQLConnection.unix_socket</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_user">7.2.43. Property <code class="literal">MySQLConnection.user</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLCursor">7.3. Class <code class="literal">cursor.MySQLCursor</code></a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_MySQLCursor">7.3.1. Constructor <code class="literal">cursor.MySQLCursor</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_callproc">7.3.2. Method <code class="literal">MySQLCursor.callproc(procname, args=())</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_close">7.3.3. Method <code class="literal">MySQLCursor.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_execute">7.3.4. Method <code class="literal">MySQLCursor.execute(operation, params=None,
  47. multi=False)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_executemany">7.3.5. Method <code class="literal">MySQLCursor.executemany(operation, seq_params)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchall">7.3.6. Method <code class="literal">MySQLCursor.fetchall()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchmany">7.3.7. Method <code class="literal">MySQLCursor.fetchmany(size=1)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchone">7.3.8. Method <code class="literal">MySQLCursor.fetchone()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchwarnings">7.3.9. Method <code class="literal">MySQLCursor.fetchwarnings()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_stored_results">7.3.10. Method <code class="literal">MySQLCursor.stored_results()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_column_names">7.3.11. Property <code class="literal">MySQLCursor.column_names</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_statement">7.3.12. Property <code class="literal">MySQLCursor.statement</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_with_rows">7.3.13. Property <code class="literal">MySQLCursor.with_rows</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLCursorBuffered">7.4. Class <code class="literal">cursor.MySQLCursorBuffered</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-ClientFlag">7.5. Class <code class="literal">constants.ClientFlag</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-FieldType">7.6. Class <code class="literal">constants.FieldType</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-SQLMode">7.7. Class <code class="literal">constants.SQLMode</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-CharacterSet">7.8. Class <code class="literal">constants.CharacterSet</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-RefreshOption">7.9. Class <code class="literal">constants.RefreshOption</code></a></span></dt></dl></dd><dt><span class="chapter"><a href="#cpython-news">8. MySQL Connector/Python Change History</a></span></dt><dd><dl><dt><span class="section"><a href="#cpython-news-1-0-8">8.1. Changes in MySQL Connector/Python 1.0.8 (Not yet released, Generally Available)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-7">8.2. Changes in MySQL Connector/Python 1.0.7 (29 September 2012, Generally Available)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-6">8.3. Changes in MySQL Connector/Python 1.0.6 (30 August 2012, beta)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-5">8.4. Changes in MySQL Connector/Python 1.0.5 (17 July 2012, beta)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-4">8.5. Changes in MySQL Connector/Python 1.0.4 (07 July 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-3">8.6. Changes in MySQL Connector/Python 1.0.3 (08 June 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-2">8.7. Changes in MySQL Connector/Python 1.0.2 (19 May 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-1">8.8. Changes in MySQL Connector/Python 1.0.1 (26 April 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-0">8.9. Changes in MySQL Connector/Python 1.0.0 (22 April 2012, alpha)</a></span></dt></dl></dd></dl></div><div class="preface"><div class="titlepage"><div><div><h1 class="title"><a name="preface"></a>Preface and Legal Notices</h1></div></div></div><p>
  48. This manual describes how to install, configure, and develop
  49. database applications using MySQL Connector/Python, the a
  50. self-contained Python driver for communicating with MySQL servers.
  51. </p><h2><a name="legalnotice"></a>Legal Notices</h2><p>
  52. Copyright © 2012, Oracle and/or its affiliates. All rights
  53. reserved.
  54. </p><p>
  55. This software and related documentation are provided under a license
  56. agreement containing restrictions on use and disclosure and are
  57. protected by intellectual property laws. Except as expressly
  58. permitted in your license agreement or allowed by law, you may not
  59. use, copy, reproduce, translate, broadcast, modify, license,
  60. transmit, distribute, exhibit, perform, publish, or display any
  61. part, in any form, or by any means. Reverse engineering,
  62. disassembly, or decompilation of this software, unless required by
  63. law for interoperability, is prohibited.
  64. </p><p>
  65. The information contained herein is subject to change without notice
  66. and is not warranted to be error-free. If you find any errors,
  67. please report them to us in writing.
  68. </p><p>
  69. If this software or related documentation is delivered to the U.S.
  70. Government or anyone licensing it on behalf of the U.S. Government,
  71. the following notice is applicable:
  72. </p><p>
  73. U.S. GOVERNMENT RIGHTS Programs, software, databases, and related
  74. documentation and technical data delivered to U.S. Government
  75. customers are "commercial computer software" or "commercial
  76. technical data" pursuant to the applicable Federal Acquisition
  77. Regulation and agency-specific supplemental regulations. As such,
  78. the use, duplication, disclosure, modification, and adaptation shall
  79. be subject to the restrictions and license terms set forth in the
  80. applicable Government contract, and, to the extent applicable by the
  81. terms of the Government contract, the additional rights set forth in
  82. FAR 52.227-19, Commercial Computer Software License (December 2007).
  83. Oracle USA, Inc., 500 Oracle Parkway, Redwood City, CA 94065.
  84. </p><p>
  85. This software is developed for general use in a variety of
  86. information management applications. It is not developed or intended
  87. for use in any inherently dangerous applications, including
  88. applications which may create a risk of personal injury. If you use
  89. this software in dangerous applications, then you shall be
  90. responsible to take all appropriate fail-safe, backup, redundancy,
  91. and other measures to ensure the safe use of this software. Oracle
  92. Corporation and its affiliates disclaim any liability for any
  93. damages caused by use of this software in dangerous applications.
  94. </p><p>
  95. Oracle is a registered trademark of Oracle Corporation and/or its
  96. affiliates. MySQL is a trademark of Oracle Corporation and/or its
  97. affiliates, and shall not be used without Oracle's express written
  98. authorization. Other names may be trademarks of their respective
  99. owners.
  100. </p><p>
  101. This software and documentation may provide access to or information
  102. on content, products, and services from third parties. Oracle
  103. Corporation and its affiliates are not responsible for and expressly
  104. disclaim all warranties of any kind with respect to third-party
  105. content, products, and services. Oracle Corporation and its
  106. affiliates will not be responsible for any loss, costs, or damages
  107. incurred due to your access to or use of third-party content,
  108. products, or services.
  109. </p><p>
  110. This documentation is in prerelease status and is intended for
  111. demonstration and preliminary use only. It may not be specific to
  112. the hardware on which you are using the software. Oracle Corporation
  113. and its affiliates are not responsible for and expressly disclaim
  114. all warranties of any kind with respect to this documentation and
  115. will not be responsible for any loss, costs, or damages incurred due
  116. to the use of this documentation.
  117. </p><p>
  118. The information contained in this document is for informational
  119. sharing purposes only and should be considered in your capacity as a
  120. customer advisory board member or pursuant to your beta trial
  121. agreement only. It is not a commitment to deliver any material,
  122. code, or functionality, and should not be relied upon in making
  123. purchasing decisions. The development, release, and timing of any
  124. features or functionality described in this document remains at the
  125. sole discretion of Oracle.
  126. </p><p>
  127. This document in any form, software or printed matter, contains
  128. proprietary information that is the exclusive property of Oracle.
  129. Your access to and use of this material is subject to the terms and
  130. conditions of your Oracle Software License and Service Agreement,
  131. which has been executed and with which you agree to comply. This
  132. document and information contained herein may not be disclosed,
  133. copied, reproduced, or distributed to anyone outside Oracle without
  134. prior written consent of Oracle or as specifically provided below.
  135. This document is not part of your license agreement nor can it be
  136. incorporated into any contractual agreement with Oracle or its
  137. subsidiaries or affiliates.
  138. </p><p>
  139. This documentation is NOT distributed under a GPL license. Use of
  140. this documentation is subject to the following terms:
  141. </p><p>
  142. You may create a printed copy of this documentation solely for your
  143. own personal use. Conversion to other formats is allowed as long as
  144. the actual content is not altered or edited in any way. You shall
  145. not publish or distribute this documentation in any form or on any
  146. media, except if you distribute the documentation in a manner
  147. similar to how Oracle disseminates it (that is, electronically for
  148. download on a Web site with the software) or on a CD-ROM or similar
  149. medium, provided however that the documentation is disseminated
  150. together with the software on the same medium. Any other use, such
  151. as any dissemination of printed copies or use of this documentation,
  152. in whole or in part, in another publication, requires the prior
  153. written consent from an authorized representative of Oracle. Oracle
  154. and/or its affiliates reserve any and all rights to this
  155. documentation not expressly granted above.
  156. </p><p>
  157. For more information on the terms of this license, or for details on
  158. how the MySQL documentation is built and produced, please visit
  159. <a class="ulink" href="http://dev.mysql.com/contact/" target="_top">MySQL Contact &amp;
  160. Questions</a>.
  161. </p><p>
  162. For additional licensing information, including licenses for
  163. third-party libraries used by MySQL products, see
  164. <a class="xref" href="#preface" title="Preface and Legal Notices">Preface and Legal Notices</a>.
  165. </p><p>
  166. For help with using MySQL, please visit either the
  167. <a class="ulink" href="http://forums.mysql.com" target="_top">MySQL Forums</a> or
  168. <a class="ulink" href="http://lists.mysql.com" target="_top">MySQL Mailing Lists</a>
  169. where you can discuss your issues with other MySQL users.
  170. </p><p>
  171. For additional documentation on MySQL products, including
  172. translations of the documentation into other languages, and
  173. downloadable versions in variety of formats, including HTML and PDF
  174. formats, see the <a class="ulink" href="http://dev.mysql.com/doc" target="_top">MySQL
  175. Documentation Library</a>.
  176. </p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python"></a>Chapter 1. MySQL Connector/Python</h1></div></div></div><a class="indexterm" name="id498227"></a><a class="indexterm" name="id498233"></a><a class="indexterm" name="id498240"></a><p>
  177. MySQL Connector/Python allows Python programs to access MySQL
  178. databases, using an API that is compliant with the
  179. <a class="ulink" href="http://www.python.org/dev/peps/pep-0249/" target="_top">Python DB API
  180. version 2.0</a>. It is written in pure Python and does not have
  181. any dependencies except for the
  182. <a class="ulink" href="http://docs.python.org/library/" target="_top">Python Standard
  183. Library</a>.
  184. </p><p>
  185. MySQL Connector/Python includes support for:
  186. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  187. Almost all features provided by MySQL Server up to and including
  188. MySQL Server version 5.5.
  189. </p></li><li class="listitem"><p>
  190. Converting parameter values back and forth between Python and
  191. MySQL data types, for example Python <code class="literal">datetime</code>
  192. and MySQL <code class="literal">DATETIME</code>. You can turn automatic
  193. conversion on for convenience, or off for optimal performance.
  194. </p></li><li class="listitem"><p>
  195. All MySQL extensions to standard SQL syntax.
  196. </p></li><li class="listitem"><p>
  197. Protocol compression, which enables compressing the data stream
  198. between the client and server.
  199. </p></li><li class="listitem"><p>
  200. Connections using TCP/IP sockets and on Unix using Unix sockets.
  201. </p></li><li class="listitem"><p>
  202. Secure TCP/IP connections using SSL.
  203. </p></li><li class="listitem"><p>
  204. Self-contained driver. Connector/Python does not require the
  205. MySQL client library or any Python modules outside the standard
  206. library.
  207. </p></li></ul></div><p>
  208. MySQL Connector/Python supports from Python version 2.4 through 2.7,
  209. and Python 3.1 and later. Note that Connector/Python does not
  210. support the old MySQL Server authentication methods, which means
  211. that MySQL versions prior to 4.1 will not work.
  212. </p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-versions"></a>Chapter 2. Connector/Python Versions</h1></div></div></div><p>
  213. MySQL Connector/Python v1.0.x series went going through a series of
  214. beta releases, leading to the first generally available (GA) version
  215. 1.0.7. Any development releases prior to general availability are
  216. not supported now that the GA version is released.
  217. </p><p>
  218. The following table summarizes the available Connector/Python
  219. versions:
  220. </p><div class="table"><a name="id498371"></a><p class="title"><b>Table 2.1. Connector/Python Version Reference</b></p><div class="table-contents"><table summary=" For each Connector/Python version, this table lists the
  221. corresponding MySQL Server versions that it works with, the
  222. corresponding versions of Python that it works with, and whether
  223. it is pre-release, generally available (supported), or obsolete.
  224. MySQL server and Python versions within brackets are known to work
  225. with Connector/Python, but are not officially supported. Bugs
  226. might not get fixed for those versions. " border="1"><colgroup><col><col><col><col></colgroup><thead><tr><th scope="col">Connector/Python Version</th><th scope="col">MySQL Server Versions</th><th scope="col">Python Versions</th><th scope="col">Support Status for Connector</th></tr></thead><tbody><tr><td scope="row">1.0</td><td>5.6, 5.5 (5.1, 5.0, 4.1)</td><td>2.7, 2.6 (2.5, 2.4); 3.1 and later</td><td>Recommended version</td></tr></tbody></table></div></div><br class="table-break"><div xmlns="http://www.w3.org/1999/xhtml" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><div class="admon-title">Note</div><p xmlns="">
  227. MySQL server and Python versions within brackets are known to work
  228. with Connector/Python, but are not officially supported. Bugs
  229. might not get fixed for those versions.
  230. </p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-install"></a>Chapter 3. Connector/Python Installation</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#connector-python-installation-src-unix">3.1. Installing Connector/Python Source Distribution on Linux, UNIX, or OS X</a></span></dt><dt><span class="section"><a href="#connector-python-installation-src-windows">3.2. Installing Connector/Python Source Distribution on Microsoft Windows</a></span></dt><dt><span class="section"><a href="#connector-python-verification">3.3. Verifying Your Connector/Python Installation</a></span></dt></dl></div><p>
  231. Connector/Python runs on any platform where Python is installed.
  232. Python comes pre-installed on almost any Linux distribution or
  233. UNIX-like system such as Apple Mac OS X and FreeBSD. On Microsoft
  234. Windows systems, you can install Python using the installer found on
  235. the <a class="ulink" href="http://python.org/download/" target="_top">Python Download
  236. website</a>.
  237. </p><p>
  238. Connector/Python is a pure Python implementation of the MySQL
  239. Client/Server protocol, meaning it does not require any other MySQL
  240. client libraries or other components. It also has no third-party
  241. dependencies. If you need SSL support, verify that your Python
  242. installation has been compiled using the
  243. <a class="ulink" href="http://www.openssl.org/" target="_top">OpenSSL</a> libraries.
  244. </p><p>
  245. The installation of Connector/Python is similar on every platform
  246. and follows the standard
  247. <a class="ulink" href="http://docs.python.org/install/index.html#install-index" target="_top">Python
  248. Distribution Utilities</a> or
  249. <a class="ulink" href="http://docs.python.org/distutils/" target="_top">Distutils</a>.
  250. Some platforms have specific packaging, for example RPM, and, when
  251. made available, the installation of these will be covered in this
  252. manual.
  253. </p><p>
  254. Python terminology regarding distributions:
  255. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  256. <span class="bold"><strong>Source Distribution</strong></span> is a
  257. distribution that contains only source files and is generally
  258. platform independent.
  259. </p></li><li class="listitem"><p>
  260. <span class="bold"><strong>Built Distribution</strong></span> can be
  261. regarded as a binary package. It contains both sources and
  262. platform-independent bytecode.
  263. </p></li></ul></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-installation-src-unix"></a>3.1. Installing Connector/Python Source Distribution on Linux, UNIX, or OS X</h2></div></div></div><p>
  264. On UNIX-like systems such as Linux distributions, Solaris, Apple
  265. Mac OS X, and FreeBSD, you can download Connector/Python as a
  266. <code class="literal">tar</code> archive from
  267. <a class="ulink" href="http://dev.mysql.com/downloads/connector/python/" target="_top">http://dev.mysql.com/downloads/connector/python/</a>.
  268. </p><p>
  269. To install Connector/Python from the <code class="literal">.tar.gz</code>
  270. file, download the latest version and follow these steps:
  271. </p><pre class="programlisting">shell&gt; <strong class="userinput"><code>gunzip mysql-connector-python-1.0.6b1.tar.gz</code></strong>
  272. shell&gt; <strong class="userinput"><code>tar xf mysql-connector-python-1.0.6b1.tar</code></strong>
  273. shell&gt; <strong class="userinput"><code>cd mysql-connector-python-1.0.6b1</code></strong>
  274. shell&gt; <strong class="userinput"><code>sudo python setup.py install</code></strong>
  275. </pre><p>
  276. On UNIX-like systems, Connector/Python gets installed in the
  277. default location
  278. <code class="filename">/<em class="replaceable"><code>prefix</code></em>/lib/python<em class="replaceable"><code>X.Y</code></em>/site-packages/</code>,
  279. where <em class="replaceable"><code>prefix</code></em> is the location where
  280. Python was installed and <em class="replaceable"><code>X.Y</code></em> is the
  281. version of Python. See
  282. <a class="ulink" href="http://docs.python.org/install/index.html#how-installation-works" target="_top">How
  283. installation works</a> in the Python manual.
  284. </p><p>
  285. If you are not sure where Connector/Python was installed, do the
  286. following to retrieve the location:
  287. </p><pre class="programlisting">shell&gt; python
  288. &gt;&gt;&gt; from distutils.sysconfig import get_python_lib
  289. &gt;&gt;&gt; print get_python_lib() # Python v2.x
  290. /Library/Python/2.7/site-packages
  291. &gt;&gt;&gt; print(get_python_lib()) # Python v3.x
  292. /Library/Frameworks/Python.framework/Versions/3.1/lib/python3.1/site-packages</pre><div xmlns="http://www.w3.org/1999/xhtml" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><div class="admon-title">Note</div><p xmlns="">
  293. The above example shows the default installation location on Mac
  294. OS X 10.7.
  295. </p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-installation-src-windows"></a>3.2. Installing Connector/Python Source Distribution on Microsoft Windows</h2></div></div></div><p>
  296. On Microsoft Windows systems, you can download Connector/Python as
  297. a <code class="literal">zip</code> archive from
  298. <a class="ulink" href="http://dev.mysql.com/downloads/connector/python/" target="_top">http://dev.mysql.com/downloads/connector/python/</a>.
  299. </p><p>
  300. Make sure that the Python executable is available in the Windows
  301. <code class="literal">%PATH%</code> setting. For more information about
  302. installation and configuration of Python on Windows, see the
  303. section
  304. <a class="ulink" href="http://docs.python.org/using/windows.html" target="_top">Using
  305. Python on Windows</a> in the Python documentation.
  306. </p><p>
  307. To install Connector/Python from the <code class="literal">.zip</code> file,
  308. download the latest version and follow these steps:
  309. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>
  310. Unpack the downloaded <code class="literal">zip</code> archive into a
  311. directory of your choice. For example, into the folder
  312. <code class="filename">C:\mysql-connector\</code>. Use the appropriate
  313. unzip command for your system, for example,
  314. <code class="literal">unzip</code>, <code class="literal">pkunzip</code>, and so
  315. on.
  316. </p></li><li class="listitem"><p>
  317. Start a console window (or a DOS window) and change to the
  318. folder where you unpacked the Connector/Python
  319. <code class="literal">zip</code> archive.
  320. </p><pre class="programlisting">shell&gt; <strong class="userinput"><code>cd C:\mysql-connector\</code></strong>
  321. </pre></li><li class="listitem"><p>
  322. Once inside the Connector/Python folder, do the following:
  323. </p><pre class="programlisting">shell&gt; <strong class="userinput"><code>python setup.py install</code></strong>
  324. </pre></li></ol></div><p>
  325. On Windows, Connector/Python gets installed in the default
  326. location
  327. <code class="filename">C:\Python<em class="replaceable"><code>X.Y</code></em>\Lib\site-packages\</code>
  328. where <em class="replaceable"><code>X.Y</code></em> is the Python version you
  329. used to install the connector.
  330. </p><p>
  331. If you are not sure where Connector/Python ended up, do the
  332. following to retrieve the location where packages get installed:
  333. </p><pre class="programlisting">shell&gt; python
  334. &gt;&gt;&gt; from distutils.sysconfig import get_python_lib
  335. &gt;&gt;&gt; print get_python_lib() # Python v2.x
  336. &gt;&gt;&gt; print(get_python_lib()) # Python v3.x</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-verification"></a>3.3. Verifying Your Connector/Python Installation</h2></div></div></div><p>
  337. To test that your Connector/Python installation is working and is
  338. able to connect to a MySQL database server, you can run a very
  339. simple program where you substitute the login credentials and host
  340. information of the MySQL server. See
  341. <a class="xref" href="#myconnpy_example_connecting" title="4.1. Connecting to MySQL Using Connector/Python">Section 4.1, Connecting to MySQL Using Connector/Python</a> for an example.
  342. </p></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-examples"></a>Chapter 4. Connector/Python Coding Examples</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#myconnpy_example_connecting">4.1. Connecting to MySQL Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_ddl">4.2. Creating Tables Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_cursor_transaction">4.3. Inserting Data Using Connector/Python</a></span></dt><dt><span class="section"><a href="#myconnpy_example_cursor_select">4.4. Querying Data Using Connector/Python</a></span></dt></dl></div><p>
  343. These coding examples illustrate how to develop Python applications
  344. and scripts which connect to a MySQL Server using MySQL
  345. Connector/Python.
  346. </p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="myconnpy_example_connecting"></a>4.1. Connecting to MySQL Using Connector/Python</h2></div></div></div><p>
  347. The <code class="literal">connect()</code> constructor is used for creating
  348. a connection to the MySQL server and returns a
  349. <code class="literal">MySQLConnection</code> object.
  350. </p><p>
  351. The following example shows how to connect to the MySQL server:
  352. </p><pre class="programlisting">import mysql.connector
  353. cnx = mysql.connector.connect(user='scott', password='tiger',
  354. host='127.0.0.1',
  355. database='employees')
  356. cnx.close()</pre><p>
  357. See <a class="xref" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">Chapter 6, <i>Connector/Python Connection Arguments</i></a> for all
  358. possible connection arguments.
  359. </p><p>
  360. It is also possible to create connection objects using the
  361. <a class="link" href="#connector-python-apiref-MySQLConnection" title="7.2. Class connection.MySQLConnection">connection.MySQLConnection()</a>
  362. class. Both methods, using the <code class="literal">connect()</code>
  363. constructor, or the class directly, are valid and functionally
  364. equal, but using <code class="literal">connector()</code> is preferred and
  365. will be used in most examples in this manual.
  366. </p><p>
  367. To handle connection errors, use the <code class="literal">try</code>
  368. statement and catch all errors using the
  369. <a class="link" href="#connector-python-apiref-errors-error" title="7.1.2. Exception errors.Error">errors.Error</a>
  370. exception:
  371. </p><pre class="programlisting">import mysql.connector
  372. from mysql.connector import errorcode
  373. try:
  374. cnx = mysql.connector.connect(user='scott',
  375. database='testt')
  376. except mysql.connector.Error as err:
  377. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  378. print("Something is wrong your username or password")
  379. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  380. print("Database does not exists")
  381. else:
  382. print(err)
  383. else:
  384. cnx.close()</pre><p>
  385. If you have lots of connection arguments, it's best to keep them
  386. in a dictionary and use the <code class="literal">**</code>-operator. Here
  387. is an example:
  388. </p><pre class="programlisting">import mysql.connector
  389. config = {
  390. 'user': 'scott',
  391. 'password': 'tiger',
  392. 'host': '127.0.0.1',
  393. 'database': 'employees',
  394. 'raise_on_warnings': True,
  395. }
  396. cnx = mysql.connector.connect(**config)
  397. cnx.close()</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="myconnpy_example_ddl"></a>4.2. Creating Tables Using Connector/Python</h2></div></div></div><p>
  398. All <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_ddl" target="_top">DDL</a> (Data Definition Language)
  399. statements are executed using a handle structure known as a
  400. cursor. The following examples show how to create the tables of
  401. the <code class="literal">employees</code> database. You will need them for
  402. the other examples.
  403. </p><p>
  404. In a MySQL server, tables are very long-lived objects, and are
  405. often accessed by multiple applications written in different
  406. languages. You might typically work with tables that are already
  407. set up, rather than creating them within your own application.
  408. Avoid setting up and dropping tables over and over again, as that
  409. is an expensive operation. The exception is
  410. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_temporary_table" target="_top">temporary tables</a>,
  411. which can be created and dropped quickly within an application.
  412. </p><pre class="programlisting">from __future__ import print_function
  413. import mysql.connector
  414. from mysql.connector import errorcode
  415. DB_NAME = 'employees'
  416. TABLES = {}
  417. TABLES['employees'] = (
  418. "CREATE TABLE `employees` ("
  419. " `emp_no` int(11) NOT NULL AUTO_INCREMENT,"
  420. " `birth_date` date NOT NULL,"
  421. " `first_name` varchar(14) NOT NULL,"
  422. " `last_name` varchar(16) NOT NULL,"
  423. " `gender` enum('M','F') NOT NULL,"
  424. " `hire_date` date NOT NULL,"
  425. " PRIMARY KEY (`emp_no`)"
  426. ") ENGINE=InnoDB")
  427. TABLES['departments'] = (
  428. "CREATE TABLE `departments` ("
  429. " `dept_no` char(4) NOT NULL,"
  430. " `dept_name` varchar(40) NOT NULL,"
  431. " PRIMARY KEY (`dept_no`), UNIQUE KEY `dept_name` (`dept_name`)"
  432. ") ENGINE=InnoDB")
  433. TABLES['salaries'] = (
  434. "CREATE TABLE `salaries` ("
  435. " `emp_no` int(11) NOT NULL,"
  436. " `salary` int(11) NOT NULL,"
  437. " `from_date` date NOT NULL,"
  438. " `to_date` date NOT NULL,"
  439. " PRIMARY KEY (`emp_no`,`from_date`), KEY `emp_no` (`emp_no`),"
  440. " CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) "
  441. " REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
  442. ") ENGINE=InnoDB")
  443. TABLES['dept_emp'] = (
  444. "CREATE TABLE `dept_emp` ("
  445. " `emp_no` int(11) NOT NULL,"
  446. " `dept_no` char(4) NOT NULL,"
  447. " `from_date` date NOT NULL,"
  448. " `to_date` date NOT NULL,"
  449. " PRIMARY KEY (`emp_no`,`dept_no`), KEY `emp_no` (`emp_no`),"
  450. " KEY `dept_no` (`dept_no`),"
  451. " CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) "
  452. " REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
  453. " CONSTRAINT `dept_emp_ibfk_2` FOREIGN KEY (`dept_no`) "
  454. " REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
  455. ") ENGINE=InnoDB")
  456. TABLES['dept_manager'] = (
  457. " CREATE TABLE `dept_manager` ("
  458. " `dept_no` char(4) NOT NULL,"
  459. " `emp_no` int(11) NOT NULL,"
  460. " `from_date` date NOT NULL,"
  461. " `to_date` date NOT NULL,"
  462. " PRIMARY KEY (`emp_no`,`dept_no`),"
  463. " KEY `emp_no` (`emp_no`),"
  464. " KEY `dept_no` (`dept_no`),"
  465. " CONSTRAINT `dept_manager_ibfk_1` FOREIGN KEY (`emp_no`) "
  466. " REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
  467. " CONSTRAINT `dept_manager_ibfk_2` FOREIGN KEY (`dept_no`) "
  468. " REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
  469. ") ENGINE=InnoDB")
  470. TABLES['titles'] = (
  471. "CREATE TABLE `titles` ("
  472. " `emp_no` int(11) NOT NULL,"
  473. " `title` varchar(50) NOT NULL,"
  474. " `from_date` date NOT NULL,"
  475. " `to_date` date DEFAULT NULL,"
  476. " PRIMARY KEY (`emp_no`,`title`,`from_date`), KEY `emp_no` (`emp_no`),"
  477. " CONSTRAINT `titles_ibfk_1` FOREIGN KEY (`emp_no`)"
  478. " REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
  479. ") ENGINE=InnoDB")</pre><p>
  480. The above code shows how we are storing the
  481. <code class="literal">CREATE</code> statements in a Python dictionary called
  482. <code class="literal">TABLES</code>. We also define the database in a global
  483. variable called <code class="literal">DB_NAME</code>, which allows you to
  484. easily use a different schema.
  485. </p><pre class="programlisting">cnx = mysql.connector.connect(user='scott')
  486. cursor = cnx.cursor()</pre><p>
  487. A single MySQL server can contain multiple
  488. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_database" target="_top">databases</a>. Typically, you
  489. specify the database to switch to when connecting to the MySQL
  490. server. This example does not connect to the database upon
  491. connection, so that it can make sure the database exists, and
  492. create it if not.
  493. </p><pre class="programlisting">def create_database(cursor):
  494. try:
  495. cursor.execute(
  496. "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
  497. except mysql.connector.Error as err:
  498. print("Failed creating database: {}".format(err))
  499. exit(1)
  500. try:
  501. cnx.database = DB_NAME
  502. except mysql.connector.Error as err:
  503. if err.errno == errorcode.ER_BAD_DB_ERROR:
  504. create_database(cursor)
  505. cnx.database = DB_NAME
  506. else:
  507. print(err)
  508. exit(1)</pre><p>
  509. We first try to change to a particular database using the
  510. <code class="literal">database</code> property of the connection object
  511. <code class="literal">cnx</code>. If there is an error, we examine the error
  512. number to check if the database does not exist. If so, we call the
  513. <code class="literal">create_database</code> function to create it for us.
  514. </p><p>
  515. On any other error, the application exits and displays the error
  516. message.
  517. </p><pre class="programlisting">for name, ddl in TABLES.iteritems():
  518. try:
  519. print("Creating table {}: ".format(name), end='')
  520. cursor.execute(ddl)
  521. except mysql.connector.Error as err:
  522. if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
  523. print("already exists.")
  524. else:
  525. print(err.errmsg)
  526. else:
  527. print("OK")
  528. cursor.close()
  529. cnx.close()</pre><p>
  530. After we succesfully created or changed to the target database, we
  531. create the tables by iterating over the items of the
  532. <code class="literal">TABLES</code> dictionary.
  533. </p><p>
  534. We handle the error when the table already exists by simply
  535. notifying the user that it was already there. Other errors are
  536. printed, but we simply continue creating tables. (We show how to
  537. handle the <span class="quote"><span class="quote">table already exists</span></span> condition for
  538. illustration purposes. In a real application, we would typically
  539. avoid the error condition entirely by using the <code class="literal">IF NOT
  540. EXISTS</code> clause of the <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/create-table.html" target="_top"><code class="literal">CREATE
  541. TABLE</code></a> statement.)
  542. </p><p>
  543. The output would be something like this:
  544. </p><pre class="programlisting">Creating table employees: already exists.
  545. Creating table salaries: already exists.
  546. Creating table titles: OK
  547. Creating table departments: already exists.
  548. Creating table dept_manager: already exists.
  549. Creating table dept_emp: already exists.</pre><p>
  550. To populate the employees tables, use the dump files of the
  551. <a class="ulink" href="http://dev.mysql.com/doc/employee/en/index.html" target="_top">Employee
  552. Sample Database</a>. Note that you only need the data dump
  553. files that you will find in an archive named like
  554. <code class="literal">employees_db-dump-files-1.0.5.tar.bz2</code>. After
  555. downloading the dump files, do the following from the command
  556. line, adding connection options to the <span class="command"><strong>mysql</strong></span>
  557. commands if necessary:
  558. </p><pre class="programlisting">shell&gt; tar xzf employees_db-dump-files-1.0.5.tar.bz2
  559. shell&gt; cd employees_db
  560. shell&gt; mysql employees &lt; load_employees.dump
  561. shell&gt; mysql employees &lt; load_titles.dump
  562. shell&gt; mysql employees &lt; load_departments.dump
  563. shell&gt; mysql employees &lt; load_salaries.dump
  564. shell&gt; mysql employees &lt; load_dept_emp.dump
  565. shell&gt; mysql employees &lt; load_dept_manager.dump</pre></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="myconnpy_example_cursor_transaction"></a>4.3. Inserting Data Using Connector/Python</h2></div></div></div><p>
  566. Inserting or updating data is also done using the handler
  567. structure known as a cursor. When you use a transactional storage
  568. engine such as <code class="literal">InnoDB</code> (which is the default in
  569. MySQL 5.5 and later), you must
  570. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_commit" target="_top">commit</a> the data after a
  571. sequence of <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/insert.html" target="_top"><code class="literal">INSERT</code></a>,
  572. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/delete.html" target="_top"><code class="literal">DELETE</code></a>, and
  573. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/update.html" target="_top"><code class="literal">UPDATE</code></a> statements.
  574. </p><p>
  575. In this example we show how to insert new data. The second
  576. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/insert.html" target="_top"><code class="literal">INSERT</code></a> depends on the value of the
  577. newly created <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_primary_key" target="_top">primary key</a>
  578. of the first. We are also demonstrating how to use extended
  579. formats. The task is to add a new employee starting to work
  580. tomorrow with a salary set to 50000.
  581. </p><div xmlns="http://www.w3.org/1999/xhtml" class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><div class="admon-title">Note</div><p xmlns="">
  582. The following example uses tables created in the example
  583. <a class="xref" href="#myconnpy_example_ddl" title="4.2. Creating Tables Using Connector/Python">Section 4.2, Creating Tables Using Connector/Python</a>. The
  584. <code class="literal">AUTO_INCREMENT</code> column option for the primary
  585. key of the <code class="literal">employees</code> table is important to
  586. ensure reliable, easily searchable data.
  587. </p></div><pre class="programlisting">from __future__ import print_function
  588. from datetime import date, datetime, timedelta
  589. import mysql.connector
  590. cnx = mysql.connector.connect(user='scott', database='employees')
  591. cursor = cnx.cursor()
  592. tomorrow = datetime.now().date() + timedelta(days=1)
  593. add_employee = ("INSERT INTO employees "
  594. "(first_name, last_name, hire_date, gender, birth_date) "
  595. "VALUES (%s, %s, %s, %s, %s)")
  596. add_salary = ("INSERT INTO salaries "
  597. "(emp_no, salary, from_date, to_date) "
  598. "VALUES (%(emp_no)s, %(salary)s, %(from_date)s, %(to_date)s)")
  599. data_employee = ('Geert', 'Vanderkelen', tomorrow, 'M', date(1977, 6, 14))
  600. # Insert new employee
  601. cursor.execute(add_employee, data_employee)
  602. emp_no = cursor.lastrowid
  603. # Insert salary information
  604. data_salary = {
  605. 'emp_no': emp_no,
  606. 'salary': 50000,
  607. 'from_date': tomorrow,
  608. 'to_date': date(9999, 1, 1),
  609. }
  610. cursor.execute(add_salary, data_salary)
  611. # Make sure data is committed to the database
  612. cnx.commit()
  613. cursor.close()
  614. cnx.close()</pre><p>
  615. We first open a connection to the MySQL server and store the
  616. <a class="link" href="#connector-python-apiref-MySQLConnection" title="7.2. Class connection.MySQLConnection">connection
  617. object</a> in the variable <code class="literal">cnx</code>. We then
  618. create a new cursor, by default a
  619. <a class="link" href="#connector-python-apiref-MySQLCursor" title="7.3. Class cursor.MySQLCursor">MySQLCursor</a>
  620. object, using the connection's
  621. <a class="link" href="#myconnpy_MySQLConnection_cursor" title="7.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)"><code class="literal">cursor()</code></a>
  622. method.
  623. </p><p>
  624. We could calculate tomorrow by calling a database function, but
  625. for clarity we do it in Python using the
  626. <code class="literal">datetime</code> module.
  627. </p><p>
  628. Both <code class="literal">INSERT</code> statements are stored in the
  629. variables called <code class="literal">add_employee</code> and
  630. <code class="literal">add_salary</code>. Note that the second
  631. <code class="literal">INSERT</code> statement uses extended Python format
  632. codes.
  633. </p><p>
  634. The information of the new employee is stored in the tuple
  635. <code class="literal">data_employee</code>. The query to insert the new
  636. employee is executed and we retrieve the newly inserted value for
  637. the column <code class="literal">emp_no</code> using the
  638. <code class="literal">lastrowid</code> property of the cursor object.
  639. </p><p>
  640. Next, we insert the new salary for the new employee. We are using
  641. the <code class="literal">emp_no</code> variable in the directory holding
  642. the data. This directory is passed to the
  643. <code class="literal">execute()</code> method of the cursor object.
  644. </p><p>
  645. Since by default Connector/Python turns
  646. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_autocommit" target="_top">autocommit</a> off, and MySQL
  647. 5.5 and later uses transactional <code class="literal">InnoDB</code> tables
  648. by default, it is necessary to commit your changes using the
  649. connection's <code class="literal">commit()</code> method. You could also
  650. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_rollback" target="_top">roll back</a> using the
  651. <code class="literal">rollback()</code> method.
  652. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="myconnpy_example_cursor_select"></a>4.4. Querying Data Using Connector/Python</h2></div></div></div><p>
  653. The following example shows how to
  654. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_query" target="_top">query</a> data using a cursor
  655. created using the connection's
  656. <a class="link" href="#myconnpy_MySQLConnection_cursor" title="7.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)"><code class="literal">cursor()</code></a>
  657. method. The data returned is formatted and printed on the console.
  658. </p><p>
  659. The task is to select all employees hired in the year 1999 and
  660. print their names with their hire date to the console.
  661. </p><pre class="programlisting">import datetime
  662. import mysql.connector
  663. cnx = mysql.connector.connect(user='scott', database='employees')
  664. cursor = cnx.cursor()
  665. query = ("SELECT first_name, last_name, hire_date FROM employees "
  666. "WHERE hire_date BETWEEN %s AND %s")
  667. hire_start = datetime.date(1999, 1, 1)
  668. hire_end = datetime.date(1999, 12, 31)
  669. cursor.execute(query, (hire_start, hire_end))
  670. for (first_name, last_name, hire_date) in cursor:
  671. print("{}, {} was hired on {:%d %b %Y}".format(
  672. last_name, first_name, hire_date))
  673. cursor.close()
  674. cnx.close()</pre><p>
  675. We first open a connection to the MySQL server and store the
  676. <a class="link" href="#connector-python-apiref-MySQLConnection" title="7.2. Class connection.MySQLConnection">connection
  677. object</a> in the variable <code class="literal">cnx</code>. We then
  678. create a new cursor, by default a
  679. <a class="link" href="#connector-python-apiref-MySQLCursor" title="7.3. Class cursor.MySQLCursor">MySQLCursor</a>
  680. object, using the connection's
  681. <a class="link" href="#myconnpy_MySQLConnection_cursor" title="7.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)"><code class="literal">cursor()</code></a>
  682. method.
  683. </p><p>
  684. In the preceding example, we store the <code class="literal">SELECT</code>
  685. statement in the variable <code class="literal">query</code>. Note that we
  686. are using unquoted <code class="literal">%s</code>-markers where dates
  687. should have been. Connector/Python converts
  688. <code class="literal">hire_start</code> and <code class="literal">hire_end</code> from
  689. Python types to a data type that MySQL understands and adds the
  690. required quotes. In this case, it replaces the first
  691. <code class="literal">%s</code> with <code class="literal">'1999-01-01'</code>, and
  692. the second with <code class="literal">'1999-12-31'</code>.
  693. </p><p>
  694. We then execute the operation stored in the
  695. <code class="literal">query</code> variable using the
  696. <a class="link" href="#myconnpy_MySQLCursor_execute" title="7.3.4. Method MySQLCursor.execute(operation, params=None, multi=False)"><code class="literal">execute()</code></a>
  697. method. The data used to replace the <code class="literal">%s</code>-markers
  698. in the query is passed as a tuple: <code class="literal">(hire_start,
  699. hire_end)</code>.
  700. </p><p>
  701. After executing the query, the MySQL server is ready to send the
  702. data. The result set could be zero rows, one row, or 100 million
  703. rows. Depending on the expected volume, you can use different
  704. techniques to process this result set. In this example, we use the
  705. <code class="literal">cursor</code> object as an iterator. The first column
  706. in the row will be stored in the variable
  707. <code class="literal">first_name</code>, the second in
  708. <code class="literal">last_name</code>, and the third in
  709. <code class="literal">hire_date</code>.
  710. </p><p>
  711. We print the result, formatting the output using Python's built-in
  712. <code class="literal">format()</code> function. Note that
  713. <code class="literal">hire_date</code> was converted automatically by
  714. Connector/Python to a Python <code class="literal">datetime.date</code>
  715. object. This means that we can easily format the date in a more
  716. human-readable form.
  717. </p><p>
  718. The output should be something like this:
  719. </p><pre class="programlisting">..
  720. Wilharm, LiMin was hired on 16 Dec 1999
  721. Wielonsky, Lalit was hired on 16 Dec 1999
  722. Kamble, Dannz was hired on 18 Dec 1999
  723. DuBourdieux, Zhongwei was hired on 19 Dec 1999
  724. Fujisawa, Rosita was hired on 20 Dec 1999
  725. ..</pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-tutorials"></a>Chapter 5. Connector/Python Tutorials</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#myconnpy_tutorial_CursorBuffered_GiveRaise">5.1. Tutorial: Raise employee's salary using a buffering cursor</a></span></dt></dl></div><p>
  726. These tutorials illustrate how to develop Python applications and
  727. scripts that connect to a MySQL database server using MySQL
  728. Connector/Python.
  729. </p><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="myconnpy_tutorial_CursorBuffered_GiveRaise"></a>5.1. Tutorial: Raise employee's salary using a buffering cursor</h2></div></div></div><p>
  730. The following example script will give a long-overdue raise
  731. effective tomorrow to all employees who joined in the year 2000
  732. and are still with the company.
  733. </p><p>
  734. We are using buffered cursors to iterate through the selected
  735. employees. This way we do not have to fetch the rows in a new
  736. variables, but can instead use the cursor as an iterator.
  737. </p><p>
  738. Note that the script is an example; there are other ways of doing
  739. this simple task.
  740. </p><pre class="programlisting">from __future__ import print_function
  741. from decimal import Decimal
  742. from datetime import datetime, date, timedelta
  743. import mysql.connector
  744. # Connect with the MySQL Server
  745. cnx = mysql.connector.connect(user='scott', database='employees')
  746. # Get two buffered cursors
  747. curA = cnx.cursor(buffered=True)
  748. curB = cnx.cursor(buffered=True)
  749. # Query to get employees who joined in a period defined by two dates
  750. query = (
  751. "SELECT s.emp_no, salary, from_date, to_date FROM employees AS e "
  752. "LEFT JOIN salaries AS s USING (emp_no) "
  753. "WHERE to_date = DATE('9999-01-01')"
  754. "AND e.hire_date BETWEEN DATE(%s) AND DATE(%s)")
  755. # UPDATE and INSERT statements for the old and new salary
  756. update_old_salary = (
  757. "UPDATE salaries SET to_date = %s "
  758. "WHERE emp_no = %s AND from_date = %s")
  759. insert_new_salary = (
  760. "INSERT INTO salaries (emp_no, from_date, to_date, salary) "
  761. "VALUES (%s, %s, %s, %s)")
  762. # Select the employes getting a raise
  763. curA.execute(query, (date(2000, 1, 1), date(2001, 1, 1)))
  764. # Iterate through the result of curA
  765. for (emp_no, salary, from_date, to_date) in curA:
  766. # Update the old and insert the new salary
  767. new_salary = int(round(salary * Decimal('1.15')))
  768. curB.execute(update_old_salary, (tomorrow, emp_no, from_date))
  769. curB.execute(insert_new_salary,
  770. (emp_no, tomorrow, date(9999, 1, 1,), new_salary))
  771. # Commit the changes
  772. cnx.commit()
  773. cnx.close()</pre></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-connectargs"></a>Chapter 6. Connector/Python Connection Arguments</h1></div></div></div><p>
  774. The following lists the arguments which can be used to initiate a
  775. connection with the MySQL server using either:
  776. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  777. Function <code class="literal">mysql.connector.connect()</code>
  778. </p></li><li class="listitem"><p>
  779. Class <code class="literal">mysql.connector.MySQLConnection()</code>
  780. </p></li></ul></div><div class="table"><a name="id499609"></a><p class="title"><b>Table 6.1. Connection Arguments for Connector/Python</b></p><div class="table-contents"><table summary="Connection Arguments for Connector/Python" border="1"><colgroup><col><col><col></colgroup><thead><tr><th scope="col">Argument Name</th><th scope="col">Default</th><th scope="col">Description</th></tr></thead><tbody><tr><td scope="row"><code class="literal">user</code> (<code class="literal">username</code>*)</td><td> </td><td>The username used to authenticate with the MySQL Server.</td></tr><tr><td scope="row"><code class="literal">password</code> (<code class="literal">passwd</code>*)</td><td> </td><td>The password to authenticate the user with the MySQL Server.</td></tr><tr><td scope="row"><code class="literal">database</code> (<code class="literal">db</code>*)</td><td> </td><td>Database name to use when connecting with the MySQL Server.</td></tr><tr><td scope="row"><code class="literal">host</code></td><td>127.0.0.1</td><td>Hostname or IP address of the MySQL Server.</td></tr><tr><td scope="row"><code class="literal">port</code></td><td>3306</td><td>TCP/IP port of the MySQL Server. Must be an integer.</td></tr><tr><td scope="row"><code class="literal">unix_socket</code></td><td> </td><td>The location of the Unix socket file.</td></tr><tr><td scope="row"><code class="literal">use_unicode</code></td><td>True</td><td>Whether to use Unicode or not.</td></tr><tr><td scope="row"><code class="literal">charset</code></td><td>utf8</td><td>Which MySQL character set to use.</td></tr><tr><td scope="row"><code class="literal">collation</code></td><td>utf8_general_ci</td><td>Which MySQL collation to use.</td></tr><tr><td scope="row"><code class="literal">autocommit</code></td><td>False</td><td>Whether to <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_autocommit" target="_top">autocommit</a>
  781. transactions.</td></tr><tr><td scope="row"><code class="literal">time_zone</code></td><td> </td><td>Set the <code class="literal">time_zone</code> session variable at connection.</td></tr><tr><td scope="row"><code class="literal">sql_mode</code></td><td> </td><td>Set the <code class="literal">sql_mode</code> session variable at connection.</td></tr><tr><td scope="row"><code class="literal">get_warnings</code></td><td>False</td><td>Whether to fetch warnings.</td></tr><tr><td scope="row"><code class="literal">raise_on_warnings</code></td><td>False</td><td>Whether to raise an exception on warnings.</td></tr><tr><td scope="row"><code class="literal">connection_timeout</code>
  782. (<code class="literal">connect_timeout</code>*)</td><td> </td><td>Timeout for the TCP and Unix socket connections.</td></tr><tr><td scope="row"><code class="literal">client_flags</code></td><td> </td><td>MySQL client flags.</td></tr><tr><td scope="row"><code class="literal">buffered</code></td><td>False</td><td>Whether cursor object fetches the result immediately after executing
  783. query.</td></tr><tr><td scope="row"><code class="literal">raw</code></td><td>False</td><td>Whether MySQL results are returned as-is, rather than converted to
  784. Python types.</td></tr><tr><td scope="row"><code class="literal">ssl_ca</code></td><td> </td><td>File containing the SSL certificate authority.</td></tr><tr><td scope="row"><code class="literal">ssl_cert</code></td><td> </td><td>File containing the SSL certificate file.</td></tr><tr><td scope="row"><code class="literal">ssl_key</code></td><td> </td><td>File containing the SSL key.</td></tr><tr><td scope="row"><code class="literal">dsn</code></td><td> </td><td>Not supported (raises <code class="literal">NotSupportedError</code> when used).</td></tr></tbody></table></div></div><br class="table-break"><p>
  785. * Synonymous argument name, available only for compatibility with
  786. other Python MySQL drivers. Oracle recommends not to use these
  787. alternative names.
  788. </p><p>
  789. Authentication with MySQL will use <code class="literal">username</code> and
  790. <code class="literal">password</code>. Note that MySQL Connector/Python does
  791. not support the old, insecure password protocols of MySQL versions
  792. prior to 4.1.
  793. </p><p>
  794. When the <code class="literal">database</code> parameter is given, the current
  795. database is set to the given value. To later change the database,
  796. execute the MySQL <code class="literal">USE</code> command or set the
  797. <code class="literal">database</code> property of the
  798. <code class="literal">MySQLConnection</code> instance.
  799. </p><p>
  800. By default, Connector/Python tries to connect to a MySQL server
  801. running on <code class="literal">localhost</code> using TCP/IP. The
  802. <code class="literal">host</code> argument defaults to IP address 127.0.0.1
  803. and <code class="literal">port</code> to 3306. Unix sockets are supported by
  804. setting <code class="literal">unix_socket</code>. Named pipes on the Windows
  805. platform are not supported.
  806. </p><p>
  807. Strings coming from MySQL are by default returned as Python Unicode
  808. literals. To change this behavior, set
  809. <code class="literal">use_unicode</code> to <code class="literal">False</code>. You can
  810. change the character setting for the client connection through the
  811. <code class="literal">charset</code> argument. To change the character set
  812. after connecting to MySQL, set the <code class="literal">charset</code>
  813. property of the <code class="literal">MySQLConnection</code> instance. This
  814. technique is preferred over using the MySQL <code class="literal">SET
  815. NAMES</code> statement directly. Similar to the
  816. <code class="literal">charset</code> property, you can set the
  817. <code class="literal">collation</code> for the current MySQL session.
  818. </p><p>
  819. Transactions are not automatically committed; call the
  820. <code class="literal">commit()</code> method of the
  821. <code class="literal">MySQLConnection</code> instance within your application
  822. after doing a set of related insert, update, and delete operations.
  823. For data consistency and high throughput for write operations, it is
  824. best to leave the <code class="literal">autocommit</code> configuration option
  825. turned off when using <code class="literal">InnoDB</code> or other
  826. transactional tables.
  827. </p><p>
  828. The time zone can be set per connection using the
  829. <code class="literal">time_zone</code> argument. This is useful if the MySQL
  830. server is set, for example, to UTC and <code class="literal">TIMESTAMP</code>
  831. values should be returned by MySQL converted to the
  832. <code class="literal">PST</code> time zone.
  833. </p><p>
  834. MySQL supports so called SQL Modes. which will change the behavior
  835. of the server globally or per connection. For example, to have
  836. warnings raised as errors, set <code class="literal">sql_mode</code> to
  837. <code class="literal">TRADITIONAL</code>. For more information, see
  838. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html" target="_top">Server SQL Modes</a>.
  839. </p><p>
  840. Warnings generated by queries are fetched automatically when
  841. <code class="literal">get_warnings</code> is set to <code class="literal">True</code>.
  842. You can also immediately raise an exception by setting
  843. <code class="literal">raise_on_warnings</code> to <code class="literal">True</code>.
  844. Consider using the MySQL
  845. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html" target="_top">sql_mode</a> setting for turning
  846. warnings into errors.
  847. </p><p>
  848. To set a timeout value for connections, use
  849. <code class="literal">connection_timeout</code>.
  850. </p><p>
  851. MySQL uses <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/mysql-real-connect.html" target="_top">client flags</a> to
  852. enable or disable features. Using the
  853. <code class="literal">client_flags</code> argument, you have control of what
  854. is set. To find out what flags are available, use the following:
  855. </p><pre class="programlisting">from mysql.connector.constants import ClientFlag
  856. print '\n'.join(ClientFlag.get_full_info())</pre><p>
  857. If <code class="literal">client_flags</code> is not specified (that is, it is
  858. zero), defaults are used for MySQL v4.1 and later. If you specify an
  859. integer greater than <code class="literal">0</code>, make sure all flags are
  860. set. A better way to set and unset flags is to use a list. For
  861. example, to set <code class="literal">FOUND_ROWS</code>, but disable the
  862. default <code class="literal">LONG_FLAG</code>:
  863. </p><pre class="programlisting">flags = [ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG]
  864. mysql.connector.connect(client_flags=flags)</pre><p>
  865. By default, MySQL Connector/Python does not buffer or pre-fetch
  866. results. This means that after a query is executed, your program is
  867. responsible of fetching the data. This avoids using excessive memory
  868. when queries return large result sets. If you know that the result
  869. set is small enough to handle all at once, fetching the results
  870. immediately by setting <code class="literal">buffered</code> to
  871. <code class="literal">True</code>. It is also possible to set this per cursor
  872. (see cursor manual).
  873. </p><p>
  874. MySQL types will be converted automatically to Python types. For
  875. example, a <code class="literal">DATETIME</code> column becomes a
  876. <a class="ulink" href="http://docs.python.org/library/datetime.html#datetime.datetime" target="_top">datetime.datetime</a>
  877. object. When conversion should be done differently, for example to
  878. get better performance, set <code class="literal">raw</code> to
  879. <code class="literal">True</code>.
  880. </p><p>
  881. Using SSL connections is possible when your
  882. <a class="ulink" href="http://docs.python.org/library/ssl.html" target="_top">Python
  883. installation supports SSL</a>, that is, when it is compiled
  884. against the OpenSSL libraries. When you provide the arguments
  885. <code class="literal">ssl_ca</code>, <code class="literal">ssl_key</code> and
  886. <code class="literal">ssl_cert</code>, the connection switches to SSL. You can
  887. use this in combination with the <code class="literal">compressed</code>
  888. argument set to <code class="literal">True</code>.
  889. </p><p>
  890. <code class="literal">passwd</code>, <code class="literal">db</code> and
  891. <code class="literal">connect_timeout</code> are valid for compatibility with
  892. other MySQL interfaces and are respectively the same as
  893. <code class="literal">password</code>, <code class="literal">database</code> and
  894. <code class="literal">connection_timeout</code>. The latter take precedence.
  895. Data source name syntax or <code class="literal">dsn</code> is not used; if
  896. specified, it raises a <code class="literal">NotSupportedError</code>
  897. exception.
  898. </p></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="connector-python-reference"></a>Chapter 7. Connector/Python API Reference</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#connector-python-apiref-errors">7.1. Errors and Exceptions</a></span></dt><dd><dl><dt><span class="section"><a href="#connector-python-apiref-errorcode">7.1.1. Module <code class="literal">errorcode</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-error">7.1.2. Exception <code class="literal">errors.Error</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-warning">7.1.3. Exception <code class="literal">errors.Warning</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-interfaceerror">7.1.4. Exception <code class="literal">errors.InterfaceError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-databaseerror">7.1.5. Exception <code class="literal">errors.DatabaseError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-internalerror">7.1.6. Exception <code class="literal">errors.InternalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-operationalerror">7.1.7. Exception <code class="literal">errors.OperationalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-programmingerror">7.1.8. Exception <code class="literal">errors.ProgrammingError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-integrityerror">7.1.9. Exception <code class="literal">errors.IntegrityError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-dataerror">7.1.10. Exception <code class="literal">errors.DataError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-notsupportederror">7.1.11. Exception <code class="literal">errors.NotSupportedError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-custom-error-exception">7.1.12. Function <code class="literal">errors.custom_error_exception(error=None,
  899. exception=None)</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLConnection">7.2. Class <code class="literal">connection.MySQLConnection</code></a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_MySQLConnection">7.2.1. Constructor <code class="literal">connection.MySQLConnection(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_close">7.2.2. Method <code class="literal">MySQLConnection.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_config">7.2.3. Method <code class="literal">MySQLConnection.config(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connect">7.2.4. Method <code class="literal">MySQLConnection.connect(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_commit">7.2.5. Method <code class="literal">MySQLConnection.commit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cursor">7.2.6. Method <code class="literal">MySQLConnection.cursor(buffered=None, raw=None,
  900. cursor_class=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_change_user">7.2.7. Method <code class="literal">MySQLConnection.cmd_change_user(username='',
  901. password='', database='', charset=33)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_debug">7.2.8. Method <code class="literal">MySQLConnection.cmd_debug()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_init_db">7.2.9. Method <code class="literal">MySQLConnection.cmd_init_db(database)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_ping">7.2.10. Method <code class="literal">MySQLConnection.cmd_ping()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_info">7.2.11. Method <code class="literal">MySQLConnection.cmd_process_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_kill">7.2.12. Method <code class="literal">MySQLConnection.cmd_process_kill(mysql_pid)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_quit">7.2.13. Method <code class="literal">MySQLConnection.cmd_quit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query">7.2.14. Method <code class="literal">MySQLConnection.cmd_query(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query_iter">7.2.15. Method <code class="literal">MySQLConnection.cmd_query_iter(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_refresh">7.2.16. Method <code class="literal">MySQLConnection.cmd_refresh(options)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_shutdown">7.2.17. Method <code class="literal">MySQLConnection.cmd_shutdown()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_statistics">7.2.18. Method <code class="literal">MySQLConnection.cmd_statistics()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_disconnect">7.2.19. Method <code class="literal">MySQLConnection.disconnect()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_rows">7.2.20. Method <code class="literal">MySQLConnection.get_rows(count=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_row">7.2.21. Method <code class="literal">MySQLConnection.get_row()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_info">7.2.22. Method <code class="literal">MySQLConnection.get_server_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_version">7.2.23. Method <code class="literal">MySQLConnection.get_server_version()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_is_connected">7.2.24. Method <code class="literal">MySQLConnection.is_connected()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_isset_client_flag">7.2.25. Method <code class="literal">MySQLConnection.isset_client_flag(flag)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_ping">7.2.26. Method <code class="literal">MySQLConnection.ping(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnectoin_reconnect">7.2.27. Method <code class="literal">MySQLConnection.reconnect(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_rollback">7.2.28. Method <code class="literal">MySQLConnection.rollback()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_charset_collation">7.2.29. Method <code class="literal">MySQLConnection.set_charset_collation(charset=None,
  902. collation=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_client_flags">7.2.30. Method <code class="literal">MySQLConnection.set_client_flags(flags)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_autocommit">7.2.31. Property <code class="literal">MySQLConnection.autocommit</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_charset_name">7.2.32. Property <code class="literal">MySQLConnection.charset_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_collation_name">7.2.33. Property <code class="literal">MySQLConnection.collation_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connection_id">7.2.34. Property <code class="literal">MySQLConnection.connection_id</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_database">7.2.35. Property <code class="literal">MySQLConnection.database</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_warnings">7.2.36. Property <code class="literal">MySQLConnection.get_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_raise_on_warnings">7.2.37. Property <code class="literal">MySQLConnection.raise_on_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_host">7.2.38. Property <code class="literal">MySQLConnection.server_host</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_port">7.2.39. Property <code class="literal">MySQLConnection.server_port</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_sql_mode">7.2.40. Property <code class="literal">MySQLConnection.sql_mode</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_time_zone">7.2.41. Property <code class="literal">MySQLConnection.time_zone</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_unix_socket">7.2.42. Property <code class="literal">MySQLConnection.unix_socket</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_user">7.2.43. Property <code class="literal">MySQLConnection.user</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLCursor">7.3. Class <code class="literal">cursor.MySQLCursor</code></a></span></dt><dd><dl><dt><span class="section"><a href="#myconnpy_MySQLCursor">7.3.1. Constructor <code class="literal">cursor.MySQLCursor</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_callproc">7.3.2. Method <code class="literal">MySQLCursor.callproc(procname, args=())</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_close">7.3.3. Method <code class="literal">MySQLCursor.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_execute">7.3.4. Method <code class="literal">MySQLCursor.execute(operation, params=None,
  903. multi=False)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_executemany">7.3.5. Method <code class="literal">MySQLCursor.executemany(operation, seq_params)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchall">7.3.6. Method <code class="literal">MySQLCursor.fetchall()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchmany">7.3.7. Method <code class="literal">MySQLCursor.fetchmany(size=1)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchone">7.3.8. Method <code class="literal">MySQLCursor.fetchone()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchwarnings">7.3.9. Method <code class="literal">MySQLCursor.fetchwarnings()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_stored_results">7.3.10. Method <code class="literal">MySQLCursor.stored_results()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_column_names">7.3.11. Property <code class="literal">MySQLCursor.column_names</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_statement">7.3.12. Property <code class="literal">MySQLCursor.statement</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_with_rows">7.3.13. Property <code class="literal">MySQLCursor.with_rows</code></a></span></dt></dl></dd><dt><span class="section"><a href="#connector-python-apiref-MySQLCursorBuffered">7.4. Class <code class="literal">cursor.MySQLCursorBuffered</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-ClientFlag">7.5. Class <code class="literal">constants.ClientFlag</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-FieldType">7.6. Class <code class="literal">constants.FieldType</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-SQLMode">7.7. Class <code class="literal">constants.SQLMode</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-CharacterSet">7.8. Class <code class="literal">constants.CharacterSet</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-RefreshOption">7.9. Class <code class="literal">constants.RefreshOption</code></a></span></dt></dl></div><p>
  904. This section contains the public API reference of Connector/Python.
  905. Although valid for both Python 2 and Python 3, examples should be
  906. considered working for Python 2.7, and Python 3.1 and greater.
  907. </p><p>
  908. The following overview shows the <code class="literal">mysql.connector</code>
  909. package with its modules. Currently, only the most useful modules,
  910. classes and functions for end users are documented.
  911. </p><pre class="programlisting">mysql.connector
  912. errorcode
  913. errors
  914. connection
  915. constants
  916. conversion
  917. cursor
  918. dbapi
  919. locales
  920. eng
  921. client_error
  922. protocol
  923. utils</pre><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-errors"></a>7.1. Errors and Exceptions</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="#connector-python-apiref-errorcode">7.1.1. Module <code class="literal">errorcode</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-error">7.1.2. Exception <code class="literal">errors.Error</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-warning">7.1.3. Exception <code class="literal">errors.Warning</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-interfaceerror">7.1.4. Exception <code class="literal">errors.InterfaceError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-databaseerror">7.1.5. Exception <code class="literal">errors.DatabaseError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-internalerror">7.1.6. Exception <code class="literal">errors.InternalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-operationalerror">7.1.7. Exception <code class="literal">errors.OperationalError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-programmingerror">7.1.8. Exception <code class="literal">errors.ProgrammingError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-integrityerror">7.1.9. Exception <code class="literal">errors.IntegrityError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-dataerror">7.1.10. Exception <code class="literal">errors.DataError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-notsupportederror">7.1.11. Exception <code class="literal">errors.NotSupportedError</code></a></span></dt><dt><span class="section"><a href="#connector-python-apiref-errors-custom-error-exception">7.1.12. Function <code class="literal">errors.custom_error_exception(error=None,
  924. exception=None)</code></a></span></dt></dl></div><p>
  925. The <code class="literal">mysql.connector.errors</code> module defines
  926. exception classes for errors and warnings raised by MySQL
  927. Connector/Python. Most classes defined in this module are
  928. available when you import <code class="literal">mysql.connector</code>.
  929. </p><p>
  930. The exception classes defined in this module follow mostly the
  931. Python Database Specification v2.0 (PEP-249). For some MySQL
  932. client or server errors it is not always clear which exception to
  933. raise. It is good to discuss whether an error should be
  934. reclassified by opening a bug report.
  935. </p><p>
  936. MySQL Server errors are mapped with Python exception based on
  937. their SQLState (see <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html" target="_top">Server Error Codes and Messages</a>). The
  938. following list shows the <code class="literal">SQLState</code> classes and
  939. the exception Connector/Python will raise. It is, however,
  940. possible to redefine which exception is raised for each server
  941. error. Note that the default exception is
  942. <code class="literal">DatabaseError</code>.
  943. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  944. <code class="literal">02</code>: DataError
  945. </p></li><li class="listitem"><p>
  946. <code class="literal">07</code>: DatabaseError
  947. </p></li><li class="listitem"><p>
  948. <code class="literal">08</code>: OperationalError
  949. </p></li><li class="listitem"><p>
  950. <code class="literal">0A</code>: NotSupportedError
  951. </p></li><li class="listitem"><p>
  952. <code class="literal">21</code>: DataError
  953. </p></li><li class="listitem"><p>
  954. <code class="literal">22</code>: DataError
  955. </p></li><li class="listitem"><p>
  956. <code class="literal">23</code>: IntegrityError
  957. </p></li><li class="listitem"><p>
  958. <code class="literal">24</code>: ProgrammingError
  959. </p></li><li class="listitem"><p>
  960. <code class="literal">25</code>: ProgrammingError
  961. </p></li><li class="listitem"><p>
  962. <code class="literal">26</code>: ProgrammingError
  963. </p></li><li class="listitem"><p>
  964. <code class="literal">27</code>: ProgrammingError
  965. </p></li><li class="listitem"><p>
  966. <code class="literal">28</code>: ProgrammingError
  967. </p></li><li class="listitem"><p>
  968. <code class="literal">2A</code>: ProgrammingError
  969. </p></li><li class="listitem"><p>
  970. <code class="literal">2B</code>: DatabaseError
  971. </p></li><li class="listitem"><p>
  972. <code class="literal">2C</code>: ProgrammingError
  973. </p></li><li class="listitem"><p>
  974. <code class="literal">2D</code>: DatabaseError
  975. </p></li><li class="listitem"><p>
  976. <code class="literal">2E</code>: DatabaseError
  977. </p></li><li class="listitem"><p>
  978. <code class="literal">33</code>: DatabaseError
  979. </p></li><li class="listitem"><p>
  980. <code class="literal">34</code>: ProgrammingError
  981. </p></li><li class="listitem"><p>
  982. <code class="literal">35</code>: ProgrammingError
  983. </p></li><li class="listitem"><p>
  984. <code class="literal">37</code>: ProgrammingError
  985. </p></li><li class="listitem"><p>
  986. <code class="literal">3C</code>: ProgrammingError
  987. </p></li><li class="listitem"><p>
  988. <code class="literal">3D</code>: ProgrammingError
  989. </p></li><li class="listitem"><p>
  990. <code class="literal">3F</code>: ProgrammingError
  991. </p></li><li class="listitem"><p>
  992. <code class="literal">40</code>: InternalError
  993. </p></li><li class="listitem"><p>
  994. <code class="literal">42</code>: ProgrammingError
  995. </p></li><li class="listitem"><p>
  996. <code class="literal">44</code>: InternalError
  997. </p></li><li class="listitem"><p>
  998. <code class="literal">HZ</code>: OperationalError
  999. </p></li><li class="listitem"><p>
  1000. <code class="literal">XA</code>: IntegrityError
  1001. </p></li><li class="listitem"><p>
  1002. <code class="literal">0K</code>: OperationalError
  1003. </p></li><li class="listitem"><p>
  1004. <code class="literal">HY</code>: DatabaseError
  1005. </p></li></ul></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errorcode"></a>7.1.1. Module <code class="literal">errorcode</code></h3></div></div></div><p>
  1006. This module contains both MySQL server and client error codes
  1007. defined as module attributes with the error number as value.
  1008. Using error codes instead of error numbers could make reading
  1009. the source code a bit easier.
  1010. </p><pre class="programlisting">
  1011. &gt;&gt;&gt; from mysql.connector import errorcode
  1012. &gt;&gt;&gt; errorcode.ER_BAD_TABLE_ERROR
  1013. 1051
  1014. </pre><p>
  1015. See <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html" target="_top">Server Error Codes and Messages</a> and
  1016. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/error-messages-client.html" target="_top">Client Error Codes and Messages</a>.
  1017. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-error"></a>7.1.2. Exception <code class="literal">errors.Error</code></h3></div></div></div><p>
  1018. This exception is the base class for all other exceptions in the
  1019. <code class="literal">errors</code> module. It can be used to catch all
  1020. errors in a single <code class="literal">except</code> statement.
  1021. </p><p>
  1022. The following example shows how we could catch syntax errors:
  1023. </p><pre class="programlisting">import mysql.connector
  1024. try:
  1025. cnx = mysql.connector.connect(user='scott', database='employees')
  1026. cursor = cnx.cursor()
  1027. cursor.execute("SELECT * FORM employees") # Syntax error in query
  1028. cnx.close()
  1029. except mysql.connector.Error as err:
  1030. print("Something went wrong: {}".format(err))</pre><p>
  1031. Initializing the exception supports a few optional arguments,
  1032. namely <code class="literal">msg</code>, <code class="literal">errno</code>,
  1033. <code class="literal">values</code> and <code class="literal">sqlstate</code>. All
  1034. of them are optional and default to <code class="literal">None</code>.
  1035. <code class="literal">errors.Error</code> isinternally used by
  1036. Connector/Python to raise MySQL client and server errors and
  1037. should not be used by your application to raise exceptions.
  1038. </p><p>
  1039. The following examples show the result when using no or a
  1040. combination of the arguments:
  1041. </p><pre class="programlisting">
  1042. &gt;&gt;&gt; from mysql.connector.errors import Error
  1043. &gt;&gt;&gt; str(Error())
  1044. 'Unknown error'
  1045. &gt;&gt;&gt; str(Error("Oops! There was an error."))
  1046. 'Oops! There was an error.'
  1047. &gt;&gt;&gt; str(Error(errno=2006))
  1048. '2006: MySQL server has gone away'
  1049. &gt;&gt;&gt; str(Error(errno=2002, values=('/tmp/mysql.sock', 2)))
  1050. "2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"
  1051. &gt;&gt;&gt; str(Error(errno=1146, sqlstate='42S02', msg="Table 'test.spam' doesn't exist"))
  1052. "1146 (42S02): Table 'test.spam' doesn't exist"
  1053. </pre><p>
  1054. The example which uses error number 1146 is used when
  1055. Connector/Python receives an error packet from the MySQL Server.
  1056. The information is parsed and passed to the
  1057. <code class="literal">Error</code> exception as shown.
  1058. </p><p>
  1059. Each exception subclassing from <code class="literal">Error</code> can be
  1060. initialized using the above mentioned arguments. Additionally,
  1061. each instance has the attributes <code class="literal">errno</code>,
  1062. <code class="literal">msg</code> and <code class="literal">sqlstate</code> which can
  1063. be used in your code.
  1064. </p><p>
  1065. The following example shows how to handle errors when dropping a
  1066. table which does not exists (when you do not want to use the
  1067. <code class="literal">IF EXISTS</code> clause):
  1068. </p><pre class="programlisting">import mysql.connector
  1069. from mysql.connector import errorcode
  1070. cnx = mysql.connector.connect(user='scott', database='test')
  1071. try:
  1072. cur.execute("DROP TABLE spam")
  1073. except mysql.connector.Error as err:
  1074. if err.errno == errorcode.ER_BAD_TABLE_ERROR:
  1075. print("Creating table spam")
  1076. else:
  1077. raise</pre><p>
  1078. <code class="literal">errors.Error</code> is a subclass of the Python
  1079. <code class="literal">StandardError</code>.
  1080. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-warning"></a>7.1.3. Exception <code class="literal">errors.Warning</code></h3></div></div></div><p>
  1081. This exception is used for reporting important warnings,
  1082. however, Connector/Python does not use it. It is included to be
  1083. compliant with the Python Database Specification v2.0 (PEP-249).
  1084. </p><p>
  1085. Consider using either more strict
  1086. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html" target="_top">Server SQL Modes</a> or the
  1087. <a class="link" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">raise_on_warnings</a>
  1088. connection argument to make Connector/Python raise errors when
  1089. your queries produce warnings.
  1090. </p><p>
  1091. <code class="literal">errors.Warning</code> is a subclass of the Python
  1092. <code class="literal">StandardError</code>.
  1093. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-interfaceerror"></a>7.1.4. Exception <code class="literal">errors.InterfaceError</code></h3></div></div></div><p>
  1094. This exception is raised for errors originating from
  1095. Connector/Python itself, not related to the MySQL server.
  1096. </p><p>
  1097. <code class="literal">errors.InterfaceError</code> is a subclass of
  1098. <code class="literal">errors.Error</code>.
  1099. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-databaseerror"></a>7.1.5. Exception <code class="literal">errors.DatabaseError</code></h3></div></div></div><p>
  1100. This exception is the default for any MySQL error which does not
  1101. fit the other exceptions.
  1102. </p><p>
  1103. <code class="literal">errors.DatabaseError</code> is a subclass of
  1104. <code class="literal">errors.Error</code>.
  1105. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-internalerror"></a>7.1.6. Exception <code class="literal">errors.InternalError</code></h3></div></div></div><p>
  1106. This exception is raised when the MySQL server encounters an
  1107. internal error, for example, when a deadlock occurred.
  1108. </p><p>
  1109. <code class="literal">errors.InternalError</code> is a subclass of
  1110. <code class="literal">errors.DatabaseError</code>.
  1111. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-operationalerror"></a>7.1.7. Exception <code class="literal">errors.OperationalError</code></h3></div></div></div><p>
  1112. This exception is raised for errors which are related to MySQL's
  1113. operations. For example, to many connections, a hostname could
  1114. not be resolved, bad handshake, server is shutting down,
  1115. communication errors, and so on.
  1116. </p><p>
  1117. <code class="literal">errors.OperationalError</code> is a subclass of
  1118. <code class="literal">errors.DatabaseError</code>.
  1119. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-programmingerror"></a>7.1.8. Exception <code class="literal">errors.ProgrammingError</code></h3></div></div></div><p>
  1120. This exception is raised on programming errors, for example when
  1121. you have a syntax error in your SQL or a table was not found.
  1122. </p><p>
  1123. The following example shows how to handle syntax errors:
  1124. </p><pre class="programlisting">try:
  1125. cursor.execute("CREATE DESK t1 (id int, PRIMARY KEY (id))")
  1126. except mysql.connector.ProgrammingError as err:
  1127. if err.errno == errorcode.ER_SYNTAX_ERROR:
  1128. print("Check your syntax!")
  1129. else:
  1130. print("Error: {}".format(err))</pre><p>
  1131. <code class="literal">errors.ProgrammingError</code> is a subclass of
  1132. <code class="literal">errors.DatabaseError</code>.
  1133. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-integrityerror"></a>7.1.9. Exception <code class="literal">errors.IntegrityError</code></h3></div></div></div><p>
  1134. This exception is raised when the relational integrity of the
  1135. data is affected. For example, a duplicate key was inserted or a
  1136. foreign key constraint would fail.
  1137. </p><p>
  1138. The following example shows a duplicate key error raised as
  1139. IntegrityError:
  1140. </p><pre class="programlisting">cursor.execute("CREATE TABLE t1 (id int, PRIMARY KEY (id))")
  1141. try:
  1142. cursor.execute("INSERT INTO t1 (id) VALUES (1)")
  1143. cursor.execute("INSERT INTO t1 (id) VALUES (1)")
  1144. except mysql.connector.IntegrityError as err:
  1145. print("Error: {}".format(err))</pre><p>
  1146. <code class="literal">errors.IntegrityError</code> is a subclass of
  1147. <code class="literal">errors.DatabaseError</code>.
  1148. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-dataerror"></a>7.1.10. Exception <code class="literal">errors.DataError</code></h3></div></div></div><p>
  1149. This exception is raised when there were problems with the data.
  1150. Examples are a column set to NULL when it can not, out of range
  1151. values for a column, division by zero, column count does not
  1152. match value count, and so on.
  1153. </p><p>
  1154. <code class="literal">errors.DataError</code> is a subclass of
  1155. <code class="literal">errors.DatabaseError</code>.
  1156. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-notsupportederror"></a>7.1.11. Exception <code class="literal">errors.NotSupportedError</code></h3></div></div></div><p>
  1157. This exception is raised is case some feature was used but not
  1158. supported by the version of MySQL which returned the error. It
  1159. is also raised when using functions or statements which are not
  1160. supported by stored routines.
  1161. </p><p>
  1162. <code class="literal">errors.NotSupportedError</code> is a subclass of
  1163. <code class="literal">errors.DatabaseError</code>.
  1164. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="connector-python-apiref-errors-custom-error-exception"></a>7.1.12. Function <code class="literal">errors.custom_error_exception(error=None,
  1165. exception=None)</code></h3></div></div></div><p>
  1166. This function defines custom exceptions for MySQL server errors
  1167. and returns current customizations.
  1168. </p><p>
  1169. If <code class="literal">error</code> is a MySQL Server error number, then
  1170. you have to pass also the <code class="literal">exception</code> class.
  1171. The <code class="literal">error</code> argument can also be a dictionary
  1172. in which case the key is the server error number, and value the
  1173. class of the exception to be raised.
  1174. </p><p>
  1175. To reset the customizations, simply supply an empty dictionary.
  1176. </p><pre class="programlisting">import mysql.connector
  1177. from mysql.connector import errorcode
  1178. # Server error 1028 should raise a DatabaseError
  1179. mysql.connector.custom_error_exception(1028, mysql.connector.DatabaseError)
  1180. # Or using a dictionary:
  1181. mysql.connector.custom_error_exception({
  1182. 1028: mysql.connector.DatabaseError,
  1183. 1029: mysql.connector.OperationalError,
  1184. })
  1185. # To reset, pass an empty dictionary:
  1186. mysql.connector.custom_error_exception({})</pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-MySQLConnection"></a>7.2. Class <code class="literal">connection.MySQLConnection</code></h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="#myconnpy_MySQLConnection">7.2.1. Constructor <code class="literal">connection.MySQLConnection(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_close">7.2.2. Method <code class="literal">MySQLConnection.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_config">7.2.3. Method <code class="literal">MySQLConnection.config(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connect">7.2.4. Method <code class="literal">MySQLConnection.connect(**kwargs)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_commit">7.2.5. Method <code class="literal">MySQLConnection.commit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cursor">7.2.6. Method <code class="literal">MySQLConnection.cursor(buffered=None, raw=None,
  1187. cursor_class=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_change_user">7.2.7. Method <code class="literal">MySQLConnection.cmd_change_user(username='',
  1188. password='', database='', charset=33)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_debug">7.2.8. Method <code class="literal">MySQLConnection.cmd_debug()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_init_db">7.2.9. Method <code class="literal">MySQLConnection.cmd_init_db(database)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_ping">7.2.10. Method <code class="literal">MySQLConnection.cmd_ping()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_info">7.2.11. Method <code class="literal">MySQLConnection.cmd_process_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_process_kill">7.2.12. Method <code class="literal">MySQLConnection.cmd_process_kill(mysql_pid)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_quit">7.2.13. Method <code class="literal">MySQLConnection.cmd_quit()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query">7.2.14. Method <code class="literal">MySQLConnection.cmd_query(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_query_iter">7.2.15. Method <code class="literal">MySQLConnection.cmd_query_iter(statement)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_refresh">7.2.16. Method <code class="literal">MySQLConnection.cmd_refresh(options)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_shutdown">7.2.17. Method <code class="literal">MySQLConnection.cmd_shutdown()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_cmd_statistics">7.2.18. Method <code class="literal">MySQLConnection.cmd_statistics()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_disconnect">7.2.19. Method <code class="literal">MySQLConnection.disconnect()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_rows">7.2.20. Method <code class="literal">MySQLConnection.get_rows(count=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_row">7.2.21. Method <code class="literal">MySQLConnection.get_row()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_info">7.2.22. Method <code class="literal">MySQLConnection.get_server_info()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_server_version">7.2.23. Method <code class="literal">MySQLConnection.get_server_version()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_is_connected">7.2.24. Method <code class="literal">MySQLConnection.is_connected()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_isset_client_flag">7.2.25. Method <code class="literal">MySQLConnection.isset_client_flag(flag)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_ping">7.2.26. Method <code class="literal">MySQLConnection.ping(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnectoin_reconnect">7.2.27. Method <code class="literal">MySQLConnection.reconnect(attempts=1, delay=0)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_rollback">7.2.28. Method <code class="literal">MySQLConnection.rollback()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_charset_collation">7.2.29. Method <code class="literal">MySQLConnection.set_charset_collation(charset=None,
  1189. collation=None)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_set_client_flags">7.2.30. Method <code class="literal">MySQLConnection.set_client_flags(flags)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_autocommit">7.2.31. Property <code class="literal">MySQLConnection.autocommit</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_charset_name">7.2.32. Property <code class="literal">MySQLConnection.charset_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_collation_name">7.2.33. Property <code class="literal">MySQLConnection.collation_name</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_connection_id">7.2.34. Property <code class="literal">MySQLConnection.connection_id</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_database">7.2.35. Property <code class="literal">MySQLConnection.database</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_get_warnings">7.2.36. Property <code class="literal">MySQLConnection.get_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_raise_on_warnings">7.2.37. Property <code class="literal">MySQLConnection.raise_on_warnings</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_host">7.2.38. Property <code class="literal">MySQLConnection.server_host</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_server_port">7.2.39. Property <code class="literal">MySQLConnection.server_port</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_sql_mode">7.2.40. Property <code class="literal">MySQLConnection.sql_mode</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_time_zone">7.2.41. Property <code class="literal">MySQLConnection.time_zone</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_unix_socket">7.2.42. Property <code class="literal">MySQLConnection.unix_socket</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLConnection_user">7.2.43. Property <code class="literal">MySQLConnection.user</code></a></span></dt></dl></div><p>
  1190. The MySQLConnection class is used to open and manage a connection
  1191. to a MySQL server. It also used to send commands and SQL queries
  1192. and read result.
  1193. </p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection"></a>7.2.1. Constructor <code class="literal">connection.MySQLConnection(**kwargs)</code></h3></div></div></div><p>
  1194. The MySQLConnection constructor initializes the attributes and
  1195. when at least one argument is passed, it tries to connect with
  1196. the MySQL server.
  1197. </p><p>
  1198. For a complete list or arguments, see
  1199. <a class="xref" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">Chapter 6, <i>Connector/Python Connection Arguments</i></a>.
  1200. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_close"></a>7.2.2. Method <code class="literal">MySQLConnection.close()</code></h3></div></div></div><p>
  1201. See
  1202. <a class="link" href="#myconnpy_MySQLConnection_disconnect" title="7.2.19. Method MySQLConnection.disconnect()">disconnect()</a>.
  1203. </p><p>
  1204. Returns a tuple.
  1205. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_config"></a>7.2.3. Method <code class="literal">MySQLConnection.config(**kwargs)</code></h3></div></div></div><p>
  1206. Allows to configure a MySQLConnection instance after it was
  1207. instantiated. See <a class="xref" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">Chapter 6, <i>Connector/Python Connection Arguments</i></a>
  1208. for a complete list of possible arguments.
  1209. </p><p>
  1210. You could use the <code class="literal">config()</code> method to change,
  1211. for example, the username and call
  1212. <code class="literal">reconnect()</code>.
  1213. </p><pre class="programlisting">cnx = MySQLConnection(user='joe', database='test')
  1214. # Connected as 'joe'
  1215. cnx.config(user='jane')
  1216. cnx.reconnect()
  1217. # Now connected as 'jane'</pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_connect"></a>7.2.4. Method <code class="literal">MySQLConnection.connect(**kwargs)</code></h3></div></div></div><p>
  1218. This method sets up the connection to the MySQL server. If no
  1219. arguments are given, it uses the already configured or default
  1220. values. See <a class="xref" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">Chapter 6, <i>Connector/Python Connection Arguments</i></a> for a
  1221. complete list of possible arguments.
  1222. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_commit"></a>7.2.5. Method <code class="literal">MySQLConnection.commit()</code></h3></div></div></div><p>
  1223. This method sends the <code class="literal">COMMIT</code> command to the
  1224. MySQL server, committing the current transaction. Since by
  1225. default, Connector/Python does not auto commit, it is important
  1226. to call this method after every transaction which updates data
  1227. for tables using transactional storage engines.
  1228. </p><p>
  1229. See the
  1230. <a class="link" href="#myconnpy_MySQLConnection_rollback" title="7.2.28. Method MySQLConnection.rollback()">rollback()</a>
  1231. method for rolling back transactions.
  1232. </p><pre class="programlisting">
  1233. &gt;&gt;&gt; cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane'))
  1234. &gt;&gt;&gt; cnx.commit()
  1235. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cursor"></a>7.2.6. Method <code class="literal">MySQLConnection.cursor(buffered=None, raw=None,
  1236. cursor_class=None)</code></h3></div></div></div><p>
  1237. This method returns a MySQLCursor() object, or a subclass of it
  1238. depending the passed arguments.
  1239. </p><p>
  1240. When <code class="literal">buffered</code> is True, the cursor will fetch
  1241. all rows after the operation is executed. This is useful when
  1242. queries return small result sets. Setting <code class="literal">raw</code>
  1243. will skip the conversion from MySQL data types to Python types
  1244. when fetching rows. Raw is usually used when you want to have
  1245. more performance and/or you want to do the conversion yourself.
  1246. </p><p>
  1247. The <code class="literal">cursor_class</code> argument can be used to pass
  1248. a class to use for instantiating a new cursor. It has to be a
  1249. subclass of <code class="literal">cursor.CursorBase</code>.
  1250. </p><p>
  1251. The returned object depends on the combination of the
  1252. <code class="literal">buffered</code> and <code class="literal">raw</code>
  1253. arguments.
  1254. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  1255. If not buffered and not raw:
  1256. <code class="literal">cursor.MySQLCursor</code>
  1257. </p></li><li class="listitem"><p>
  1258. If buffered and not raw:
  1259. <code class="literal">cursor.MySQLCursorBuffered</code>
  1260. </p></li><li class="listitem"><p>
  1261. If buffered and raw:
  1262. <code class="literal">cursor.MySQLCursorBufferedRaw</code>
  1263. </p></li><li class="listitem"><p>
  1264. If not buffered and raw:
  1265. <code class="literal">cursor.MySQLCursorRaw</code>
  1266. </p></li></ul></div><p>
  1267. Returns a <code class="literal">CursorBase</code> instance.
  1268. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_change_user"></a>7.2.7. Method <code class="literal">MySQLConnection.cmd_change_user(username='',
  1269. password='', database='', charset=33)</code></h3></div></div></div><p>
  1270. Changes the user using <code class="literal">username</code> and
  1271. <code class="literal">password</code>. It also causes the specified
  1272. <code class="literal">database</code> to become the default (current)
  1273. database. It is also possible to change the character set using
  1274. the <code class="literal">charset</code> argument.
  1275. </p><p>
  1276. Returns a dictionary containing the OK packet information.
  1277. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_debug"></a>7.2.8. Method <code class="literal">MySQLConnection.cmd_debug()</code></h3></div></div></div><p>
  1278. Instructs the server to write some debug information to the log.
  1279. For this to work, the connected user must have the
  1280. <code class="literal">SUPER</code> privilege.
  1281. </p><p>
  1282. Returns a dictionary containing the OK packet information.
  1283. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_init_db"></a>7.2.9. Method <code class="literal">MySQLConnection.cmd_init_db(database)</code></h3></div></div></div><p>
  1284. This method makes specified database the default (current)
  1285. database. In subsequent queries, this database is the default
  1286. for table references that do not include an explicit database
  1287. specifier.
  1288. </p><p>
  1289. Returns a dictionary containing the OK packet information.
  1290. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_ping"></a>7.2.10. Method <code class="literal">MySQLConnection.cmd_ping()</code></h3></div></div></div><p>
  1291. Checks whether the connection to the server is working.
  1292. </p><p>
  1293. This method is not to be used directly. Use
  1294. <a class="link" href="#myconnpy_MySQLConnection_ping" title="7.2.26. Method MySQLConnection.ping(attempts=1, delay=0)">ping()</a>
  1295. or
  1296. <a class="link" href="#myconnpy_MySQLConnection_is_connected" title="7.2.24. Method MySQLConnection.is_connected()">is_connected()</a>
  1297. instead.
  1298. </p><p>
  1299. Returns a dictionary containing the OK packet information.
  1300. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_process_info"></a>7.2.11. Method <code class="literal">MySQLConnection.cmd_process_info()</code></h3></div></div></div><p>
  1301. This method raises the NotSupportedError exception. Instead, use
  1302. the <code class="literal">SHOW PROCESSLIST</code> statement or query the
  1303. tables found in the database
  1304. <code class="literal">INFORMATION_SCHEMA</code>.
  1305. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_process_kill"></a>7.2.12. Method <code class="literal">MySQLConnection.cmd_process_kill(mysql_pid)</code></h3></div></div></div><p>
  1306. Asks the server to kill the thread specified by
  1307. <code class="literal">mysql_pid</code>. Although still available, it's
  1308. better to use the SQL <code class="literal">KILL</code> command.
  1309. </p><p>
  1310. Returns a dictionary containing the OK packet information.
  1311. </p><p>
  1312. The following two lines do the same:
  1313. </p><pre class="programlisting">
  1314. &gt;&gt;&gt; cnx.cmd_process_kill(123)
  1315. &gt;&gt;&gt; cnx.cmd_query('KILL 123')
  1316. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_quit"></a>7.2.13. Method <code class="literal">MySQLConnection.cmd_quit()</code></h3></div></div></div><p>
  1317. This method sends the <code class="literal">QUIT</code> command to the
  1318. MySQL server, closing the current connection. Since there is no
  1319. response from the MySQL, the packet that was sent is returned.
  1320. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_query"></a>7.2.14. Method <code class="literal">MySQLConnection.cmd_query(statement)</code></h3></div></div></div><p>
  1321. This method sends the given <code class="literal">statement</code> to the
  1322. MySQL server and returns a result. If you need to send multiple
  1323. statements, you have to use the
  1324. <a class="link" href="#myconnpy_MySQLConnection_cmd_query_iter" title="7.2.15. Method MySQLConnection.cmd_query_iter(statement)">cmd_query_iter()</a>
  1325. method.
  1326. </p><p>
  1327. The returned dictionary contains information depending on what
  1328. kind of query was executed. If the query is a
  1329. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/select.html" target="_top"><code class="literal">SELECT</code></a> statement, the result
  1330. contains information about columns. Other statements return a
  1331. dictionary containing OK or EOF packet information.
  1332. </p><p>
  1333. Errors received from the MySQL server are raised as exceptions.
  1334. An <code class="literal">InterfaceError</code> is raised when multiple
  1335. results are found.
  1336. </p><p>
  1337. Returns a dictionary.
  1338. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_query_iter"></a>7.2.15. Method <code class="literal">MySQLConnection.cmd_query_iter(statement)</code></h3></div></div></div><p>
  1339. Similar to the
  1340. <a class="link" href="#myconnpy_MySQLConnection_cmd_query" title="7.2.14. Method MySQLConnection.cmd_query(statement)">cmd_query()</a>
  1341. method, but returns a generator object to iterate through
  1342. results. Use <code class="literal">cmd_query_iter()</code> when sending
  1343. multiple statements, and separate the statements with
  1344. semicolons.
  1345. </p><p>
  1346. The following example shows how to iterate through the results
  1347. after sending multiple statements:
  1348. </p><pre class="programlisting">statement = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
  1349. for result in cnx.cmd_query(statement, iterate=True):
  1350. if 'columns' in result:
  1351. columns = result['columns']
  1352. rows = cnx.get_rows()
  1353. else:
  1354. # do something useful with INSERT result</pre><p>
  1355. Returns a generator object.
  1356. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_refresh"></a>7.2.16. Method <code class="literal">MySQLConnection.cmd_refresh(options)</code></h3></div></div></div><p>
  1357. This method flushes tables or caches, or resets replication
  1358. server information. The connected user must have the
  1359. <code class="literal">RELOAD</code> privilege.
  1360. </p><p>
  1361. The <code class="literal">options</code> argument should be a bitwise
  1362. value using constants from the class
  1363. <code class="literal">constants.RefreshOption</code>.
  1364. </p><p>
  1365. See <a class="xref" href="#connector-python-apiref-RefreshOption" title="7.9. Class constants.RefreshOption">Section 7.9, Class <code class="literal">constants.RefreshOption</code></a> for
  1366. a list of options.
  1367. </p><p>
  1368. Example:
  1369. </p><pre class="programlisting">
  1370. &gt;&gt;&gt; from mysql.connector import RefreshOption
  1371. &gt;&gt;&gt; refresh = RefreshOption.LOG | RefreshOption.THREADS
  1372. &gt;&gt;&gt; cnx.cmd_refresh(refresh)
  1373. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_shutdown"></a>7.2.17. Method <code class="literal">MySQLConnection.cmd_shutdown()</code></h3></div></div></div><p>
  1374. Asks the database server to shut down. The connected user must
  1375. have the <code class="literal">SHUTDOWN</code> privilege.
  1376. </p><p>
  1377. Returns a dictionary containing the OK packet information.
  1378. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_cmd_statistics"></a>7.2.18. Method <code class="literal">MySQLConnection.cmd_statistics()</code></h3></div></div></div><p>
  1379. Returns a dictionary containing information about the MySQL
  1380. server including uptime in seconds and the number of running
  1381. threads, questions, reloads, and open tables.
  1382. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_disconnect"></a>7.2.19. Method <code class="literal">MySQLConnection.disconnect()</code></h3></div></div></div><p>
  1383. This method tries to send the <code class="literal">QUIT</code> command
  1384. and close the socket. It does not raise any exceptions.
  1385. </p><p>
  1386. <code class="literal">MySQLConnection.close()</code> is a synonymous
  1387. method name and more commonly used.
  1388. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_get_rows"></a>7.2.20. Method <code class="literal">MySQLConnection.get_rows(count=None)</code></h3></div></div></div><p>
  1389. This method retrieves all or remaining rows of a query result
  1390. set, returning a tuple containing the rows as sequence and the
  1391. EOF packet information. The count argument can be used to get a
  1392. given amount of rows. If count is not specified or is
  1393. <code class="literal">None</code>, all rows are retrieved.
  1394. </p><p>
  1395. The tuple returned by <code class="literal">get_rows()</code> consists of:
  1396. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  1397. A list of tuples containing the row data as byte objects, or
  1398. an empty list when no rows are available.
  1399. </p></li><li class="listitem"><p>
  1400. EOF packet information as a dictionary containing
  1401. <code class="literal">status_flag</code> and
  1402. <code class="literal">warning_count</code>.
  1403. </p></li></ul></div><p>
  1404. An <code class="literal">InterfaceError</code> is raised when all rows
  1405. have been retrieved.
  1406. </p><p>
  1407. The <code class="literal">get_rows()</code> method is used by
  1408. <a class="link" href="#connector-python-apiref-MySQLCursor" title="7.3. Class cursor.MySQLCursor">MySQLCursor</a>
  1409. to fetch rows.
  1410. </p><p>
  1411. Returns a tuple.
  1412. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_get_row"></a>7.2.21. Method <code class="literal">MySQLConnection.get_row()</code></h3></div></div></div><p>
  1413. This method retrieves the next row of a query result set,
  1414. returning a tuple.
  1415. </p><p>
  1416. The tuple returned by <code class="literal">get_row()</code> consists of:
  1417. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  1418. The row as a tuple containing byte objects, or
  1419. <code class="literal">None</code> when no more rows are available.
  1420. </p></li><li class="listitem"><p>
  1421. EOF packet information as a dictionary containing
  1422. <code class="literal">status_flag</code> and
  1423. <code class="literal">warning_count</code>, or <code class="literal">None</code>
  1424. when the row returned is not the last row.
  1425. </p></li></ul></div><p>
  1426. The <code class="literal">get_row()</code> method is used by
  1427. <a class="link" href="#connector-python-apiref-MySQLCursor" title="7.3. Class cursor.MySQLCursor">MySQLCursor</a>
  1428. to fetch rows.
  1429. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_get_server_info"></a>7.2.22. Method <code class="literal">MySQLConnection.get_server_info()</code></h3></div></div></div><p>
  1430. This method returns the MySQL server information verbatim, for
  1431. example <code class="literal">'5.5.24-log'</code>, or
  1432. <code class="literal">None</code> when not connected.
  1433. </p><p>
  1434. Returns a string or <code class="literal">None</code>.
  1435. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_get_server_version"></a>7.2.23. Method <code class="literal">MySQLConnection.get_server_version()</code></h3></div></div></div><p>
  1436. This method returns the MySQL server version as a tuple, or
  1437. <code class="literal">None</code> when not connected.
  1438. </p><p>
  1439. Returns a tuple or <code class="literal">None</code>.
  1440. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_is_connected"></a>7.2.24. Method <code class="literal">MySQLConnection.is_connected()</code></h3></div></div></div><p>
  1441. Reports whether the connection to MySQL Server is available.
  1442. </p><p>
  1443. This method checks whether the connection to MySQL is available
  1444. using the
  1445. <a class="link" href="#myconnpy_MySQLConnection_ping" title="7.2.26. Method MySQLConnection.ping(attempts=1, delay=0)">ping()</a>
  1446. method, but unlike <code class="literal">ping()</code>,
  1447. <code class="literal">is_connected()</code> returns
  1448. <code class="literal">True</code> when the connection is available,
  1449. <code class="literal">False</code> otherwise.
  1450. </p><p>
  1451. Returns <code class="literal">True</code> or <code class="literal">False</code>.
  1452. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_isset_client_flag"></a>7.2.25. Method <code class="literal">MySQLConnection.isset_client_flag(flag)</code></h3></div></div></div><p>
  1453. This method returns <code class="literal">True</code> if the client flag
  1454. was set, <code class="literal">False</code> otherwise.
  1455. </p><p>
  1456. Returns <code class="literal">True</code> or <code class="literal">False</code>.
  1457. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_ping"></a>7.2.26. Method <code class="literal">MySQLConnection.ping(attempts=1, delay=0)</code></h3></div></div></div><p>
  1458. Check whether the connection to the MySQL server is still
  1459. available.
  1460. </p><p>
  1461. When <code class="literal">reconnect</code> is set to
  1462. <code class="literal">True</code>, one or more attempts are made to try to
  1463. reconnect to the MySQL server using the
  1464. <code class="literal">reconnect()</code> method. Use the
  1465. <code class="literal">delay</code> argument (seconds) if you want to wait
  1466. between each retry.
  1467. </p><p>
  1468. When the connection is not available, an
  1469. <code class="literal">InterfaceError</code> is raised. Use the
  1470. <a class="link" href="#myconnpy_MySQLConnection_is_connected" title="7.2.24. Method MySQLConnection.is_connected()">is_connected()</a>
  1471. method to check the connection without raising an error.
  1472. </p><p>
  1473. Raises <code class="literal">InterfaceError</code> on errors.
  1474. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnectoin_reconnect"></a>7.2.27. Method <code class="literal">MySQLConnection.reconnect(attempts=1, delay=0)</code></h3></div></div></div><p>
  1475. Attempt to reconnect with the MySQL server.
  1476. </p><p>
  1477. The argument <code class="literal">attempts</code> specifies the number of
  1478. times a reconnect is tried. The <code class="literal">delay</code>
  1479. argument is the number of seconds to wait between each retry.
  1480. </p><p>
  1481. You might set the number of attempts higher and use a longer
  1482. delay when you expect the MySQL server to be down for
  1483. maintenance, or when you expect the network to be temporarily
  1484. unavailable.
  1485. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_rollback"></a>7.2.28. Method <code class="literal">MySQLConnection.rollback()</code></h3></div></div></div><p>
  1486. This method sends the <code class="literal">ROLLBACK</code> command to the
  1487. MySQL server, undoing all data changes from the current
  1488. <code class="literal">transaction</code>. Since by default,
  1489. Connector/Python does not auto commit, it is possible to cancel
  1490. transactions when using transactional storage engines such as
  1491. <code class="literal">InnoDB</code>.
  1492. </p><p>
  1493. See the
  1494. <a class="link" href="#myconnpy_MySQLConnection_commit" title="7.2.5. Method MySQLConnection.commit()">commit()</a>
  1495. method for <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/glossary.html#glos_commit" target="_top">committing</a>
  1496. transactions.
  1497. </p><pre class="programlisting">
  1498. &gt;&gt;&gt; cursor.execute("INSERT INTO employees (first_name) VALUES (%s)", ('Jane'))
  1499. &gt;&gt;&gt; cnx.rollback()
  1500. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_set_charset_collation"></a>7.2.29. Method <code class="literal">MySQLConnection.set_charset_collation(charset=None,
  1501. collation=None)</code></h3></div></div></div><p>
  1502. This method sets the character set and collation to be used for
  1503. the current connection. The <code class="literal">charset</code> argument
  1504. can be either the name of a character set, or the numerical
  1505. equivalent as defined in
  1506. <code class="literal">constants.CharacterSet</code>.
  1507. </p><p>
  1508. When <code class="literal">collation</code> is <code class="literal">None</code>,
  1509. the default will be looked up and used.
  1510. </p><p>
  1511. The <code class="literal">charset</code> argument then be either:
  1512. </p><p>
  1513. In the following example, we set the character set to
  1514. <code class="literal">latin1</code> and the collation will be set to the
  1515. default <code class="literal">latin1_swedish_ci</code>:
  1516. </p><pre class="programlisting">
  1517. &gt;&gt;&gt; cnx = mysql.connector.connect(user='scott')
  1518. &gt;&gt;&gt; cnx.set_charset('latin1')
  1519. </pre><p>
  1520. Specify a specific collation as follows:
  1521. </p><pre class="programlisting">
  1522. &gt;&gt;&gt; cnx = mysql.connector.connect(user='scott')
  1523. &gt;&gt;&gt; cnx.set_charset('latin1', 'latin1_general_ci')
  1524. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_set_client_flags"></a>7.2.30. Method <code class="literal">MySQLConnection.set_client_flags(flags)</code></h3></div></div></div><p>
  1525. This method sets the client flags which are used when connecting
  1526. with the MySQL server and returns the new value. The
  1527. <code class="literal">flags</code> argument can be either an integer or a
  1528. sequence of valid client flag values (see
  1529. <a class="xref" href="#connector-python-apiref-ClientFlag" title="7.5. Class constants.ClientFlag">Section 7.5, Class <code class="literal">constants.ClientFlag</code></a>).
  1530. </p><p>
  1531. If <code class="literal">flags</code> is a sequence, each item in the
  1532. sequence will set the flag when the value is positive or unset
  1533. it when negative. For example, to unset
  1534. <code class="literal">LONG_FLAG</code> and set the
  1535. <code class="literal">FOUND_ROWS</code> flags:
  1536. </p><pre class="programlisting">
  1537. &gt;&gt;&gt; from mysql.connector.constants import ClientFlag
  1538. &gt;&gt;&gt; cnx.set_client_flags([ClientFlag.FOUND_ROWS, -ClientFlag.LONG_FLAG])
  1539. &gt;&gt;&gt; cnx.reconnect()
  1540. </pre><p>
  1541. Note that client flags are only set or used when connecting with
  1542. the MySQL server. It is therefor necessary to reconnect after
  1543. making changes.
  1544. </p><p>
  1545. Returns an integer.
  1546. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_autocommit"></a>7.2.31. Property <code class="literal">MySQLConnection.autocommit</code></h3></div></div></div><p>
  1547. This property is used to toggle the auto commit feature of MySQL
  1548. and retrieve the current state. When the value evaluates to
  1549. True, auto commit will be turned, otherwise it is turned off.
  1550. </p><p>
  1551. Note that auto commit is disabled by default when connecting
  1552. through Connector/Python. This can be toggled using the
  1553. <a class="link" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">connection
  1554. parameter</a> <code class="literal">autocommit</code>.
  1555. </p><p>
  1556. When the auto commit is turned off, you have to
  1557. <a class="link" href="#myconnpy_MySQLConnection_commit" title="7.2.5. Method MySQLConnection.commit()">commit</a>
  1558. transactions when using transactional storage engines such as
  1559. InnoDB or NDBCluster.
  1560. </p><pre class="programlisting">
  1561. &gt;&gt;&gt; cnx.autocommit
  1562. False
  1563. &gt;&gt;&gt; cnx.autocommit = True
  1564. &gt;&gt;&gt; cnx.autocommit
  1565. True
  1566. </pre><p>
  1567. Returns True or False.
  1568. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_charset_name"></a>7.2.32. Property <code class="literal">MySQLConnection.charset_name</code></h3></div></div></div><p>
  1569. This property returns which character set is used for the
  1570. connection whether it is connected or not.
  1571. </p><p>
  1572. Returns a string.
  1573. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_collation_name"></a>7.2.33. Property <code class="literal">MySQLConnection.collation_name</code></h3></div></div></div><p>
  1574. This property returns which collation is used for the connection
  1575. whether it is connected or not.
  1576. </p><p>
  1577. Returns a string.
  1578. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_connection_id"></a>7.2.34. Property <code class="literal">MySQLConnection.connection_id</code></h3></div></div></div><p>
  1579. This property returns the connection ID (thread ID or session
  1580. ID) for the current connection or None when not connected.
  1581. </p><p>
  1582. Returns a integer or None.
  1583. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_database"></a>7.2.35. Property <code class="literal">MySQLConnection.database</code></h3></div></div></div><p>
  1584. This property is used to set current (active) database executing
  1585. the <code class="literal">USE</code> command. The property can also be
  1586. used to retrieve the current database name.
  1587. </p><pre class="programlisting">
  1588. &gt;&gt;&gt; cnx.database = 'test'
  1589. &gt;&gt;&gt; cnx.database = 'mysql'
  1590. &gt;&gt;&gt; cnx.database
  1591. u'mysql'
  1592. </pre><p>
  1593. Returns a string.
  1594. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_get_warnings"></a>7.2.36. Property <code class="literal">MySQLConnection.get_warnings</code></h3></div></div></div><p>
  1595. This property is used to toggle whether warnings should be
  1596. fetched automatically or not. It accepts True or False
  1597. (default).
  1598. </p><p>
  1599. Fetching warnings automatically could be useful when debugging
  1600. queries. Cursors will make warnings available through the method
  1601. <a class="link" href="#myconnpy_MySQLCursor_fetchwarnings" title="7.3.9. Method MySQLCursor.fetchwarnings()">MySQLCursor.fetchwarnings()</a>.
  1602. </p><pre class="programlisting">
  1603. &gt;&gt;&gt; cnx.get_warnings = True
  1604. &gt;&gt;&gt; cursor.execute('SELECT "a"+1')
  1605. &gt;&gt;&gt; cursor.fetchall()
  1606. [(1.0,)]
  1607. &gt;&gt;&gt; cursor.fetchwarnings()
  1608. [(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'")]
  1609. </pre><p>
  1610. Returns True or False.
  1611. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_raise_on_warnings"></a>7.2.37. Property <code class="literal">MySQLConnection.raise_on_warnings</code></h3></div></div></div><p>
  1612. This property is used to toggle whether warnings should raise
  1613. exceptions or not. It accepts True or False (default).
  1614. </p><p>
  1615. Toggling <code class="literal">raise_on_warnings</code> will also toggle
  1616. <code class="literal">get_warnings</code> since warnings need to be
  1617. fetched so they can be raised as exceptions.
  1618. </p><p>
  1619. Note that you might always want to check setting SQL Mode if you
  1620. would like to have the MySQL server directly report warnings as
  1621. errors. It is also good to use transactional engines so
  1622. transactions can be rolled back when catching the exception.
  1623. </p><p>
  1624. Result sets needs to be fetched completely before any exception
  1625. can be raised. The following example shows the execution of a
  1626. query which produces a warning
  1627. </p><pre class="programlisting">
  1628. &gt;&gt;&gt; cnx.raise_on_warnings = True
  1629. &gt;&gt;&gt; cursor.execute('SELECT "a"+1')
  1630. &gt;&gt;&gt; cursor.fetchall()
  1631. ..
  1632. mysql.connector.errors.DataError: 1292: Truncated incorrect DOUBLE value: 'a'
  1633. </pre><p>
  1634. Returns True or False.
  1635. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_server_host"></a>7.2.38. Property <code class="literal">MySQLConnection.server_host</code></h3></div></div></div><p>
  1636. This read-only property returns the hostname or IP address used
  1637. for connecting with the MySQL server.
  1638. </p><p>
  1639. Returns a string.
  1640. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_server_port"></a>7.2.39. Property <code class="literal">MySQLConnection.server_port</code></h3></div></div></div><p>
  1641. This read-only property returns the TCP/IP port used for
  1642. connecting with the MySQL server.
  1643. </p><p>
  1644. Returns a integer.
  1645. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_sql_mode"></a>7.2.40. Property <code class="literal">MySQLConnection.sql_mode</code></h3></div></div></div><p>
  1646. This property is used to retrieve and set the SQL Modes for the
  1647. current. The value should be list of different modes separated
  1648. by comma (","), or a sequence of modes, preferably using the
  1649. constants.SQLMode class.
  1650. </p><p>
  1651. To unset all modes, pass an empty string or an empty sequence.
  1652. </p><pre class="programlisting">
  1653. &gt;&gt;&gt; cnx.sql_mode = 'TRADITIONAL,NO_ENGINE_SUBSTITUTION'
  1654. &gt;&gt;&gt; cnx.sql_mode.split(',')
  1655. [u'STRICT_TRANS_TABLES', u'STRICT_ALL_TABLES', u'NO_ZERO_IN_DATE',
  1656. u'NO_ZERO_DATE', u'ERROR_FOR_DIVISION_BY_ZERO', u'TRADITIONAL',
  1657. u'NO_AUTO_CREATE_USER', u'NO_ENGINE_SUBSTITUTION']
  1658. &gt;&gt;&gt; from mysql.connector.constants import SQLMode
  1659. &gt;&gt;&gt; cnx.sql_mode = [ SQLMode.NO_ZERO_DATE, SQLMode.REAL_AS_FLOAT]
  1660. &gt;&gt;&gt; cnx.sql_mode
  1661. u'REAL_AS_FLOAT,NO_ZERO_DATE'</pre><p>
  1662. Returns a string.
  1663. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_time_zone"></a>7.2.41. Property <code class="literal">MySQLConnection.time_zone</code></h3></div></div></div><p>
  1664. This property is used to set the time zone session variable for
  1665. the current connection and retrieve it.
  1666. </p><pre class="programlisting">
  1667. &gt;&gt;&gt; cnx.time_zone = '+00:00'
  1668. &gt;&gt;&gt; cur.execute('SELECT NOW()') ; cur.fetchone()
  1669. (datetime.datetime(2012, 6, 15, 11, 24, 36),)
  1670. &gt;&gt;&gt; cnx.time_zone = '-09:00'
  1671. &gt;&gt;&gt; cur.execute('SELECT NOW()') ; cur.fetchone()
  1672. (datetime.datetime(2012, 6, 15, 2, 24, 44),)
  1673. &gt;&gt;&gt; cnx.time_zone
  1674. u'-09:00'
  1675. </pre><p>
  1676. Returns a string.
  1677. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_unix_socket"></a>7.2.42. Property <code class="literal">MySQLConnection.unix_socket</code></h3></div></div></div><p>
  1678. This read-only property returns the UNIX socket user for
  1679. connecting with the MySQL server.
  1680. </p><p>
  1681. Returns a string.
  1682. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLConnection_user"></a>7.2.43. Property <code class="literal">MySQLConnection.user</code></h3></div></div></div><p>
  1683. This read-only property returns the username used for connecting
  1684. with the MySQL server.
  1685. </p><p>
  1686. Returns a string.
  1687. </p></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-MySQLCursor"></a>7.3. Class <code class="literal">cursor.MySQLCursor</code></h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="#myconnpy_MySQLCursor">7.3.1. Constructor <code class="literal">cursor.MySQLCursor</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_callproc">7.3.2. Method <code class="literal">MySQLCursor.callproc(procname, args=())</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_close">7.3.3. Method <code class="literal">MySQLCursor.close()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_execute">7.3.4. Method <code class="literal">MySQLCursor.execute(operation, params=None,
  1688. multi=False)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_executemany">7.3.5. Method <code class="literal">MySQLCursor.executemany(operation, seq_params)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchall">7.3.6. Method <code class="literal">MySQLCursor.fetchall()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchmany">7.3.7. Method <code class="literal">MySQLCursor.fetchmany(size=1)</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchone">7.3.8. Method <code class="literal">MySQLCursor.fetchone()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_fetchwarnings">7.3.9. Method <code class="literal">MySQLCursor.fetchwarnings()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_stored_results">7.3.10. Method <code class="literal">MySQLCursor.stored_results()</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_column_names">7.3.11. Property <code class="literal">MySQLCursor.column_names</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_statement">7.3.12. Property <code class="literal">MySQLCursor.statement</code></a></span></dt><dt><span class="section"><a href="#myconnpy_MySQLCursor_with_rows">7.3.13. Property <code class="literal">MySQLCursor.with_rows</code></a></span></dt></dl></div><p>
  1689. The MySQLCursor class is used to instantiate object which can
  1690. execute operation such as SQL queries. They interact with the
  1691. MySQL server using a MySQLConnection object.
  1692. </p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor"></a>7.3.1. Constructor <code class="literal">cursor.MySQLCursor</code></h3></div></div></div><p>
  1693. The constructor initializes the instance with the optional
  1694. <code class="literal">connection</code>, which should be an instance of
  1695. <a class="link" href="#connector-python-apiref-MySQLConnection" title="7.2. Class connection.MySQLConnection">MySQLConnection</a>.
  1696. </p><p>
  1697. In most cases, the MySQLConnection method
  1698. <a class="link" href="#myconnpy_MySQLConnection_cursor" title="7.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)"><code class="literal">cursor()</code></a>
  1699. is used to instantiate a MySQLCursor object.
  1700. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_callproc"></a>7.3.2. Method <code class="literal">MySQLCursor.callproc(procname, args=())</code></h3></div></div></div><p>
  1701. This method calls a stored procedure with the given name. The
  1702. <code class="literal">args</code> sequence of parameters must contain one
  1703. entry for each argument that the routine expects. The result is
  1704. returned as modified copy of the input sequence. Input
  1705. parameters are left untouched, output and input/output
  1706. parameters replaced with possibly new values.
  1707. </p><p>
  1708. Result set provided by the stored procedure are automatically
  1709. fetched and stored as
  1710. <a class="link" href="#connector-python-apiref-MySQLCursorBuffered" title="7.4. Class cursor.MySQLCursorBuffered">MySQLBufferedCursor</a>
  1711. instances. See
  1712. <a class="link" href="#myconnpy_MySQLCursor_stored_results" title="7.3.10. Method MySQLCursor.stored_results()">stored_results()</a>
  1713. for more information.
  1714. </p><p>
  1715. The following example shows how to execute a stored procedure
  1716. which takes two parameters, multiplies the values and returns
  1717. the product:
  1718. </p><pre class="programlisting">
  1719. # Definition of the multiply stored procedure:
  1720. # CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
  1721. # BEGIN
  1722. # SET pProd := pFac1 * pFac2;
  1723. # END
  1724. &gt;&gt;&gt; args = (5, 5, 0) # 0 is to hold value of the OUT parameter pProd
  1725. &gt;&gt;&gt; cursor.callproc('multiply', args)
  1726. ('5', '5', 25L)
  1727. </pre></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_close"></a>7.3.3. Method <code class="literal">MySQLCursor.close()</code></h3></div></div></div><p>
  1728. This method will close the MySQL cursor, resetting all results
  1729. and removing the connection.
  1730. </p><p>
  1731. Use <code class="literal">close()</code> every time you are done using the
  1732. cursor.
  1733. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_execute"></a>7.3.4. Method <code class="literal">MySQLCursor.execute(operation, params=None,
  1734. multi=False)</code></h3></div></div></div><p>
  1735. This method prepare the given database
  1736. <code class="literal">operation</code> (query or command). The parameters
  1737. found in the tuple or dictionary <code class="literal">params</code> will
  1738. be bound to the variables in the operation. Variables are
  1739. specified using %s markers or named markers %(name)s.
  1740. </p><p>
  1741. For example, insert information about a new employee and
  1742. selecting again the data of this person:
  1743. </p><pre class="programlisting">insert = (
  1744. "INSERT INTO employees (emp_no, first_name, last_name, hire_date) "
  1745. "VALUES (%s, %s, %s, %s)")
  1746. data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
  1747. cursor.execute(insert, data)
  1748. select = "SELECT * FROM employees WHERE emp_no = %(emp_no)s"
  1749. cursor.execute(select, { 'emp_no': 2 })</pre><p>
  1750. Note that the data is converted from Python object to something
  1751. MySQL understand. In the above example, the datetime.date()
  1752. instance is converted to '2012-03-23' in the above example.
  1753. </p><p>
  1754. When <code class="literal">multi</code> is set to True,
  1755. <code class="literal">execute()</code> will be able to execute multiple
  1756. statements. It will return an iterator which makes it possible
  1757. to go through all results for each statement. Note that using
  1758. parameters is not working well in this case, and it's usually a
  1759. good idea to execute each statement on its own.
  1760. </p><p>
  1761. In the following example we select and insert data in one
  1762. operation and display the result:
  1763. </p><pre class="programlisting">operation = 'SELECT 1; INSERT INTO t1 VALUES (); SELECT 2'
  1764. for result in cursor.execute(operation):
  1765. if result.with_rows:
  1766. print("Statement '{}' has following rows:".format(
  1767. result.statement))
  1768. print(result.fetchall())
  1769. else:
  1770. print("Affected row(s) by query '{}' was {}".format(
  1771. result.statement, result.rowcount))</pre><p>
  1772. If the connection was configured to fetch warnings, warnings
  1773. generated by the operation will be available through the method
  1774. <a class="link" href="#myconnpy_MySQLCursor_fetchwarnings" title="7.3.9. Method MySQLCursor.fetchwarnings()">MySQLCursor.fetchwarnings()</a>.
  1775. </p><p>
  1776. Returns an iterator when multi is True.
  1777. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_executemany"></a>7.3.5. Method <code class="literal">MySQLCursor.executemany(operation, seq_params)</code></h3></div></div></div><p>
  1778. This method prepares a database operation (query or command) and
  1779. then execute it against all parameter sequences or mappings
  1780. found in the sequence <code class="literal">seq_of_params</code>.
  1781. </p><p>
  1782. The <code class="literal">executemany()</code> is simply iterating through
  1783. the sequence of parameters calling the
  1784. <code class="literal">execute()</code> method. Inserting data, however, is
  1785. optimized by batching them using the multiple rows syntax.
  1786. </p><p>
  1787. In the following example we are inserting 3 records:
  1788. </p><pre class="programlisting">data = [
  1789. ('Jane', date(2005, 2, 12)),
  1790. ('Joe', date(2006, 5, 23)),
  1791. ('John', date(2010, 10, 3)),
  1792. ]
  1793. stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
  1794. cursor.executemany(stmt, data)</pre><p>
  1795. In the above example, the INSERT statement sent to MySQL would
  1796. be as follows: <code class="literal">INSERT INTO employees (first_name,
  1797. hire_date) VALUES ('Jane', '2005-02-12'), ('Joe', '2006-05-23'),
  1798. ('John', '2010-10-03')</code>.
  1799. </p><p>
  1800. Note that it is not possible to execute multiple statements
  1801. using the <code class="literal">executemany()</code> method. Doing so will
  1802. raise an <code class="literal">InternalError</code> exception.
  1803. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_fetchall"></a>7.3.6. Method <code class="literal">MySQLCursor.fetchall()</code></h3></div></div></div><p>
  1804. The method fetches all or remaining rows of a query result set,
  1805. returning a list of tuples. An empty list is returned when no
  1806. rows are (anymore) available.
  1807. </p><p>
  1808. The following examples shows how to retrieve the first 2 rows of
  1809. a result set, and then retrieve the remaining rows:
  1810. </p><pre class="programlisting">
  1811. &gt;&gt;&gt; cursor.execute("SELECT * FROM employees ORDER BY emp_no")
  1812. &gt;&gt;&gt; head_rows = cursor.fetchmany(size=2)
  1813. &gt;&gt;&gt; remaining_rows = cursor.fetchall()
  1814. </pre><p>
  1815. Note that you have to fetch all rows before being able to
  1816. execute new queries using the same connection.
  1817. </p><p>
  1818. Returns a list of tuples or empty list when no rows available.
  1819. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_fetchmany"></a>7.3.7. Method <code class="literal">MySQLCursor.fetchmany(size=1)</code></h3></div></div></div><p>
  1820. This method fetches the next set of rows of a query results,
  1821. returning a list of tuples. An empty list is returned when no
  1822. more rows are available.
  1823. </p><p>
  1824. The number of rows returned can be specified using the size
  1825. argument, which defaults to one. Fewer rows might be returned,
  1826. when there are not more rows available than specified by the
  1827. argument.
  1828. </p><p>
  1829. Note that you have to fetch all rows before being able to
  1830. execute new queries using the same connection.
  1831. </p><p>
  1832. Returns a list of tuples or empty list when no rows available.
  1833. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_fetchone"></a>7.3.8. Method <code class="literal">MySQLCursor.fetchone()</code></h3></div></div></div><p>
  1834. This method retrieves the next row of a query result set,
  1835. returning a single sequence, or None when no more data is
  1836. available.The returned tuple consists of data returned by the
  1837. MySQL server converted to Python objects.
  1838. </p><p>
  1839. The <code class="literal">fetchone()</code> method is used by
  1840. <a class="link" href="#myconnpy_MySQLCursor_fetchmany" title="7.3.7. Method MySQLCursor.fetchmany(size=1)">fetchmany()</a>
  1841. and
  1842. <a class="link" href="#myconnpy_MySQLCursor_fetchall" title="7.3.6. Method MySQLCursor.fetchall()">fetchall()</a>.
  1843. It is also used when using the MySQLCursor instance as an
  1844. iterator.
  1845. </p><p>
  1846. The following examples show how to iterate through the result of
  1847. a query using <code class="literal">fetchone()</code>:
  1848. </p><pre class="programlisting"># Using a while-loop
  1849. cursor.execute("SELECT * FROM employees")
  1850. row = cursor.fetchone()
  1851. while row is not None:
  1852. print(row)
  1853. row = cursor.fetchone()
  1854. # Using the cursor as iterator
  1855. cursor.execute("SELECT * FROM employees")
  1856. for row in cursor:
  1857. print(row)</pre><p>
  1858. Note that you have to fetch all rows before being able to
  1859. execute new queries using the same connection.
  1860. </p><p>
  1861. Returns a tuple or None.
  1862. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_fetchwarnings"></a>7.3.9. Method <code class="literal">MySQLCursor.fetchwarnings()</code></h3></div></div></div><p>
  1863. This method returns a list of tuples containing warnings
  1864. generated by previously executed statement. Use the connection's
  1865. <code class="literal">get_warnings</code> property to toggle whether
  1866. warnings has to be fetched.
  1867. </p><p>
  1868. The following example shows a SELECT statement which generated a
  1869. warning:
  1870. </p><pre class="programlisting">
  1871. &gt;&gt;&gt; cnx.get_warnings = True
  1872. &gt;&gt;&gt; cursor.execute('SELECT "a"+1')
  1873. &gt;&gt;&gt; cursor.fetchall()
  1874. [(1.0,)]
  1875. &gt;&gt;&gt; cursor.fetchwarnings()
  1876. [(u'Warning', 1292, u"Truncated incorrect DOUBLE value: 'a'")]
  1877. </pre><p>
  1878. It is also possible to raise errors when warnings are found. See
  1879. the MySQLConnection property
  1880. <a class="link" href="#myconnpy_MySQLConnection_raise_on_warnings" title="7.2.37. Property MySQLConnection.raise_on_warnings">raise_on_warnings</a>.
  1881. </p><p>
  1882. Returns a list of tuples.
  1883. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_stored_results"></a>7.3.10. Method <code class="literal">MySQLCursor.stored_results()</code></h3></div></div></div><p>
  1884. This method returns an list iterator object which can be used to
  1885. go through result sets provided by stored procedures after
  1886. calling them using the
  1887. <a class="link" href="#myconnpy_MySQLCursor_callproc" title="7.3.2. Method MySQLCursor.callproc(procname, args=())">callproc()</a>
  1888. method.
  1889. </p><p>
  1890. In the following example we execute a stored procedure which
  1891. will provide two result sets. We use
  1892. <code class="literal">stored_results()</code> to retrieve them:
  1893. </p><pre class="programlisting">
  1894. &gt;&gt;&gt; cursor.callproc('sp1')
  1895. ()
  1896. &gt;&gt;&gt; for result in cursor.stored_results():
  1897. ... print result.fetchall()
  1898. ...
  1899. [(1,)]
  1900. [(2,)]
  1901. </pre><p>
  1902. Note that the result sets stay available until you executed
  1903. another operation or call another stored procedure.
  1904. </p><p>
  1905. Returns a listiterator.
  1906. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_column_names"></a>7.3.11. Property <code class="literal">MySQLCursor.column_names</code></h3></div></div></div><p>
  1907. This read-only property returns the column names of a result set
  1908. as sequence of (unicode) strings.
  1909. </p><p>
  1910. The following example shows how you can create a dictionary out
  1911. of a tuple containing data with keys using
  1912. <code class="literal">column_names</code>:
  1913. </p><pre class="programlisting">cursor.execute("SELECT last_name, first_name, hire_date "
  1914. "FROM employees WHERE emp_no = %s", (123,))
  1915. row = dict(zip(cursor.column_names, cursor.fetchone())
  1916. print("{last_name}, {first_name}: {hire_date}".format(row))</pre><p>
  1917. Returns a tuple.
  1918. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_statement"></a>7.3.12. Property <code class="literal">MySQLCursor.statement</code></h3></div></div></div><p>
  1919. This read-only property returns the last executed statement. In
  1920. case multiple statements where executed, it will show the actual
  1921. statement.
  1922. </p><p>
  1923. The <code class="literal">statement</code> property might be useful for
  1924. debugging and showing what was send to the MySQL server.
  1925. </p><p>
  1926. Returns a string.
  1927. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="myconnpy_MySQLCursor_with_rows"></a>7.3.13. Property <code class="literal">MySQLCursor.with_rows</code></h3></div></div></div><p>
  1928. This read-only property will return True when the result of the
  1929. executed operation provides rows.
  1930. </p><p>
  1931. The <code class="literal">with_rows</code> property is useful when
  1932. executing multiple statements and you need to fetch rows. In the
  1933. following example we only report the affected rows by the
  1934. <code class="literal">UPDATE</code> statement:
  1935. </p><pre class="programlisting">import mysql.connector
  1936. cnx = mysql.connector.connect(user='scott', database='test')
  1937. cursor = cnx.cursor()
  1938. operation = 'SELECT 1; UPDATE t1 SET c1 = 2; SELECT 2'
  1939. for result in cursor.execute(operation, multi=True):
  1940. if result.with_rows:
  1941. result.fetchall()
  1942. else:
  1943. print("Updated row(s): {}".format(result.rowcount))</pre></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-MySQLCursorBuffered"></a>7.4. Class <code class="literal">cursor.MySQLCursorBuffered</code></h2></div></div></div><p>
  1944. This class is inheriting from cursor.MySQLCursor and if needed
  1945. automatically retrieves rows after an operation has been executed.
  1946. </p><p>
  1947. MySQLCursorBuffered can be useful in situations where two queries,
  1948. with small result sets, need to be combined or computed with each
  1949. other.
  1950. </p><p>
  1951. You can either use the <code class="literal">buffered</code> argument when
  1952. using the connection's
  1953. <a class="link" href="#myconnpy_MySQLConnection_cursor" title="7.2.6. Method MySQLConnection.cursor(buffered=None, raw=None, cursor_class=None)">
  1954. <code class="literal">cursor()</code></a> method, or you can use the
  1955. <code class="literal">buffered</code>
  1956. <a class="link" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">connection
  1957. option</a> to make all created cursors by default buffering.
  1958. </p><pre class="programlisting">import mysql.connector
  1959. cnx = mysql.connector.connect()
  1960. # Only this particular cursor will be buffering results
  1961. cursor.cursor(buffered=True)
  1962. # All cursors by default buffering
  1963. cnx = mysql.connector.connect(buffered=True)</pre><p>
  1964. See <a class="xref" href="#myconnpy_tutorial_CursorBuffered_GiveRaise" title="5.1. Tutorial: Raise employee's salary using a buffering cursor">Section 5.1, Tutorial: Raise employee's salary using a buffering cursor”</a>
  1965. for a practical use case.
  1966. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-ClientFlag"></a>7.5. Class <code class="literal">constants.ClientFlag</code></h2></div></div></div><p>
  1967. This class provides constants defining MySQL client flags which
  1968. can be used upon connection to configure the session. The
  1969. ClientFlag class is available when importing mysql.connector.
  1970. </p><pre class="programlisting">
  1971. &gt;&gt;&gt; import mysql.connector
  1972. &gt;&gt;&gt; mysql.connector.ClientFlag.FOUND_ROWS
  1973. 2
  1974. </pre><p>
  1975. See <a class="xref" href="#myconnpy_MySQLConnection_set_client_flags" title="7.2.30. Method MySQLConnection.set_client_flags(flags)">Section 7.2.30, Method <code class="literal">MySQLConnection.set_client_flags(flags)</code></a>
  1976. and the <a class="link" href="#connector-python-connectargs" title="Chapter 6. Connector/Python Connection Arguments">connection
  1977. argument</a> <code class="literal">client_flag</code>.
  1978. </p><p>
  1979. Note that the ClientFlag class can not be instantiated.
  1980. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-FieldType"></a>7.6. Class <code class="literal">constants.FieldType</code></h2></div></div></div><p>
  1981. This class provides all supported MySQL field or data types. They
  1982. can be useful when dealing with raw data or defining your own
  1983. converters. The field type is stored with every cursor in the
  1984. description for each column.
  1985. </p><p>
  1986. The following example shows how you can print the name of the data
  1987. types for each of the columns in the result set.
  1988. </p><pre class="programlisting">from __future__ import print_function
  1989. import mysql.connector
  1990. from mysql.connector import FieldType
  1991. cnx = mysql.connector.connect(user='scott', database='test')
  1992. cursor = cnx.cursor()
  1993. cursor.execute(
  1994. "SELECT DATE(NOW()) AS `c1`, TIME(NOW()) AS `c2`, "
  1995. "NOW() AS `c3`, 'a string' AS `c4`, 42 AS `c5`")
  1996. rows = cursor.fetchall()
  1997. for desc in cursor.description:
  1998. colname = desc[0]
  1999. coltype = desc[1]
  2000. print("Column {} has type {}".format(
  2001. colname, FieldType.get_info(coltype)))
  2002. cursor.close()
  2003. cnx.close()</pre><p>
  2004. Note that the FieldType class can not be instantiated.
  2005. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-SQLMode"></a>7.7. Class <code class="literal">constants.SQLMode</code></h2></div></div></div><p>
  2006. This class provides all known MySQL
  2007. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/server-sql-mode.html" target="_top">Server SQL Modes</a>. It is
  2008. mostly used when setting the SQL modes at connection time using
  2009. the connection's property <code class="literal">sql_mode</code>. See
  2010. <a class="xref" href="#myconnpy_MySQLConnection_sql_mode" title="7.2.40. Property MySQLConnection.sql_mode">Section 7.2.40, Property <code class="literal">MySQLConnection.sql_mode</code></a>.
  2011. </p><p>
  2012. Note that the SQLMode class can not be instantiated.
  2013. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-CharacterSet"></a>7.8. Class <code class="literal">constants.CharacterSet</code></h2></div></div></div><p>
  2014. This class provides all known MySQL characters sets and their
  2015. default collations. See
  2016. <a class="xref" href="#myconnpy_MySQLConnection_set_charset_collation" title="7.2.29. Method MySQLConnection.set_charset_collation(charset=None, collation=None)">Section 7.2.29, Method <code class="literal">MySQLConnection.set_charset_collation(charset=None,
  2017. collation=None)</code></a>
  2018. for examples.
  2019. </p><p>
  2020. Note that the CharacterSet class can not be instantiated.
  2021. </p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="connector-python-apiref-RefreshOption"></a>7.9. Class <code class="literal">constants.RefreshOption</code></h2></div></div></div><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2022. <code class="literal">RefreshOption.GRANT</code>
  2023. </p><p>
  2024. Refresh the grant tables, like
  2025. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/flush.html" target="_top"><code class="literal">FLUSH
  2026. PRIVILEGES</code></a>.
  2027. </p></li><li class="listitem"><p>
  2028. <code class="literal">RefreshOption.LOG</code>
  2029. </p><p>
  2030. Flush the logs, like
  2031. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/flush.html" target="_top"><code class="literal">FLUSH LOGS</code></a>.
  2032. </p></li><li class="listitem"><p>
  2033. <code class="literal">RefreshOption.TABLES</code>
  2034. </p><p>
  2035. Flush the table cache, like
  2036. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/flush.html" target="_top"><code class="literal">FLUSH TABLES</code></a>.
  2037. </p></li><li class="listitem"><p>
  2038. <code class="literal">RefreshOption.HOSTS</code>
  2039. </p><p>
  2040. Flush the host cache, like
  2041. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/flush.html" target="_top"><code class="literal">FLUSH HOSTS</code></a>.
  2042. </p></li><li class="listitem"><p>
  2043. <code class="literal">RefreshOption.STATUS</code>
  2044. </p><p>
  2045. Reset status variables, like <code class="literal">FLUSH STATUS</code>.
  2046. </p></li><li class="listitem"><p>
  2047. <code class="literal">RefreshOption.THREADS</code>
  2048. </p><p>
  2049. Flush the thread cache.
  2050. </p></li><li class="listitem"><p>
  2051. <code class="literal">RefreshOption.SLAVE</code>
  2052. </p><p>
  2053. On a slave replication server, reset the master server
  2054. information and restart the slave, like
  2055. <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/reset-slave.html" target="_top"><code class="literal">RESET SLAVE</code></a>.
  2056. </p></li><li class="listitem"><p>
  2057. <code class="literal">RefreshOption.MASTER</code>
  2058. </p><p>
  2059. On a master replication server, remove the binary log files
  2060. listed in the binary log index and truncate the index file,
  2061. like <a class="ulink" href="http://dev.mysql.com/doc/refman/5.5/en/reset-master.html" target="_top"><code class="literal">RESET MASTER</code></a>.
  2062. </p></li></ul></div></div></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="cpython-news"></a>Chapter 8. MySQL Connector/Python Change History</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="section"><a href="#cpython-news-1-0-8">8.1. Changes in MySQL Connector/Python 1.0.8 (Not yet released, Generally Available)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-7">8.2. Changes in MySQL Connector/Python 1.0.7 (29 September 2012, Generally Available)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-6">8.3. Changes in MySQL Connector/Python 1.0.6 (30 August 2012, beta)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-5">8.4. Changes in MySQL Connector/Python 1.0.5 (17 July 2012, beta)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-4">8.5. Changes in MySQL Connector/Python 1.0.4 (07 July 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-3">8.6. Changes in MySQL Connector/Python 1.0.3 (08 June 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-2">8.7. Changes in MySQL Connector/Python 1.0.2 (19 May 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-1">8.8. Changes in MySQL Connector/Python 1.0.1 (26 April 2012, alpha)</a></span></dt><dt><span class="section"><a href="#cpython-news-1-0-0">8.9. Changes in MySQL Connector/Python 1.0.0 (22 April 2012, alpha)</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-8"></a>8.1. Changes in MySQL Connector/Python 1.0.8 (Not yet released, Generally Available)</h2></div></div></div><p class="release-level">
  2063. Fixes bugs since the initial 1.0.7 GA release.
  2064. </p><p>Version 1.0.8 has no changelog entries.</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-7"></a>8.2. Changes in MySQL Connector/Python 1.0.7 (29 September 2012, Generally Available)</h2></div></div></div><p class="release-level">
  2065. GA release. Connector/Python is now production-ready.
  2066. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2067. Client and server errors have been regenerated using the MySQL
  2068. 5.6.6 development release.
  2069. </p></li></ul></div><p><span class="bold"><strong>Bugs Fixed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2070. Fixed formatting of client errors changing numeric to string
  2071. placeholders.
  2072. (Bug #14548043)</p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-6"></a>8.3. Changes in MySQL Connector/Python 1.0.6 (30 August 2012, beta)</h2></div></div></div><p class="release-level">
  2073. Second beta release.
  2074. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2075. Changed name and version of distributions to align with other
  2076. MySQL projects:
  2077. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  2078. The version now includes the suffix 'b' for beta and 'a' for
  2079. alpha followed by a number. This version is used in the
  2080. source and built distributions. GA versions will have no
  2081. suffix.
  2082. </p></li><li class="listitem"><p>
  2083. The RPM spec files have been updated to create packages
  2084. whose names are aligned with RPMs from other MySQL projects.
  2085. </p></li></ul></div></li><li class="listitem"><p>
  2086. Changed how MySQL server errors are mapped to Python exceptions.
  2087. We now use the <code class="literal">SQLState</code> (when available) to
  2088. raise a better error.
  2089. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  2090. Incompatibility: some server errors are now raised with a
  2091. different exception.
  2092. </p></li><li class="listitem"><p>
  2093. It is possible to override how errors are raised using the
  2094. <code class="literal">mysql.connector.custom_error_exception()</code>
  2095. function, defined in the
  2096. <code class="literal">mysql.connector.errors</code> module. This can
  2097. be useful for certain frameworks to align with other
  2098. database drivers.
  2099. </p></li></ul></div></li></ul></div><p><span class="bold"><strong>Bugs Fixed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2100. Fixed version-specific code so Connector/Python works with
  2101. Python 3.3.
  2102. (Bug #14524942)</p></li><li class="listitem"><p>
  2103. Fixed <code class="literal">MySQLCursorRaw.fetchall()</code> so it does
  2104. not raise an exception when results are available.
  2105. (Bug #14517262, Bug #66465)</p></li><li class="listitem"><p>
  2106. Fixed installation of <code class="filename">version.py</code> on OS X:
  2107. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  2108. <code class="filename">version.py</code> is now correctly installed
  2109. on OS X in the <code class="literal">mysql.connector</code> package.
  2110. Previously, it was installed through
  2111. <code class="literal">data_files</code>, and
  2112. <code class="filename">version.py</code> ended up in the system-wide
  2113. package location of Python, from which it could not be
  2114. imported.
  2115. </p></li><li class="listitem"><p>
  2116. <code class="literal">data_files</code> is not used any longer in
  2117. <code class="filename">setup.py</code> and is removed. Extra files
  2118. like <code class="filename">version.py</code> are now copied in the
  2119. custom <code class="literal">Distutils</code> commands.
  2120. </p></li></ul></div><p>(Bug #14483142)</p></li><li class="listitem"><p>
  2121. Timeout for unit tests has been set to 10 seconds. Test cases
  2122. can individually adjust it to be higher or lower.
  2123. (Bug #14487502)</p></li><li class="listitem"><p>
  2124. Fixed test cases in <code class="filename">test_mysql_database.py</code>
  2125. that failed when using <code class="literal">YEAR(2)</code> with MySQL
  2126. 5.6.6 and greater.
  2127. (Bug #14460680)</p></li><li class="listitem"><p>
  2128. Fixed SSL unit testing for source distributions:
  2129. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; "><li class="listitem"><p>
  2130. The SSL keys and certificates were missing and are now added
  2131. to the source distribution. Now SSL testing works properly.
  2132. </p></li><li class="listitem"><p>
  2133. Additionally for the Windows platform, forward slashes were
  2134. added to the option file creation so the MySQL server can
  2135. pick up the needed SSL files.
  2136. </p></li></ul></div><p>(Bug #14402737)</p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-5"></a>8.4. Changes in MySQL Connector/Python 1.0.5 (17 July 2012, beta)</h2></div></div></div><p>
  2137. First beta release.
  2138. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2139. Added SQLMode class in the constants module to make it easier to
  2140. set modes. For example:
  2141. </p><pre class="programlisting">cnx.sql_mode = [SQLMode.REAL_AS_FLOAT, SQLMode.NO_ZERO_DATE]</pre></li><li class="listitem"><p>
  2142. Added descriptive error codes for both client and server errors
  2143. in the module <code class="literal">errorcode</code>. A new sub-package
  2144. <code class="literal">locales</code> has been added, which currently only
  2145. supports English client error messages.
  2146. </p><p>
  2147. For example, <code class="literal">errorcode.CR_CONNECTION_ERROR</code> is
  2148. 2002.
  2149. </p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-4"></a>8.5. Changes in MySQL Connector/Python 1.0.4 (07 July 2012, alpha)</h2></div></div></div><p>
  2150. Internal alpha release.
  2151. </p><p><span class="bold"><strong>Bugs Fixed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><span class="bold"><strong>Incompatible Change</strong></span>:
  2152. The <code class="literal">MySQLConnection</code> methods
  2153. <code class="literal">unset_client_flag()</code> and
  2154. <code class="literal">set_client_flag()</code> have been removed. Use
  2155. the<code class="literal">set_client_flags()</code> method instead using a
  2156. sequence.
  2157. (Bug #14259996)</p></li><li class="listitem"><p><span class="bold"><strong>Incompatible Change</strong></span>:
  2158. The method <code class="literal">MySQLConnection.set_charset()</code> has
  2159. been removed and replaced by
  2160. <code class="literal">MySQLConnection.set_charset_collation()</code> to
  2161. simplify setting and retrieving character set and collation
  2162. information. The <code class="literal">MySQLConnection</code> properties
  2163. <code class="literal">collation</code> and <code class="literal">charset</code> are
  2164. now read-only.
  2165. (Bug #14260052)</p></li><li class="listitem"><p><span class="bold"><strong>Incompatible Change</strong></span>:
  2166. Fixed <code class="literal">MySQLConnection.cmd_query()</code> to raise an
  2167. error when the operation has multiple statements. We introduced
  2168. a new method <code class="literal">MySQLConnection.cmd_query_iter()</code>
  2169. which needs to be used when multiple statements send to the
  2170. MySQL server. It returns a generator object to iterate through
  2171. results.
  2172. </p><p>
  2173. When executing single statements,
  2174. <code class="literal">MySQLCursor.execute()</code> will always return
  2175. <code class="literal">None</code>. You can use the
  2176. <code class="literal">MySQLCursor</code> property
  2177. <code class="literal">with_rows</code> to check whether a result could
  2178. have rows or not.
  2179. </p><p>
  2180. <code class="literal">MySQLCursor.execute()</code> returns a generator
  2181. object with which you can iterate over results when executing
  2182. multiple statements.
  2183. </p><p>
  2184. The <code class="literal">MySQLCursor.next_resultset()</code> became
  2185. obsolete and was removed and the
  2186. <code class="literal">MySQLCursor.next_proc_result()</code> method has
  2187. been renamed to <code class="literal">MySQLCursor.proc_results()</code>,
  2188. which returns a generator object. The
  2189. <code class="literal">MySQLCursor.with_rows</code> property can be used to
  2190. check if a result could return rows. The
  2191. <code class="filename">multiple_resultset.py</code> example script shows
  2192. how to go through results produced by sending multiple
  2193. statements.
  2194. (Bug #14208326)</p></li><li class="listitem"><p>
  2195. Fixed <code class="literal">MySQLCursor.executemany()</code> when
  2196. <code class="literal">INSERT</code> statements use the <code class="literal">ON
  2197. DUPLICATE KEY</code> clause with a function such as
  2198. <code class="literal">VALUES()</code>.
  2199. (Bug #14259954)</p></li><li class="listitem"><p>
  2200. Fixed unit testing on the Microsoft Windows platform.
  2201. (Bug #14236592)</p></li><li class="listitem"><p>
  2202. Fixed converting a <code class="literal">datetime.time</code> to a MySQL
  2203. type using Python 2.4 and 2.5. The <code class="literal">strftime()</code>
  2204. function has no support for the <code class="literal">%f</code> mark in
  2205. those Python versions.
  2206. (Bug #14231941)</p></li><li class="listitem"><p>
  2207. Fixed <code class="literal">cursor.CursorBase</code> attributes
  2208. <code class="literal">description</code>, <code class="literal">lastrowid</code> and
  2209. <code class="literal">rowcount</code> to be read-only properties.
  2210. (Bug #14231160)</p></li><li class="listitem"><p>
  2211. Fixed <code class="literal">MySQLConnection.cmd_query()</code> and other
  2212. methods so they check first whether there are unread results.
  2213. (Bug #14184643)</p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-3"></a>8.6. Changes in MySQL Connector/Python 1.0.3 (08 June 2012, alpha)</h2></div></div></div><p>
  2214. Internal alpha release.
  2215. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2216. Adding new <code class="literal">Distutils</code> commands to create
  2217. Windows Installers using WiX and RPM packages.
  2218. </p></li><li class="listitem"><p>
  2219. Adding support for time values with a fractional part, for MySQL
  2220. 5.6.4 and greater. A new example script
  2221. <code class="filename">microseconds.py</code> was added to show this
  2222. functionality.
  2223. </p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-2"></a>8.7. Changes in MySQL Connector/Python 1.0.2 (19 May 2012, alpha)</h2></div></div></div><p>
  2224. Internal alpha release.
  2225. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2226. Added more unit tests for modules like
  2227. <code class="literal">connection</code> and <code class="literal">network</code> as
  2228. well as testing the SSL functionality.
  2229. </p></li></ul></div><p><span class="bold"><strong>Bugs Fixed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2230. Fixing and refactoring the
  2231. <code class="literal">mysql.connector.errors</code> module.
  2232. (Bug #14039339)</p></li><li class="listitem"><p>
  2233. Fixed bootstrapping MySQL 5.6 running unit tests.
  2234. </p><p>
  2235. Messages send by the bootstrapped MySQL server to
  2236. <code class="literal">stdout</code> and <code class="literal">stderr</code> are now
  2237. discarded.
  2238. (Bug #14048685)</p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-1"></a>8.8. Changes in MySQL Connector/Python 1.0.1 (26 April 2012, alpha)</h2></div></div></div><p>
  2239. Internal alpha release.
  2240. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2241. Change the version so it only contain integers. The 'a' or
  2242. 'alpha' suffix will not be present in packages, but it will be
  2243. mentioned in the <code class="filename">_version.py</code> module since
  2244. <code class="filename">metasetupinfo.py</code> uses this information to
  2245. set, for example, the Trove classifiers dynamically.
  2246. </p></li></ul></div></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="cpython-news-1-0-0"></a>8.9. Changes in MySQL Connector/Python 1.0.0 (22 April 2012, alpha)</h2></div></div></div><p>
  2247. Internal alpha release.
  2248. </p><p><span class="bold"><strong>Functionality Added or Changed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p><span class="bold"><strong>Incompatible Change</strong></span>:
  2249. <code class="literal">MySQLConnection.reconnect()</code> can be used to
  2250. reconnect to the MySQL server. It accepts number of retries and
  2251. an optional delay between attempts.
  2252. </p><p>
  2253. <code class="literal">MySQLConnectiong.ping()</code> is now a method and
  2254. works the way the MySQL C API <code class="literal">mysql_ping()</code>
  2255. function works: it raises an error. It can also optionally
  2256. reconnect.
  2257. </p><p>
  2258. <code class="literal">MySQLConnection.is_connected()</code> now returns
  2259. <code class="literal">True</code> when connection is available,
  2260. <code class="literal">False</code> otherwise.
  2261. </p><p>
  2262. <code class="literal">ping()</code> and <code class="literal">is_connected()</code>
  2263. are backwards incompatible.
  2264. (Bug #13392739)</p></li><li class="listitem"><p>
  2265. Refactored the modules connection and protocol and created a new
  2266. module <code class="literal">network</code>. The
  2267. <code class="literal">MySQLProtocol</code> does not keep a reference to
  2268. the connection object any more and deals only with creating and
  2269. parsing MySQL packets. Network interaction is now done by the
  2270. <code class="literal">MySQLConnection</code> objects (with the exception
  2271. of <code class="literal">MySQLProtocol.read_text_result()</code>).
  2272. </p></li></ul></div><p><span class="bold"><strong>Bugs Fixed</strong></span></p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  2273. Fixed <code class="filename">metasetupinfo.py</code> to use the
  2274. Connector/Python which is being installed instead of the version
  2275. already installed.
  2276. (Bug #13962765)</p></li><li class="listitem"><p>
  2277. Fixed <code class="literal">MySQLCursor.description</code> so it stores
  2278. column names as Unicode.
  2279. (Bug #13792575)</p></li><li class="listitem"><p>
  2280. Fixed <code class="literal">dbapi.Binary</code> to be a bytes types for
  2281. Python 3.x.
  2282. (Bug #13780676)</p></li><li class="listitem"><p>
  2283. Fixed automatic garbage collection which caused memory usage to
  2284. grow over time. Note that <code class="literal">MySQLConnection</code>
  2285. does not keep track of its cursors any longer.
  2286. (Bug #13435186)</p></li><li class="listitem"><p>
  2287. Fixed setting time zone for current MySQL session.
  2288. (Bug #13395083)</p></li><li class="listitem"><p>
  2289. Fixed setting and retrieving character set and collation.
  2290. (Bug #13375632)</p></li><li class="listitem"><p>
  2291. Fixed handling of errors after authentication for Python 3.
  2292. (Bug #13364285)</p></li></ul></div></div></div></div><div class="copyright-footer">
  2293. Copyright © 2012, Oracle and/or its affiliates. All rights
  2294. reserved.
  2295. <a href="#legalnotice">Legal Notices</a></div></body></html>