python连接MySQL数据库用的是MySQLdb扩展模块,其中MySQLdb包下得__init__.py文件中有这样的一条语句:
connect = Connection = Connect
也就是说我们用如下的三种方法是等效的
1、conn = MySQLdb.connect ()
2、conn = MySQLdb.Connection ()
3、conn = MySQLdb.Connect()
在MySQLdb包下的connections模块中有如下文档字符串,用于说明我们在建立连接是可以指定哪些参数。
大意如下:
强烈推荐在建立数据库连接时使用关键字参数(原因参数太多)。想了解更多信息参阅MySQL C API。
host:字符串类型,指定连接的主机
user:字符串类型,指定连接的用户名
passwd:字符串类型,指定连接的密码
db:字符窜类型,指定连接的数据库
port:integer类型,指定连接的端口号
unix_socket:字符串类型,指定unix套接字的位置
conv:转换字典,参考MySQLdb.converters模块
connect_timeout:连接超时的时间,单位秒
compress:是否压缩
named_pipe:命名管道
init_command:连接建立后执行的第一条命令
Create a connection to the database. It is strongly recommended
that you only use keyword parameters. Consult the MySQL C API
documentation for more information.
host
string, host to connect
user
string, user to connect as
passwd
string, password to use
db
string, database to use
port
integer, TCP/IP port to connect to
unix_socket
string, location of unix_socket to use
conv
conversion dictionary, see MySQLdb.converters
connect_timeout
number of seconds to wait before the connection attempt
fails.
compress
if set, compression is enabled
named_pipe
if set, a named pipe is used to connect (Windows only)
init_command
command which is run once the connection is created
read_default_file
file from which default client values are read
read_default_group
configuration group to use from the default file
cursorclass
class object, used to create cursors (keyword only)
use_unicode
If True, text-like columns are returned as unicode objects
using the connection's character set. Otherwise, text-like
columns are returned as strings. columns are returned as
normal strings. Unicode objects will always be encoded to
the connection's character set regardless of this setting.
charset
If supplied, the connection character set will be changed
to this character set (MySQL-4.1 and newer). This implies
use_unicode=True.
sql_mode
If supplied, the session SQL mode will be changed to this
setting (MySQL-4.1 and newer). For more details and legal
values, see the MySQL documentation.
client_flag
integer, flags to use or 0
(see MySQL docs or constants/CLIENTS.py)
ssl
dictionary or mapping, contains SSL connection parameters;
see the MySQL documentation for more details
(mysql_ssl_set()). If this is set, and the client does not
support SSL, NotSupportedError will be raised.
local_infile
integer, non-zero enables LOAD LOCAL INFILE; zero disables
There are a number of undocumented, non-standard methods. See the
documentation for the MySQL C API for some hints on what they do.
来自
http://blog.163.com/longsu2010@yeah/blog/static/173612348201174113815700/