user_guide.rst | user_guide.rst | |||
---|---|---|---|---|
skipping to change at line 152 | skipping to change at line 152 | |||
Generally speaking, putting passwords in your code is not such a good | Generally speaking, putting passwords in your code is not such a good | |||
idea:: | idea:: | |||
db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my. cnf") | db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my. cnf") | |||
This does what the previous example does, but gets the username and | This does what the previous example does, but gets the username and | |||
password and other parameters from ~/.my.cnf (UNIX-like systems). Read | password and other parameters from ~/.my.cnf (UNIX-like systems). Read | |||
about `option files`_ for more details. | about `option files`_ for more details. | |||
.. _`option files`: http://dev.mysql.com/doc/mysql/en/Option_files.html | .. _`option files`: http://dev.mysql.com/doc/refman/en/option-files.html | |||
So now you have an open connection as ``db`` and want to do a | So now you have an open connection as ``db`` and want to do a | |||
query. Well, there are no cursors in MySQL, and no parameter | query. Well, there are no cursors in MySQL, and no parameter | |||
substitution, so you have to pass a complete query string to | substitution, so you have to pass a complete query string to | |||
``db.query()``:: | ``db.query()``:: | |||
db.query("""SELECT spam, eggs, sausage FROM breakfast | db.query("""SELECT spam, eggs, sausage FROM breakfast | |||
WHERE price < 5""") | WHERE price < 5""") | |||
There's no return value from this, but exceptions can be raised. The | There's no return value from this, but exceptions can be raised. The | |||
skipping to change at line 366 | skipping to change at line 366 | |||
*This must be a keyword parameter.* | *This must be a keyword parameter.* | |||
ssl | ssl | |||
This parameter takes a dictionary or mapping, where the | This parameter takes a dictionary or mapping, where the | |||
keys are parameter names used by the mysql_ssl_set_ MySQL | keys are parameter names used by the mysql_ssl_set_ MySQL | |||
C API call. If this is set, it initiates an SSL connection | C API call. If this is set, it initiates an SSL connection | |||
to the server; if there is no SSL support in the client, | to the server; if there is no SSL support in the client, | |||
an exception is raised. *This must be a keyword | an exception is raised. *This must be a keyword | |||
parameter.* | parameter.* | |||
.. _mysql_ssl_set: http://dev.mysql.com/doc/mysql/en/mysql_ssl_set.html | .. _mysql_ssl_set: http://dev.mysql.com/doc/refman/en/mysql-ssl-set.html | |||
apilevel | apilevel | |||
String constant stating the supported DB API level. '2.0' | String constant stating the supported DB API level. '2.0' | |||
threadsafety | threadsafety | |||
Integer constant stating the level of thread safety the | Integer constant stating the level of thread safety the | |||
interface supports. This is set to 1, which means: Threads may | interface supports. This is set to 1, which means: Threads may | |||
share the module. | share the module. | |||
The MySQL protocol can not handle multiple threads using the | The MySQL protocol can not handle multiple threads using the | |||
skipping to change at line 401 | skipping to change at line 401 | |||
The general upshot of this is: Don't share connections between | The general upshot of this is: Don't share connections between | |||
threads. It's really not worth your effort or mine, and in the | threads. It's really not worth your effort or mine, and in the | |||
end, will probably hurt performance, since the MySQL server runs | end, will probably hurt performance, since the MySQL server runs | |||
a separate thread for each connection. You can certainly do | a separate thread for each connection. You can certainly do | |||
things like cache connections in a pool, and give those | things like cache connections in a pool, and give those | |||
connections to one thread at a time. If you let two threads use | connections to one thread at a time. If you let two threads use | |||
a connection simultaneously, the MySQL client library will | a connection simultaneously, the MySQL client library will | |||
probably upchuck and die. You have been warned. | probably upchuck and die. You have been warned. | |||
For threaded applications, try using a connection pool. | ||||
This can be done using the `Pool module`_. | ||||
.. _`Pool module`: http://dustman.net/andy/python/Pool | ||||
charset | charset | |||
The character set used by the connection. In MySQL-4.1 and newer, | The character set used by the connection. In MySQL-4.1 and newer, | |||
it is possible (but not recommended) to change the connection's | it is possible (but not recommended) to change the connection's | |||
character set with an SQL statement. If you do this, you'll also | character set with an SQL statement. If you do this, you'll also | |||
need to change this attribute. Otherwise, you'll get encoding | need to change this attribute. Otherwise, you'll get encoding | |||
errors. | errors. | |||
paramstyle | paramstyle | |||
String constant stating the type of parameter marker formatting | String constant stating the type of parameter marker formatting | |||
expected by the interface. Set to 'format' = ANSI C printf | expected by the interface. Set to 'format' = ANSI C printf | |||
skipping to change at line 444 | skipping to change at line 439 | |||
can be either: | can be either: | |||
* a callable object which takes a string argument (the MySQL | * a callable object which takes a string argument (the MySQL | |||
value),' returning a Python value | value),' returning a Python value | |||
* a sequence of 2-tuples, where the first value is a combination | * a sequence of 2-tuples, where the first value is a combination | |||
of flags from ``MySQLdb.constants.FLAG``, and the second value | of flags from ``MySQLdb.constants.FLAG``, and the second value | |||
is a function as above. The sequence is tested until the flags | is a function as above. The sequence is tested until the flags | |||
on the field match those of the first value. If both values | on the field match those of the first value. If both values | |||
are None, then the default conversion is done. Presently this | are None, then the default conversion is done. Presently this | |||
is only used to distinquish TEXT and BLOB columns. | is only used to distinguish TEXT and BLOB columns. | |||
If the key is a Python type or class, then the value is a | If the key is a Python type or class, then the value is a | |||
callable Python object (usually a function) taking two arguments | callable Python object (usually a function) taking two arguments | |||
(value to convert, and the conversion dictionary) which converts | (value to convert, and the conversion dictionary) which converts | |||
values of this type to a SQL literal string value. | values of this type to a SQL literal string value. | |||
This is initialized with reasonable defaults for most | This is initialized with reasonable defaults for most | |||
types. When creating a Connection object, you can pass your own | types. When creating a Connection object, you can pass your own | |||
type converter dictionary as a keyword parameter. Otherwise, it | type converter dictionary as a keyword parameter. Otherwise, it | |||
uses a copy of ``MySQLdb.converters.conversions``. Several | uses a copy of ``MySQLdb.converters.conversions``. Several | |||
skipping to change at line 615 | skipping to change at line 610 | |||
Here we are inserting three rows of five values. Notice that there is | Here we are inserting three rows of five values. Notice that there is | |||
a mix of types (strings, ints, floats) though we still only use | a mix of types (strings, ints, floats) though we still only use | |||
``%s``. And also note that we only included format strings for one | ``%s``. And also note that we only included format strings for one | |||
row. MySQLdb picks those out and duplicates them for each row. | row. MySQLdb picks those out and duplicates them for each row. | |||
Using and extending | Using and extending | |||
------------------- | ------------------- | |||
In general, it is probably wise to not directly interact with the DB | In general, it is probably wise to not directly interact with the DB | |||
API except for small applicatons. Databases, even SQL databases, vary | API except for small applications. Databases, even SQL databases, vary | |||
widely in capabilities and may have non-standard features. The DB API | widely in capabilities and may have non-standard features. The DB API | |||
does a good job of providing a reasonably portable interface but some | does a good job of providing a reasonably portable interface but some | |||
methods are non-portable. Specifically, the parameters accepted by | methods are non-portable. Specifically, the parameters accepted by | |||
``connect()`` are completely implementation-dependent. | ``connect()`` are completely implementation-dependent. | |||
If you believe your application may need to run on several different | If you believe your application may need to run on several different | |||
databases, the author recommends the following approach, based on | databases, the author recommends the following approach, based on | |||
personal experience: Write a simplified API for your application which | personal experience: Write a simplified API for your application which | |||
implements the specific queries and operations your application needs | implements the specific queries and operations your application needs | |||
to perform. Implement this API as a base class which should be have | to perform. Implement this API as a base class which should be have | |||
End of changes. 5 change blocks. | ||||
9 lines changed or deleted | 4 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |