annotate doc/MySQLdb.txt @ 56:89b07ce2a788 MySQLdb

python 2.4 is our minimum version now
author kylev
date Fri, 27 Feb 2009 19:46:55 +0000
parents e48810735f11
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
1 ====================
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
2 MySQLdb User's Guide
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
3 ====================
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5 .. contents::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6 ..
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
7
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
8 Introduction
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9 ------------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
10
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
11 MySQLdb is an thread-compatible interface to the popular MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
12 database server that provides the Python database API.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
13
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
14 Installation
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15 ------------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
17 The ``README`` file has complete installation instructions.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
18
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
19
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
20 _mysql
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
21 ------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23 If you want to write applications which are portable across databases,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24 use MySQLdb_, and avoid using this module directly. ``_mysql``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
25 provides an interface which mostly implements the MySQL C API. For
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26 more information, see the `MySQL documentation`_. The documentation
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 for this module is intentionally weak because you probably should use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28 the higher-level MySQLdb module. If you really need it, use the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 standard MySQL docs and transliterate as necessary.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 .. _`MySQL documentation`: http://dev.mysql.com/doc/
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34 MySQL C API translation
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
35 .......................
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
36
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
37 The MySQL C API has been wrapped in an object-oriented way. The only
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38 MySQL data structures which are implemented are the ``MYSQL``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39 (database connection handle) and ``MYSQL_RES`` (result handle)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
40 types. In general, any function which takes ``MYSQL *mysql`` as an
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
41 argument is now a method of the connection object, and any function
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
42 which takes ``MYSQL_RES *result`` as an argument is a method of the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 result object. Functions requiring none of the MySQL data structures
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
44 are implemented as functions in the module. Functions requiring one of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
45 the other MySQL data structures are generally not implemented.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46 Deprecated functions are not implemented. In all cases, the ``mysql_``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
47 prefix is dropped from the name. Most of the ``conn`` methods listed
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
48 are also available as MySQLdb Connection object methods. Their use is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49 non-portable.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 MySQL C API function mapping
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 ............................
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54 =================================== ==================================
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 C API ``_mysql``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56 =================================== ==================================
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
57 ``mysql_affected_rows()`` ``conn.affected_rows()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 ``mysql_autocommit()`` ``conn.autocommit()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59 ``mysql_character_set_name()`` ``conn.character_set_name()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 ``mysql_close()`` ``conn.close()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61 ``mysql_commit()`` ``conn.commit()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62 ``mysql_connect()`` ``_mysql.connect()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
63 ``mysql_data_seek()`` ``result.data_seek()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64 ``mysql_debug()`` ``_mysql.debug()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65 ``mysql_dump_debug_info`` ``conn.dump_debug_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 ``mysql_escape_string()`` ``_mysql.escape_string()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 ``mysql_fetch_row()`` ``result.fetch_row()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68 ``mysql_get_character_set_info()`` ``conn.get_character_set_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
69 ``mysql_get_client_info()`` ``_mysql.get_client_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70 ``mysql_get_host_info()`` ``conn.get_host_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
71 ``mysql_get_proto_info()`` ``conn.get_proto_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
72 ``mysql_get_server_info()`` ``conn.get_server_info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
73 ``mysql_info()`` ``conn.info()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
74 ``mysql_insert_id()`` ``conn.insert_id()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
75 ``mysql_num_fields()`` ``result.num_fields()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
76 ``mysql_num_rows()`` ``result.num_rows()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77 ``mysql_options()`` various options to ``_mysql.connect()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78 ``mysql_ping()`` ``conn.ping()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 ``mysql_query()`` ``conn.query()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80 ``mysql_real_connect()`` ``_mysql.connect()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 ``mysql_real_query()`` ``conn.query()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82 ``mysql_real_escape_string()`` ``conn.escape_string()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 ``mysql_rollback()`` ``conn.rollback()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
84 ``mysql_row_seek()`` ``result.row_seek()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
85 ``mysql_row_tell()`` ``result.row_tell()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86 ``mysql_select_db()`` ``conn.select_db()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87 ``mysql_set_character_set()`` ``conn.set_character_set()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 ``mysql_ssl_set()`` ``ssl`` option to ``_mysql.connect()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 ``mysql_stat()`` ``conn.stat()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90 ``mysql_store_result()`` ``conn.store_result()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 ``mysql_thread_id()`` ``conn.thread_id()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 ``mysql_thread_safe_client()`` ``conn.thread_safe_client()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93 ``mysql_use_result()`` ``conn.use_result()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
94 ``mysql_warning_count()`` ``conn.warning_count()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 ``CLIENT_*`` ``MySQLdb.constants.CLIENT.*``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 ``CR_*`` ``MySQLdb.constants.CR.*``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 ``ER_*`` ``MySQLdb.constants.ER.*``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 ``FIELD_TYPE_*`` ``MySQLdb.constants.FIELD_TYPE.*``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 ``FLAG_*`` ``MySQLdb.constants.FLAG.*``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
100 =================================== ==================================
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
101
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 Some _mysql examples
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104 ....................
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
105
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106 Okay, so you want to use ``_mysql`` anyway. Here are some examples.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 The simplest possible database connection is::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
110 import _mysql
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
111 db=_mysql.connect()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
112
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
113 This creates a connection to the MySQL server running on the local
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114 machine using the standard UNIX socket (or named pipe on Windows),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115 your login name (from the USER environment variable), no password, and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 does not ``USE`` a database. Chances are you need to supply more
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
117 information.::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
118
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
119 db=_mysql.connect("localhost","joebob","moonpie","thangs")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
120
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
121 This creates a connection to the MySQL server running on the local
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
122 machine via a UNIX socket (or named pipe), the user name "joebob", the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
123 password "moonpie", and selects the initial database "thangs".
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
124
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125 We haven't even begun to touch upon all the parameters ``connect()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126 can take. For this reason, I prefer to use keyword parameters::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
127
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
128 db=_mysql.connect(host="localhost",user="joebob",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
129 passwd="moonpie",db="thangs")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
130
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
131 This does exactly what the last example did, but is arguably easier to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
132 read. But since the default host is "localhost", and if your login
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
133 name really was "joebob", you could shorten it to this::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
134
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
135 db=_mysql.connect(passwd="moonpie",db="thangs")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
137 UNIX sockets and named pipes don't work over a network, so if you
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
138 specify a host other than localhost, TCP will be used, and you can
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
139 specify an odd port if you need to (the default port is 3306)::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
140
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
141 db=_mysql.connect(host="outhouse",port=3307,passwd="moonpie",db="thangs")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
142
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
143 If you really had to, you could connect to the local host with TCP by
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
144 specifying the full host name, or 127.0.0.1.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
145
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 Generally speaking, putting passwords in your code is not such a good
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 idea::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
148
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
149 db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my.cnf")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
151 This does what the previous example does, but gets the username and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
152 password and other parameters from ~/.my.cnf (UNIX-like systems). Read
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
153 about `option files`_ for more details.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
154
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155 .. _`option files`: http://dev.mysql.com/doc/mysql/en/Option_files.html
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
156
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
157 So now you have an open connection as ``db`` and want to do a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
158 query. Well, there are no cursors in MySQL, and no parameter
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
159 substitution, so you have to pass a complete query string to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
160 ``db.query()``::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
161
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
162 db.query("""SELECT spam, eggs, sausage FROM breakfast
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
163 WHERE price < 5""")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
165 There's no return value from this, but exceptions can be raised. The
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
166 exceptions are defined in a separate module, ``_mysql_exceptions``,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
167 but ``_mysql`` exports them. Read DB API specification PEP-249_ to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
168 find out what they are, or you can use the catch-all ``MySQLError``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
169
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
170 .. _PEP-249: http://www.python.org/peps/pep-0249.html
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
171
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
172 At this point your query has been executed and you need to get the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
173 results. You have two options::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
174
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
175 r=db.store_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
176 # ...or...
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
177 r=db.use_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
178
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
179 Both methods return a result object. What's the difference?
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
180 ``store_result()`` returns the entire result set to the client
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
181 immediately. If your result set is really large, this could be a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
182 problem. One way around this is to add a ``LIMIT`` clause to your
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
183 query, to limit the number of rows returned. The other is to use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
184 ``use_result()``, which keeps the result set in the server and sends
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
185 it row-by-row when you fetch. This does, however, tie up server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
186 resources, and it ties up the connection: You cannot do any more
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
187 queries until you have fetched **all** the rows. Generally I
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
188 recommend using ``store_result()`` unless your result set is really
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
189 huge and you can't use ``LIMIT`` for some reason.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
190
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
191 Now, for actually getting real results::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
192
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
193 >>> r.fetch_row()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
194 (('3','2','0'),)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
195
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
196 This might look a little odd. The first thing you should know is,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
197 ``fetch_row()`` takes some additional parameters. The first one is,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
198 how many rows (``maxrows``) should be returned. By default, it returns
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
199 one row. It may return fewer rows than you asked for, but never
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
200 more. If you set ``maxrows=0``, it returns all rows of the result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
201 set. If you ever get an empty tuple back, you ran out of rows.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
202
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
203 The second parameter (``how``) tells it how the row should be
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
204 represented. By default, it is zero which means, return as a tuple.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
205 ``how=1`` means, return it as a dictionary, where the keys are the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
206 column names, or ``table.column`` if there are two columns with the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
207 same name (say, from a join). ``how=2`` means the same as ``how=1``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
208 except that the keys are *always* ``table.column``; this is for
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
209 compatibility with the old ``Mysqldb`` module.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
210
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
211 OK, so why did we get a 1-tuple with a tuple inside? Because we
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
212 implicitly asked for one row, since we didn't specify ``maxrows``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
213
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
214 The other oddity is: Assuming these are numeric columns, why are they
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
215 returned as strings? Because MySQL returns all data as strings and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
216 expects you to convert it yourself. This would be a real pain in the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
217 ass, but in fact, ``_mysql`` can do this for you. (And ``MySQLdb``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
218 does do this for you.) To have automatic type conversion done, you
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
219 need to create a type converter dictionary, and pass this to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
220 ``connect()`` as the ``conv`` keyword parameter.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
221
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
222 The keys of ``conv`` should be MySQL column types, which in the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
223 C API are ``FIELD_TYPE_*``. You can get these values like this::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
224
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
225 from MySQLdb.constants import FIELD_TYPE
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
226
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
227 By default, any column type that can't be found in ``conv`` is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
228 returned as a string, which works for a lot of stuff. For our
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
229 purposes, we probably want this::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
230
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
231 my_conv = { FIELD_TYPE.LONG: int }
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
232
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
233 This means, if it's a ``FIELD_TYPE_LONG``, call the builtin ``int()``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
234 function on it. Note that ``FIELD_TYPE_LONG`` is an ``INTEGER``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
235 column, which corresponds to a C ``long``, which is also the type used
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
236 for a normal Python integer. But beware: If it's really an ``UNSIGNED
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
237 INTEGER`` column, this could cause overflows. For this reason,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
238 ``MySQLdb`` actually uses ``long()`` to do the conversion. But we'll
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
239 ignore this potential problem for now.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
240
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
241 Then if you use ``db=_mysql.connect(conv=my_conv...)``, the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
242 results will come back ``((3, 2, 0),)``, which is what you would
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
243 expect.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
244
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
245 MySQLdb
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
246 -------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
247
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
248 MySQLdb is a thin Python wrapper around ``_mysql`` which makes it
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
249 compatible with the Python DB API interface (version 2). In reality,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
250 a fair amount of the code which implements the API is in ``_mysql``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
251 for the sake of efficiency.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
252
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
253 The DB API specification PEP-249_ should be your primary guide for
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
254 using this module. Only deviations from the spec and other
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
255 database-dependent things will be documented here.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
256
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
257 Functions and attributes
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
258 ........................
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
259
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
260 Only a few top-level functions and attributes are defined within
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
261 MySQLdb.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
262
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
263 connect(parameters...)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
264 Constructor for creating a connection to the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
265 database. Returns a Connection Object. Parameters are the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
266 same as for the MySQL C API. In addition, there are a few
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
267 additional keywords that correspond to what you would pass
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
268 ``mysql_options()`` before connecting. Note that some
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
269 parameters must be specified as keyword arguments! The
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
270 default value for each parameter is NULL or zero, as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
271 appropriate. Consult the MySQL documentation for more
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
272 details. The important parameters are:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
273
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
274 host
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
275 name of host to connect to. Default: use the local host
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
276 via a UNIX socket (where applicable)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
277
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
278 user
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
279 user to authenticate as. Default: current effective user.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
280
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
281 passwd
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
282 password to authenticate with. Default: no password.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
283
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
284 db
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
285 database to use. Default: no default database.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
286
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
287 port
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
288 TCP port of MySQL server. Default: standard port (3306).
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
289
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
290 unix_socket
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
291 location of UNIX socket. Default: use default location or
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
292 TCP for remote hosts.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
293
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
294 conv
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
295 type conversion dictionary. Default: a copy of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
296 ``MySQLdb.converters.conversions``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
297
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
298 compress
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
299 Enable protocol compression. Default: no compression.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
300
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
301 connect_timeout
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
302 Abort if connect is not completed within
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
303 given number of seconds. Default: no timeout (?)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
304
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
305 named_pipe
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
306 Use a named pipe (Windows). Default: don't.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
307
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
308 init_command
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
309 Initial command to issue to server upon
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
310 connection. Default: Nothing.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
311
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
312 read_default_file
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
313 MySQL configuration file to read; see
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
314 the MySQL documentation for ``mysql_options()``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
315
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
316 read_default_group
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
317 Default group to read; see the MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
318 documentation for ``mysql_options()``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
319
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
320 cursorclass
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
321 cursor class that ``cursor()`` uses, unless
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
322 overridden. Default: ``MySQLdb.cursors.Cursor``. *This
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
323 must be a keyword parameter.*
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
324
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
325 use_unicode
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
326 If True, CHAR and VARCHAR and TEXT columns are returned as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
327 Unicode strings, using the configured character set. It is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
328 best to set the default encoding in the server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
329 configuration, or client configuration (read with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
330 read_default_file). If you change the character set after
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
331 connecting (MySQL-4.1 and later), you'll need to put the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
332 correct character set name in connection.charset.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
333
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
334 If False, text-like columns are returned as normal strings,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
335 but you can always write Unicode strings.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
336
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
337 *This must be a keyword parameter.*
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
338
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
339 charset
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
340 If present, the connection character set will be changed
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
341 to this character set, if they are not equal. Support for
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
342 changing the character set requires MySQL-4.1 and later
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
343 server; if the server is too old, UnsupportedError will be
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
344 raised. This option implies use_unicode=True, but you can
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
345 override this with use_unicode=False, though you probably
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
346 shouldn't.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
347
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
348 If not present, the default character set is used.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
349
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
350 *This must be a keyword parameter.*
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
351
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
352 sql_mode
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
353 If present, the session SQL mode will be set to the given
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
354 string. For more information on sql_mode, see the MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
355 documentation. Only available for 4.1 and newer servers.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
356
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
357 If not present, the session SQL mode will be unchanged.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
358
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
359 *This must be a keyword parameter.*
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
360
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
361 ssl
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
362 This parameter takes a dictionary or mapping, where the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
363 keys are parameter names used by the mysql_ssl_set_ MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
364 C API call. If this is set, it initiates an SSL connection
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
365 to the server; if there is no SSL support in the client,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
366 an exception is raised. *This must be a keyword
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
367 parameter.*
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
368
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
369 .. _mysql_ssl_set: http://dev.mysql.com/doc/mysql/en/mysql_ssl_set.html
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
370
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
371
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
372 apilevel
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
373 String constant stating the supported DB API level. '2.0'
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
374
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
375 threadsafety
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
376 Integer constant stating the level of thread safety the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
377 interface supports. This is set to 1, which means: Threads may
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
378 share the module.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
379
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
380 The MySQL protocol can not handle multiple threads using the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
381 same connection at once. Some earlier versions of MySQLdb
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
382 utilized locking to achieve a threadsafety of 2. While this is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
383 not terribly hard to accomplish using the standard Cursor class
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
384 (which uses ``mysql_store_result()``), it is complicated by
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
385 SSCursor (which uses ``mysql_use_result()``; with the latter you
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
386 must ensure all the rows have been read before another query can
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
387 be executed. It is further complicated by the addition of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
388 transactions, since transactions start when a cursor execute a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
389 query, but end when ``COMMIT`` or ``ROLLBACK`` is executed by
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
390 the Connection object. Two threads simply cannot share a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
391 connection while a transaction is in progress, in addition to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
392 not being able to share it during query execution. This
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
393 excessively complicated the code to the point where it just
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
394 isn't worth it.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
395
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
396 The general upshot of this is: Don't share connections between
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
397 threads. It's really not worth your effort or mine, and in the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
398 end, will probably hurt performance, since the MySQL server runs
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
399 a separate thread for each connection. You can certainly do
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
400 things like cache connections in a pool, and give those
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
401 connections to one thread at a time. If you let two threads use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
402 a connection simultaneously, the MySQL client library will
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
403 probably upchuck and die. You have been warned.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
404
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
405 For threaded applications, try using a connection pool.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
406 This can be done using the `Pool module`_.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
407
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
408 .. _`Pool module`: http://dustman.net/andy/python/Pool
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
409
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
410 charset
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
411 The character set used by the connection. In MySQL-4.1 and newer,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
412 it is possible (but not recommended) to change the connection's
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
413 character set with an SQL statement. If you do this, you'll also
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
414 need to change this attribute. Otherwise, you'll get encoding
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
415 errors.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
416
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
417 paramstyle
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
418 String constant stating the type of parameter marker formatting
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
419 expected by the interface. Set to 'format' = ANSI C printf
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
420 format codes, e.g. '...WHERE name=%s'. If a mapping object is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
421 used for conn.execute(), then the interface actually uses
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
422 'pyformat' = Python extended format codes, e.g. '...WHERE
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
423 name=%(name)s'. However, the API does not presently allow the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
424 specification of more than one style in paramstyle.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
425
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
426 Note that any literal percent signs in the query string passed
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
427 to execute() must be escaped, i.e. %%.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
428
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
429 Parameter placeholders can **only** be used to insert column
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
430 values. They can **not** be used for other parts of SQL, such as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
431 table names, statements, etc.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
432
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
433 conv
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
434 A dictionary or mapping which controls how types are converted
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
435 from MySQL to Python and vice versa.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
436
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
437 If the key is a MySQL type (from ``FIELD_TYPE.*``), then the value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
438 can be either:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
439
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
440 * a callable object which takes a string argument (the MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
441 value),' returning a Python value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
442
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
443 * a sequence of 2-tuples, where the first value is a combination
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
444 of flags from ``MySQLdb.constants.FLAG``, and the second value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
445 is a function as above. The sequence is tested until the flags
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
446 on the field match those of the first value. If both values
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
447 are None, then the default conversion is done. Presently this
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
448 is only used to distinquish TEXT and BLOB columns.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
449
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
450 If the key is a Python type or class, then the value is a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
451 callable Python object (usually a function) taking two arguments
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
452 (value to convert, and the conversion dictionary) which converts
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
453 values of this type to a SQL literal string value.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
454
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
455 This is initialized with reasonable defaults for most
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
456 types. When creating a Connection object, you can pass your own
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
457 type converter dictionary as a keyword parameter. Otherwise, it
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
458 uses a copy of ``MySQLdb.converters.conversions``. Several
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
459 non-standard types are returned as strings, which is how MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
460 returns all columns. For more details, see the built-in module
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
461 documentation.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
462
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
463
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
464 Connection Objects
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
465 ..................
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
466
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
467 Connection objects are returned by the ``connect()`` function.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
468
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
469 commit()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
470 If the database and the tables support transactions, this
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
471 commits the current transaction; otherwise this method
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
472 successfully does nothing.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
473
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
474 rollback()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
475 If the database and tables support transactions, this rolls back
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
476 (cancels) the current transaction; otherwise a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
477 ``NotSupportedError`` is raised.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
478
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
479 cursor([cursorclass])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
480 MySQL does not support cursors; however, cursors are easily
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
481 emulated. You can supply an alternative cursor class as an
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
482 optional parameter. If this is not present, it defaults to the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
483 value given when creating the connection object, or the standard
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
484 ``Cursor`` class. Also see the additional supplied cursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
485 classes in the usage section.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
486
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
487 There are many more methods defined on the connection object which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
488 are MySQL-specific. For more information on them, consult the internal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
489 documentation using ``pydoc``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
490
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
491
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
492 Cursor Objects
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
493 ..............
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
494
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
495 callproc(procname, args)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
496 Calls stored procedure procname with the sequence of arguments
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
497 in args. Returns the original arguments. Stored procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
498 support only works with MySQL-5.0 and newer.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
499
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
500 **Compatibility note:** PEP-249_ specifies that if there are
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
501 OUT or INOUT parameters, the modified values are to be
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
502 returned. This is not consistently possible with MySQL. Stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
503 procedure arguments must be passed as server variables, and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
504 can only be returned with a SELECT statement. Since a stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
505 procedure may return zero or more result sets, it is impossible
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
506 for MySQLdb to determine if there are result sets to fetch
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
507 before the modified parmeters are accessible.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
508
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
509 The parameters are stored in the server as @_*procname*_*n*,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
510 where *n* is the position of the parameter. I.e., if you
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
511 cursor.callproc('foo', (a, b, c)), the parameters will be
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
512 accessible by a SELECT statement as @_foo_0, @_foo_1, and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
513 @_foo_2.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
514
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
515 **Compatibility note:** It appears that the mere act of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
516 executing the CALL statement produces an empty result set, which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
517 appears after any result sets which might be generated by the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
518 stored procedure. Thus, you will always need to use nextset() to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
519 advance result sets.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
520
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
521 close()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
522 Closes the cursor. Future operations raise ``ProgrammingError``.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
523 If you are using server-side cursors, it is very important to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
524 close the cursor when you are done with it and before creating a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
525 new one.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
526
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
527 info()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
528 Returns some information about the last query. Normally
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
529 you don't need to check this. If there are any MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
530 warnings, it will cause a Warning to be issued through
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
531 the Python warning module. By default, Warning causes a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
532 message to appear on the console. However, it is possible
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
533 to filter these out or cause Warning to be raised as exception.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
534 See the MySQL docs for ``mysql_info()``, and the Python warning
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
535 module. (Non-standard)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
536
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
537 setinputsizes()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
538 Does nothing, successfully.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
539
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
540 setoutputsizes()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
541 Does nothing, successfully.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
542
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
543 nextset()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
544 Advances the cursor to the next result set, discarding the remaining
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
545 rows in the current result set. If there are no additional result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
546 sets, it returns None; otherwise it returns a true value.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
547
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
548 Note that MySQL doesn't support multiple result sets until 4.1.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
549
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
550
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
551 Some examples
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
552 .............
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
553
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
554 The ``connect()`` method works nearly the same as with `_mysql`_::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
555
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
556 import MySQLdb
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
557 db=MySQLdb.connect(passwd="moonpie",db="thangs")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
558
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
559 To perform a query, you first need a cursor, and then you can execute
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
560 queries on it::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
561
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
562 c=db.cursor()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
563 max_price=5
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
564 c.execute("""SELECT spam, eggs, sausage FROM breakfast
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
565 WHERE price < %s""", (max_price,))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
566
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
567 In this example, ``max_price=5`` Why, then, use ``%s`` in the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
568 string? Because MySQLdb will convert it to a SQL literal value, which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
569 is the string '5'. When it's finished, the query will actually say,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
570 "...WHERE price < 5".
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
571
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
572 Why the tuple? Because the DB API requires you to pass in any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
573 parameters as a sequence. Due to the design of the parser, (max_price)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
574 is interpreted as using algebraic grouping and simply as max_price and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
575 not a tuple. Adding a comma, i.e. (max_price,) forces it to make a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
576 tuple.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
577
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
578 And now, the results::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
579
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
580 >>> c.fetchone()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
581 (3L, 2L, 0L)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
582
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
583 Quite unlike the ``_mysql`` example, this returns a single tuple,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
584 which is the row, and the values are properly converted by default...
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
585 except... What's with the L's?
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
586
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
587 As mentioned earlier, while MySQL's INTEGER column translates
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
588 perfectly into a Python integer, UNSIGNED INTEGER could overflow, so
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
589 these values are converted to Python long integers instead.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
590
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
591 If you wanted more rows, you could use ``c.fetchmany(n)`` or
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
592 ``c.fetchall()``. These do exactly what you think they do. On
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
593 ``c.fetchmany(n)``, the ``n`` is optional and defaults to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
594 ``c.arraysize``, which is normally 1. Both of these methods return a
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
595 sequence of rows, or an empty sequence if there are no more rows. If
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
596 you use a weird cursor class, the rows themselves might not be tuples.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
597
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
598 Note that in contrast to the above, ``c.fetchone()`` returns ``None``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
599 when there are no more rows to fetch.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
600
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
601 The only other method you are very likely to use is when you have to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
602 do a multi-row insert::
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
603
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
604 c.executemany(
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
605 """INSERT INTO breakfast (name, spam, eggs, sausage, price)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
606 VALUES (%s, %s, %s, %s, %s)""",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
607 [
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
608 ("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
609 ("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
610 ("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
611 ] )
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
612
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
613 Here we are inserting three rows of five values. Notice that there is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
614 a mix of types (strings, ints, floats) though we still only use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
615 ``%s``. And also note that we only included format strings for one
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
616 row. MySQLdb picks those out and duplicates them for each row.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
617
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
618 Using and extending
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
619 -------------------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
620
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
621 In general, it is probably wise to not directly interact with the DB
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
622 API except for small applicatons. Databases, even SQL databases, vary
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
623 widely in capabilities and may have non-standard features. The DB API
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
624 does a good job of providing a reasonably portable interface but some
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
625 methods are non-portable. Specifically, the parameters accepted by
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
626 ``connect()`` are completely implementation-dependent.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
627
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
628 If you believe your application may need to run on several different
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
629 databases, the author recommends the following approach, based on
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
630 personal experience: Write a simplified API for your application which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
631 implements the specific queries and operations your application needs
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
632 to perform. Implement this API as a base class which should be have
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
633 few database dependencies, and then derive a subclass from this which
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
634 implements the necessary dependencies. In this way, porting your
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
635 application to a new database should be a relatively simple matter of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
636 creating a new subclass, assuming the new database is reasonably
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
637 standard.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
638
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
639 Because MySQLdb's Connection and Cursor objects are written in Python,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
640 you can easily derive your own subclasses. There are several Cursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
641 classes in MySQLdb.cursors:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
642
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
643 BaseCursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
644 The base class for Cursor objects. This does not raise Warnings.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
645
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
646 CursorStoreResultMixIn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
647 Causes the Cursor to use the ``mysql_store_result()`` function to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
648 get the query result. The entire result set is stored on the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
649 client side.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
650
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
651 CursorUseResultMixIn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
652 Causes the cursor to use the ``mysql_use_result()`` function to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
653 get the query result. The result set is stored on the server side
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
654 and is transferred row by row using fetch operations.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
655
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
656 CursorTupleRowsMixIn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
657 Causes the cursor to return rows as a tuple of the column values.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
658
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
659 CursorDictRowsMixIn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
660
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
661 Causes the cursor to return rows as a dictionary, where the keys
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
662 are column names and the values are column values. Note that if
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
663 the column names are not unique, i.e., you are selecting from two
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
664 tables that share column names, some of them will be rewritten as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
665 ``table.column``. This can be avoided by using the SQL ``AS``
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
666 keyword. (This is yet-another reason not to use ``*`` in SQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
667 queries, particularly where ``JOIN`` is involved.)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
668
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
669 Cursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
670 The default cursor class. This class is composed of
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
671 ``CursorWarningMixIn``, ``CursorStoreResultMixIn``,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
672 ``CursorTupleRowsMixIn,`` and ``BaseCursor``, i.e. it raises
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
673 ``Warning``, uses ``mysql_store_result()``, and returns rows as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
674 tuples.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
675
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
676 DictCursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
677 Like ``Cursor`` except it returns rows as dictionaries.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
678
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
679 SSCursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
680 A "server-side" cursor. Like ``Cursor`` but uses
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
681 ``CursorUseResultMixIn``. Use only if you are dealing with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
682 potentially large result sets.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
683
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
684 SSDictCursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
685 Like ``SSCursor`` except it returns rows as dictionaries.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
686
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
687
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
688 Embedded Server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
689 ---------------
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
690
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
691 Instead of connecting to a stand-alone server over the network,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
692 the embedded server support lets you run a full server right in
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
693 your Python code or application server.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
694
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
695 If you have built MySQLdb with embedded server support, there
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
696 are two additional functions you will need to make use of:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
697
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
698 server_init(args, groups)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
699 Initialize embedded server. If this client is not linked against
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
700 the embedded server library, this function does nothing.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
701
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
702 args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
703 sequence of command-line arguments
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
704 groups
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
705 sequence of groups to use in defaults files
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
706
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
707 server_end()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
708 Shut down embedded server. If not using an embedded server, this
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
709 does nothing.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
710
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
711 See the MySQL documentation for more information on the embedded
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
712 server.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
713
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
714
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
715
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
716 :Title: MySQLdb: a Python interface for MySQL
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
717 :Author: Andy Dustman
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
718 :Version: $Revision$