PageRenderTime 75ms CodeModel.GetById 23ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file