标签:abs 2.7 margin set *args 2.3 连接数据库 api port
下载:MySQL-python-1.2.3.win-amd64-py2.7直接安装,要求Python2.7(Python version 2.7 required)
验证:import MySQLdb 不报错就可以了
1 连接数据库:MySQLdb.connect(host=‘‘,port=‘‘,user=‘‘,passwd=‘‘,db=‘‘)
class Connection(_mysql.connection):
"""MySQL Database Connection Object"""
default_cursor = cursors.Cursor
def __init__(self, *args, **kwargs):
"""
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
charset
If supplied, the connection character set will be changed
to this character set (MySQL-4.1 and newer). This implies
use_unicode=True.
"""
2 操作数据库:首先需要获得一个cursor对象, 然后使用cursor的方法执行SQL
def execute(self, query, args=None):
"""Execute a query.
query -- string, query to execute on server
args -- optional sequence or mapping, parameters to use with query.
Note: If args is a sequence, then %s must be used as the
parameter placeholder in the query. If a mapping is used,
%(key)s must be used as the placeholder.
Returns long integer rows affected, if any
"""
3 接收返回值:也是使用cursor对象的方法进行接收
4 关闭数据库:需要关闭cursor对象和connect对象
标签:abs 2.7 margin set *args 2.3 连接数据库 api port
原文地址:http://www.cnblogs.com/lizitest/p/6664037.html