python学习之PyMySql 1 db的方法

我们通过代码查看了方法。

DataError
DatabaseError
Error
IntegrityError
InterfaceError
InternalError
NotSupportedError
OperationalError
ProgrammingError
Warning
affected_rows
autocommit
autocommit_mode
begin
bind_address
character_set_name
charset
client_flag
close
commit
connect
connect_timeout
cursor
cursorclass
db
decoders
encoders
encoding
escape
escape_string
get_autocommit
get_host_info
get_proto_info
get_server_info
host
host_info
init_command
insert_id
kill
literal
max_allowed_packet
next_result
open
password
ping
port
protocol_version
query
rollback
salt
select_db
server_capabilities
server_charset
server_language
server_status
server_thread_id
server_version
set_charset
show_warnings
sql_mode
ssl
thread_id
unix_socket
use_unicode
user
write_packet

close函数

    def close(self):
        """
        Send the quit message and close the socket.

        See `Connection.close() <https://www.python.org/dev/peps/pep-0249/#Connection.close>`_
        in the specification.
        
        :raise Error: If the connection is already closed.
        """

翻译:发送退出信息并且关闭套接字。如果连接已经关闭就会抛出异常。多次关闭或者异常关闭了。

open函数

@property
    def open(self):
        """Return True if the connection is open"""
        return self._sock is not None

翻译:如果连接是开启状态就会返回Ture,反之亦然。

begin函数

def begin(self):
        """Begin transaction."""

翻译:无返回值,开启事务。

commit函数

def commit(self):
        """
        Commit changes to stable storage.
        
        See `Connection.commit() <https://www.python.org/dev/peps/pep-0249/#commit>`_
        in the specification.
        """

翻译:将变化后的数据存储到内存中,帮助文档

rollback函数

def rollback(self):
        """
        Roll back the current transaction.
        
        See `Connection.rollback() <https://www.python.org/dev/peps/pep-0249/#rollback>`_
        in the specification.
        """

翻译:回滚当前的事务,rollback帮助文档

show_warnings函数

 def show_warnings(self):
        """Send the "SHOW WARNINGS" SQL command."""
        self._execute_command(COMMAND.COM_QUERY, "SHOW WARNINGS")
        result = MySQLResult(self)
        result.read()
        return result.rows

翻译:发送show_warnings指令,将得到的结果返回回来。

literal函数

    def literal(self, obj):
        """Alias for escape()
        
        Non-standard, for internal use; do not use this in your applications.
        """
        return self.escape(obj, self.encoders)

翻译:escape的别名

cursor函数

def cursor(self, cursor=None):
        """
        Create a new cursor to execute queries with.
        
        :param cursor: The type of cursor to create; one of :py:class:`Cursor`,
            :py:class:`SSCursor`, :py:class:`DictCursor`, or :py:class:`SSDictCursor`.
            None means use Cursor.
        """
        if cursor:
            return cursor(self)
        return self.cursorclass(self)

翻译:为查询结果建立一个游标,参数cursor:表示将要游标创建的类型。有如下几种Cursor默认。

SSCursor,DictCursor,或者SSDictCursor.这些都在pymysql.cursor里面有定义。

query函数

# The following methods are INTERNAL USE ONLY (called from Cursor)
    def query(self, sql, unbuffered=False):
        # if DEBUG:
        #     print("DEBUG: sending query:", sql)
        if isinstance(sql, text_type) and not (JYTHON or IRONPYTHON):
            if PY2:
                sql = sql.encode(self.encoding)
            else:
                sql = sql.encode(self.encoding, 'surrogateescape')
        self._execute_command(COMMAND.COM_QUERY, sql)
        self._affected_rows = self._read_query_result(unbuffered=unbuffered)
        return self._affected_rows

翻译:这个方法是用来做查询的,返回的结果是影响的行数。

try:
    db = pymysql.connect(host="127.0.0.1",user="root",port=3306,password="123456",database="android")
    print(type(db.query("show tables")))
    db.close()
    print("连接成功")
except:
    print("连接失败")
<class 'int'>
连接成功

next_result函数

def next_result(self, unbuffered=False):
        self._affected_rows = self._read_query_result(unbuffered=unbuffered)
        return self._affected_rows

翻译:获取查询结果,返回是行数。

affected_rows函数

def affected_rows(self):
        return self._affected_rows

翻译:返回影响的行数。

ping函数

def ping(self, reconnect=True):
        """
        Check if the server is alive.
        
        :param reconnect: If the connection is closed, reconnect.
        :raise Error: If the connection is closed and reconnect=False.
        """
        if self._sock is None:
            if reconnect:
                self.connect()
                reconnect = False
            else:
                raise err.Error("Already closed")
        try:
            self._execute_command(COMMAND.COM_PING, "")
            self._read_ok_packet()
        except Exception:
            if reconnect:
                self.connect()
                self.ping(False)
            else:
                raise

翻译:检测主机是否存活,默认重新连接。如果当前的sock为None且非重连模式,则会抛出异常。然后进行查询。

cursor函数

def cursor(self, cursor=None):
        """
        Create a new cursor to execute queries with.
        
        :param cursor: The type of cursor to create; one of :py:class:`Cursor`,
            :py:class:`SSCursor`, :py:class:`DictCursor`, or :py:class:`SSDictCursor`.
            None means use Cursor.
        """
        if cursor:
            return cursor(self)
        return self.cursorclass(self)

翻译:

set_charset函数

def set_charset(self, charset):
        # Make sure charset is supported.
        encoding = charset_by_name(charset).encoding


        self._execute_command(COMMAND.COM_QUERY, "SET NAMES %s" % self.escape(charset))
        self._read_packet()
        self.charset = charset
        self.encoding = encoding

翻译:首先确保支持charset,设置字符集。设置解码格式。












猜你喜欢

转载自blog.csdn.net/rubikchen/article/details/80514221