====================
MySQLdb User's Guide
====================
.. contents::
..
Introduction
------------
MySQLdb is an thread-compatible interface to the popular MySQL
database server that provides the Python database API.
Installation
------------
The ``README`` file has complete installation instructions.
_mysql
------
If you want to write applications which are portable across databases,
use MySQLdb_, and avoid using this module directly. ``_mysql``
provides an interface which mostly implements the MySQL C API. For
more information, see the `MySQL documentation`_. The documentation
for this module is intentionally weak because you probably should use
the higher-level MySQLdb module. If you really need it, use the
standard MySQL docs and transliterate as necessary.
.. _`MySQL documentation`: http://dev.mysql.com/doc/
MySQL C API translation
.......................
The MySQL C API has been wrapped in an object-oriented way. The only
MySQL data structures which are implemented are the ``MYSQL``
(database connection handle) and ``MYSQL_RES`` (result handle)
types. In general, any function which takes ``MYSQL *mysql`` as an
argument is now a method of the connection object, and any function
which takes ``MYSQL_RES *result`` as an argument is a method of the
result object. Functions requiring none of the MySQL data structures
are implemented as functions in the module. Functions requiring one of
the other MySQL data structures are generally not implemented.
Deprecated functions are not implemented. In all cases, the ``mysql_``
prefix is dropped from the name. Most of the ``conn`` methods listed
are also available as MySQLdb Connection object methods. Their use is
non-portable.
MySQL C API function mapping
............................
=================================== ==================================
C API ``_mysql``
=================================== ==================================
``mysql_affected_rows()`` ``conn.affected_rows()``
``mysql_autocommit()`` ``conn.autocommit()``
``mysql_character_set_name()`` ``conn.character_set_name()``
``mysql_close()`` ``conn.close()``
``mysql_commit()`` ``conn.commit()``
``mysql_connect()`` ``_mysql.connect()``
``mysql_data_seek()`` ``result.data_seek()``
``mysql_debug()`` ``_mysql.debug()``
``mysql_dump_debug_info`` ``conn.dump_debug_info()``
``mysql_escape_string()`` ``_mysql.escape_string()``
``mysql_fetch_row()`` ``result.fetch_row()``
``mysql_get_character_set_info()`` ``conn.get_character_set_info()``
``mysql_get_client_info()`` ``_mysql.get_client_info()``
``mysql_get_host_info()`` ``conn.get_host_info()``
``mysql_get_proto_info()`` ``conn.get_proto_info()``
``mysql_get_server_info()`` ``conn.get_server_info()``
``mysql_info()`` ``conn.info()``
``mysql_insert_id()`` ``conn.insert_id()``
``mysql_num_fields()`` ``result.num_fields()``
``mysql_num_rows()`` ``result.num_rows()``
``mysql_options()`` various options to ``_mysql.connect()``
``mysql_ping()`` ``conn.ping()``
``mysql_query()`` ``conn.query()``
``mysql_real_connect()`` ``_mysql.connect()``
``mysql_real_query()`` ``conn.query()``
``mysql_real_escape_string()`` ``conn.escape_string()``
``mysql_rollback()`` ``conn.rollback()``
``mysql_row_seek()`` ``result.row_seek()``
``mysql_row_tell()`` ``result.row_tell()``
``mysql_select_db()`` ``conn.select_db()``
``mysql_set_character_set()`` ``conn.set_character_set()``
``mysql_ssl_set()`` ``ssl`` option to ``_mysql.connect()``
``mysql_stat()`` ``conn.stat()``
``mysql_store_result()`` ``conn.store_result()``
``mysql_thread_id()`` ``conn.thread_id()``
``mysql_thread_safe_client()`` ``conn.thread_safe_client()``
``mysql_use_result()`` ``conn.use_result()``
``mysql_warning_count()`` ``conn.warning_count()``
``CLIENT_*`` ``MySQLdb.constants.CLIENT.*``
``CR_*`` ``MySQLdb.constants.CR.*``
``ER_*`` ``MySQLdb.constants.ER.*``
``FIELD_TYPE_*`` ``MySQLdb.constants.FIELD_TYPE.*``
``FLAG_*`` ``MySQLdb.constants.FLAG.*``
=================================== ==================================
Some _mysql examples
....................
Okay, so you want to use ``_mysql`` anyway. Here are some examples.
The simplest possible database connection is::
import _mysql
db=_mysql.connect()
This creates a connection to the MySQL server running on the local
machine using the standard UNIX socket (or named pipe on Windows),
your login name (from the USER environment variable), no password, and
does not ``USE`` a database. Chances are you need to supply more
information.::
db=_mysql.connect("localhost","joebob","moonpie","thangs")
This creates a connection to the MySQL server running on the local
machine via a UNIX socket (or named pipe), the user name "joebob", the
password "moonpie", and selects the initial database "thangs".
We haven't even begun to touch upon all the parameters ``connect()``
can take. For this reason, I prefer to use keyword parameters::
db=_mysql.connect(host="localhost",user="joebob",
passwd="moonpie",db="thangs")
This does exactly what the last example did, but is arguably easier to
read. But since the default host is "localhost", and if your login
name really was "joebob", you could shorten it to this::
db=_mysql.connect(passwd="moonpie",db="thangs")
UNIX sockets and named pipes don't work over a network, so if you
specify a host other than localhost, TCP will be used, and you can
specify an odd port if you need to (the default port is 3306)::
db=_mysql.connect(host="outhouse",port=3307,passwd="moonpie",db="thangs")
If you really had to, you could connect to the local host with TCP by
specifying the full host name, or 127.0.0.1.
Generally speaking, putting passwords in your code is not such a good
idea::
db=_mysql.connect(host="outhouse",db="thangs",read_default_file="~/.my.cnf")
This does what the previous example does, but gets the username and
password and other parameters from ~/.my.cnf (UNIX-like systems). Read
about `option files`_ for more details.
.. _`option files`: http://dev.mysql.com/doc/mysql/en/Option_files.html
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
substitution, so you have to pass a complete query string to
``db.query()``::
db.query("""SELECT spam, eggs, sausage FROM breakfast
WHERE price < 5""")
There's no return value from this, but exceptions can be raised. The
exceptions are defined in a separate module, ``_mysql_exceptions``,
but ``_mysql`` exports them. Read DB API specification PEP-249_ to
find out what they are, or you can use the catch-all ``MySQLError``.
.. _PEP-249: http://www.python.org/peps/pep-0249.html
At this point your query has been executed and you need to get the
results. You have two options::
r=db.store_result()
# ...or...
r=db.use_result()
Both methods return a result object. What's the difference?
``store_result()`` returns the entire result set to the client
immediately. If your result set is really large, this could be a
problem. One way around this is to add a ``LIMIT`` clause to your
query, to limit the number of rows returned. The other is to use
``use_result()``, which keeps the result set in the server and sends
it row-by-row when you fetch. This does, however, tie up server
resources, and it ties up the connection: You cannot do any more
queries until you have fetched **all** the rows. Generally I
recommend using ``store_result()`` unless your result set is really
huge and you can't use ``LIMIT`` for some reason.
Now, for actually getting real results::
>>> r.fetch_row()
(('3','2','0'),)
This might look a little odd. The firs
没有合适的资源?快使用搜索试试~ 我知道了~
django,liunx下mysql数据库接口MySQL-python-1.2.3.tar.gz
4星 · 超过85%的资源 需积分: 34 79 下载量 201 浏览量
2010-11-11
21:55:39
上传
评论
收藏 69KB GZ 举报
温馨提示
共43个文件
py:24个
txt:5个
cvsignore:4个
django,liunx下mysql数据库接口MySQL-python-1.2.3.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
MySQL-python-1.2.3.tar.gz (43个子文件)
MySQL-python-1.2.3
setup.py 531B
.cvsignore 45B
site.cfg 592B
setup_posix.py 3KB
MySQLdb
.cvsignore 6B
times.py 3KB
connections.py 11KB
__init__.py 3KB
release.py 117B
constants
.cvsignore 6B
FLAG.py 363B
REFRESH.py 252B
__init__.py 62B
ER.py 12KB
FIELD_TYPE.py 485B
CR.py 674B
CLIENT.py 667B
cursors.py 16KB
converters.py 5KB
setup_windows.py 2KB
MANIFEST.in 272B
_mysql_exceptions.py 2KB
doc
.cvsignore 13B
MySQLdb.txt 28KB
FAQ.txt 5KB
setup.cfg 380B
_mysql.c 74KB
metadata.cfg 2KB
PKG-INFO 2KB
tests
capabilities.py 10KB
test_MySQLdb_capabilities.py 3KB
test_MySQLdb_nonstandard.py 3KB
dbapi20.py 31KB
test_MySQLdb_dbapi20.py 8KB
HISTORY 3KB
pymemcompat.h 3KB
setup_common.py 951B
MySQL_python.egg-info
top_level.txt 33B
SOURCES.txt 868B
PKG-INFO 2KB
dependency_links.txt 1B
README 7KB
ez_setup.py 10KB
共 43 条
- 1
资源评论
- xiangyang199111272014-12-16终于找到了,恩恩
- Nikomememe2018-07-19感谢分享!谢谢
wangy_zhangcj
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- user+name.csv
- 安卓学习教材经验Android进阶学习资料安卓面试资料等文档资料合集(22个).zip
- 王小晨申论高分课.zip
- 基于matlab实现说话人语音识别源码+项目说明+PPT+报告(高分项目).zip
- VSc++编程助手1.0 - 1
- 基于matlab的说话人语音识别源码+PPT+报告(优质项目).zip
- 语音识别基于matlab说话人识别系统源码+报告PPT(高分优质项目).zip
- 毕业设计: 校园失物招领小程序的设计与实现论文(源码 + 数据库 + 说明文档)
- 美易ME-DUI自绘模块3.3-易语言模块
- 毕业设计:小程序消防知识每天学(源码 + 数据库 + 说明文档)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功