pymysql - PyMySQL: A pure-Python MySQL client library.
| Use Case | Command | Description |
|---|---|---|
| π Connect | pymysql.connect(host='...', user='...', password='...', database='...') | Open a MySQL connection |
| π Execute query | cursor.execute("SELECT * FROM t") | Run a SQL statement |
| π₯ Fetch one row | cursor.fetchone() | Get next result row |
| π₯ Fetch all rows | cursor.fetchall() | Get all remaining rows |
| πΎ Commit | connection.commit() | Save transaction changes |
| βͺ Rollback | connection.rollback() | Undo transaction changes |
| π Autocommit | connection.autocommit(True) | Enable automatic commit |
| πͺ Close | connection.close() | Close the connection |
| π Dict cursor | pymysql.cursors.DictCursor | Return rows as dictionaries |
| π‘οΈ SSL connection | pymysql.connect(..., ssl_ca='ca.pem', ssl_cert='cert.pem', ssl_key='key.pem') | Secure connection with certificates |
Copyright (c) 2010-2016 PyMySQL contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Exception hierarchy:
builtins.Exception(builtins.BaseException)
pymysql.err.MySQLError
pymysql.err.Error
pymysql.err.DatabaseError
pymysql.err.DataError
pymysql.err.IntegrityError
pymysql.err.InternalError
pymysql.err.NotSupportedError
pymysql.err.OperationalError
pymysql.err.ProgrammingError
pymysql.err.InterfaceError
pymysql.err.Warning(builtins.Warning, pymysql.err.MySQLError)
builtins.Warning(builtins.Exception)
pymysql.err.Warning(builtins.Warning, pymysql.err.MySQLError)
builtins.frozenset(builtins.object)
DBAPISet
builtins.object
datetime.date
datetime.datetime
datetime.time
pymysql.connections.Connection
Connect = class Connection(builtins.object) β Alias for pymysql.connections.Connection.
Representation of a socket with a mysql server. The proper way to get an instance is to call connect().
Establish a connection to the MySQL database. Accepts several arguments:
pymysql.cursors.Cursor).See Connection in the specification.
__del__ = _force_close(self)__enter__(self)__exit__(self, *exc_info)__init__(self, *, user=None, password='', host=None, database=None, unix_socket=None, port=0, charset='', collation=None, sql_mode=None, read_default_file=None, conv=None, use_unicode=True, client_flag=0, cursorclass=<class 'pymysql.cursors.Cursor'>, init_command=None, connect_timeout=10, read_default_group=None, autocommit=False, local_infile=False, max_allowed_packet=16777216, defer_connect=False, auth_plugin_map=None, read_timeout=None, write_timeout=None, bind_address=None, binary_prefix=False, program_name=None, server_public_key=None, ssl=None, ssl_ca=None, ssl_cert=None, ssl_disabled=None, ssl_key=None, ssl_key_password=None, ssl_verify_cert=None, ssl_verify_identity=None, compress=None, named_pipe=None, passwd=None, db=None) β Initialize self.affected_rows(self)autocommit(self, value)begin(self) β Begin transaction.character_set_name(self)close(self) β Send the quit message and close the socket. See Connection.close(). Raises Error if already closed.commit(self) β Commit changes to stable storage. See Connection.commit().connect(self, sock=None)cursor(self, cursor=None) β Create a new cursor to execute queries with. cursor type: Cursor, SSCursor, DictCursor, or SSDictCursor.escape(self, obj, mapping=None) β Escape whatever value is passed. Nonβstandard, internal use only.escape_string(self, s)get_autocommit(self)get_host_info(self)get_proto_info(self)get_server_info(self)insert_id(self)kill(self, thread_id)literal(self, obj) β Alias for escape(). Nonβstandard, internal use only.next_result(self, unbuffered=False)ping(self, reconnect=False) β Check if the server is alive. reconnect is deprecated; create a new connection. Raises Error if closed and reconnect=False.query(self, sql, unbuffered=False) β # The following methods are INTERNAL USE ONLY (called from Cursor)rollback(self) β Roll back the current transaction. See Connection.rollback().select_db(self, db) β Set current db.set_character_set(self, charset, collation=None) β Send "SET NAMES" query, update Connection.encoding.set_charset(self, charset) β Deprecated. Use set_character_set().show_warnings(self) β Send "SHOW WARNINGS".thread_id(self) β # _mysql supportwrite_packet(self, payload) β Writes an entire mysql packet to the network adding its length and sequence number.open β Return True if the connection is open.__dict__ β dictionary for instance variables__weakref__ β list of weak references to the objectDataError = <class 'pymysql.err.DataError'>
DatabaseError = <class 'pymysql.err.DatabaseError'>
Error = <class 'pymysql.err.Error'>
IntegrityError = <class 'pymysql.err.IntegrityError'>
InterfaceError = <class 'pymysql.err.InterfaceError'>
InternalError = <class 'pymysql.err.InternalError'>
NotSupportedError = <class 'pymysql.err.NotSupportedError'>
OperationalError = <class 'pymysql.err.OperationalError'>
ProgrammingError = <class 'pymysql.err.ProgrammingError'>
Warning = <class 'pymysql.err.Warning'>
class DBAPISet(builtins.frozenset)
Methods defined here:
__eq__(self, other) β Return self==value.__hash__(self) β Return hash(self).__ne__(self, other) β Return self!=value.Inherits from frozenset (all set operations).
class DataError(DatabaseError)
Exception raised for errors due to problems with processed data (division by zero, numeric value out of range, etc.).
Inherits from DatabaseError β Error β MySQLError β Exception.
class DatabaseError(Error)
Exception raised for errors related to the database.
class Error(MySQLError)
Base class of all error exceptions (not Warning).
class IntegrityError(DatabaseError)
Exception raised when relational integrity is affected (foreign key check fails, duplicate key, etc.).
class InterfaceError(Error)
Exception raised for errors related to the database interface rather than the database itself.
class InternalError(DatabaseError)
Exception raised when the database encounters an internal error (cursor invalid, transaction out of sync, etc.).
class NotSupportedError(DatabaseError)
Exception raised when a method or database API is used that is not supported by the database (e.g., .rollback() on a connection without transactions).
class OperationalError(DatabaseError)
Exception raised for errors related to the database's operation (unexpected disconnect, data source name not found, transaction processing failure, memory allocation error, etc.).
class ProgrammingError(DatabaseError)
Exception raised for programming errors (table not found or already exists, syntax error in SQL, wrong number of parameters, etc.).
class Warning(builtins.Warning, MySQLError)
Exception raised for important warnings (data truncations while inserting, etc.).
Date = class date(builtins.object)
date(year, month, day) β date object
Standard Python datetime.date.
Time = class time(builtins.object)
time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) β a time object
Standard Python datetime.time.
Timestamp = class datetime(date)
datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
Standard Python datetime.datetime.
class MySQLError(builtins.Exception)
Exception related to operation with MySQL.
Binary(x) β Return x as a binary type.DateFromTicks(ticks)TimeFromTicks(ticks)TimestampFromTicks(ticks)get_client_info()install_as_MySQLdb() β After this function is called, any application that imports MySQLdb will unwittingly actually use pymysql.BINARY = DBAPISet({249, 250, 251, 252})
DATE = DBAPISet({10, 14})
NULL = 'NULL'
NUMBER = DBAPISet({0, 1, 3, 4, 5, 8, 9, 13})
ROWID = DBAPISet()
STRING = DBAPISet({253, 254, 247})
TIME = DBAPISet({11})
TIMESTAMP = DBAPISet({12, 7})
__all__ = ['BINARY', 'Binary', 'Connect', 'Connection', 'DATE', 'Date'...]
apilevel = '2.0'
paramstyle = 'pyformat'
threadsafety = 1
version_info = (2, 2, 8, 'final', 1)
2.2.8
/home/chedong/.local/lib/python3.10/site-packages/pymysql/__init__.py
Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-18 11:07 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format