python使用pymysql连接mysql并进行数据库交互详细教程(附带详细案例及解析)

本博客为使用python连接mysql数据库,对其主要方法进行解析

pymysql主要使用cursor对数据库进行交互操作和结果处理,所以cursor对象为重中之重

0、一个简单的数据库交互案例

环境安装:pymysql

pip3 install pymysql

本文测试数据库结构:

create table TEST_TABLE
(
	ID int auto_increment
		primary key,
	USER_NAME varchar(255) null,
	USER_PASSWORD varchar(255) null
);

新建一个.py文件,进行一次数据库交互

# 导入package
import pymysql

# 建立数据库连接,可传参数(详见1.2)
db = pymysql.connect(host="localhost", user="root", password="123456", db="Test", charset='utf8mb4')

# 使用 cursor() 方法创建一个游标对象 cursor,可传参数,表示返回到python的结果用什么方式存储(详见2.2)
cursor = db.cursor()

# 使用List作为参数进行查询,塞入参数
params = ['Jack Ma', '123123']

# 使用 execute()  方法执行 SQL 查询,会返回受影响的行数(详见3 - 4)
cursor.execute("select * from TEST_TABLE where USER_NAME = %s and USER_PASSWORD = %s",params)

# 获取结果(详见5)
results = cursor.fetchall()
for result in results:
	  print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s",result[0],result[1],result[2])
	  
db.close()

可以看到,使用pymysql与数据库交互非常简单,大体分为四步:

  1. 建立数据库连接:db = pymysql.connect(host="localhost", user="root", password="123456", db="Test", charset='utf8mb4')
  2. 获取游标对象:cursor = db.cursor()
  3. 调用游标的cursor.execute()方法执行SQL语句
  4. 通过各种cursor.fetch()操作拿到结果集

以下为各个步骤的关键函数的详细解析

1、pymysql.connect()

创建一个数据库连接

1.1、使用案例

# 可在值前指定传入哪个参数
db = pymysql.connect(host = "localhost", user = "root", password = "123456", db = "Test",charset='utf8')

1.2、可选参数

源码注释

class Connection(object):
    """
    Representation of a socket with a mysql server.

    The proper way to get an instance of this class is to call
    connect().

    Establish a connection to the MySQL database. Accepts several
    arguments:
    
:param host: Host where the database server is located
:param user: Username to log in as
:param password: Password to use.
:param database: Database to use, None to not use a particular one.
:param port: MySQL port to use, default is usually OK. (default: 3306)
:param bind_address: When the client has multiple network interfaces, specify
       the interface from which to connect to the host. Argument can be
       a hostname or an IP address.
:param unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
:param read_timeout: The timeout for reading from the connection in seconds (default: None - no timeout)
:param write_timeout: The timeout for writing to the connection in seconds (default: None - no timeout)
:param charset: Charset you want to use.
:param sql_mode: Default SQL_MODE to use.
:param read_default_file:
    Specifies  my.cnf file to read these parameters from under the [client] section.
:param conv:
    Conversion dictionary to use instead of the default one.
    This is used to provide custom marshalling and unmarshalling of types.
    See converters.
:param use_unicode:
    Whether or not to default to unicode strings.
    This option defaults to true for Py3k.
:param client_flag: Custom flags to send to MySQL. Find potential values in constants.CLIENT.
:param cursorclass: Custom cursor class to use.
:param init_command: Initial SQL statement to run when connection is established.
:param connect_timeout: Timeout before throwing an exception when connecting.
    (default: 10, min: 1, max: 31536000)
:param ssl:
    A dict of arguments similar to mysql_ssl_set()'s parameters.
:param read_default_group: Group to read from in the configuration file.
:param compress: Not supported
:param named_pipe: Not supported
:param autocommit: Autocommit mode. None means use server default. (default: False)
:param local_infile: Boolean to enable the use of LOAD DATA LOCAL command. (default: False)
:param max_allowed_packet: Max size of packet sent to server in bytes. (default: 16MB)
    Only used to limit size of "LOAD LOCAL INFILE" data packet smaller than default (16KB).
:param defer_connect: Don't explicitly connect on construction - wait for connect call.
    (default: False)
:param auth_plugin_map: A dict of plugin names to a class that processes that plugin.
    The class will take the Connection object as the argument to the constructor.
    The class needs an authenticate method taking an authentication packet as
    an argument.  For the dialog plugin, a prompt(echo, prompt) method can be used
    (if no authenticate method) for returning a string from the user. (experimental)
:param server_public_key: SHA256 authentication plugin public key value. (default: None)
:param db: Alias for database. (for compatibility to MySQLdb)
:param passwd: Alias for password. (for compatibility to MySQLdb)
:param binary_prefix: Add _binary prefix on bytes and bytearray. (default: False)
"""

2、db.cursor()

创建一个游标,其返回到python的存储形式,需在创建的时候指定(不指定使用默认值 cursor

2.1、使用案例

db = pymysql.connect(host = "localhost", user = "root", password = "123456", db ="LicenseServer",charset='utf8')

# 使用 cursor() 方法创建一个默认游标对象 cursor
cursor = db.cursor()
# 或者创建其他游标对象(需要导入对应包)
SSCursor = db.cursor(SSCursor)

2.2、可选参数

如果无参数,默认为Cursor,可选SSCursor,DictCursor,SSDictCursor

表示返回到Python的结果用什么方式存储,如Dict.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.
    """
Cursor普通的游标对象,默认创建的游标对象
SSCursor不缓存游标,主要用于当操作需要返回大量数据的时候
DictCursor以字典的形式返回操作结果
SSDictCursor不缓存游标,将结果以字典的形式进行返回

3、cursor.execute()

3.1、使用案例

# 直接传入字符串进入 execute()  方法执行 SQL 查询
cursor.execute("select * from TEST_TABLE")
# 使用 execute(),传入一个list作为参数执行 SQL 查询
params = [1,'Jack Ma']
cursor.execute("select * from LICENSE where id = %s and USER_NAME = %s",params)

3.2、可选参数

默认args为空

如果传入的args为一个List或者tuple,可以在sql语句中使用%s 进行替换

如果传入的args为一个dict,可以使用%(name)s

def execute(self, query, args=None):
"""Execute a query

:param str query: Query to execute.

:param args: parameters used with query. (optional)
:type args: tuple, list or dict

:return: Number of affected rows
:rtype: int

If args is a list or tuple, %s can be used as a placeholder in the query.
If args is a dict, %(name)s can be used as a placeholder in the query.

4、cursor.executemany()

向数据表中插入多条数据,使用executemany方法,在生产环境中插入多条数据 ,在后台中获取数据后,以列表的形式传入语句([(‘v1’,‘v2’),(‘v3’,‘v4’)])

4.1、使用案例

使用DML语句时,一定需要加上db.commit(),否则更改不生效

或者使用db.autocommit(true)开启自动commit

# 一次性向数据库添加两条数据
insertParams = [['kuifeng yuan','123123'],['neil','123123123']]
affected_rows  = cursor.executemany("insert into TEST_TABLE(`USER_NAME`,`USER_PASSWORD`) values(%s,%s)",insertParams)
print(affected_rows)

# 切记commit,否则insert不生效
db.commit()

4.2、可选参数

query为必填参数,为sql语句

args为填入sql语句的参数,可以为空,以list传入

def executemany(self, query, args):
  # type: (str, list) -> int
  """Run several data against one query
  
  :param query: query to execute on server
  :param args:  Sequence of sequences or mappings.  It is used as parameter.
  :return: Number of rows affected, if any.

  This method improves performance on multiple-row INSERT and
  REPLACE. Otherwise it is equivalent to looping over args with
  execute().
  """

5、cursor.fetch()

cursor.fetch()不是一个方法,而是表示该类操作的集合。作用为读取被cursor返回的数据

关于fetch有三个具体的方法:

  • cursor.fetchone():读取下一条数据,不用传入任何参数
  • cursor.fetchmany():读取多条数据,需要传入需要读取下面的多少条数据(如 size = 5 表示读取以下五条)
  • cursor.fetchall():读取所有数据,不用传入任何参数

5.1、使用案例

5.1.1、cursor.fetchall()

cursor.execute("select * from TEST_TABLE")
results =  cursor.fetchall()
for result in results:
    print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s" % \
          (result[0],result[1],result[2]))

执行结果:打印所有查询到的值

ID: 1, USER_NAME:Jack Ma, USER_PASSWORD:123123
ID: 2, USER_NAME:Small Ma, USER_PASSWORD:312312123
ID: 3, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 4, USER_NAME:Edison, USER_PASSWORD:345234
ID: 5, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 6, USER_NAME:Edison, USER_PASSWORD:345234
ID: 7, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 8, USER_NAME:Edison, USER_PASSWORD:345234
ID: 9, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 10, USER_NAME:Edison, USER_PASSWORD:345234
ID: 11, USER_NAME:Jackson, USER_PASSWORD:532342

5.5.2、cursor.fetchone()

cursor.execute("select * from TEST_TABLE")
results =  cursor.fetchone()
print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s" % \
          (results[0],results[1],results[2]))

执行结果,只有一条,并且光标自动下移一位

ID: 1, USER_NAME:Jack Ma, USER_PASSWORD:123123

5.5.3、cursor.fetchmany()

需要传入size参数

cursor2.execute("select * from TEST_TABLE")
results =  cursor2.fetchmany(3)

for result in results:
    print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s" % \
          (result[0],result[1],result[2]))

执行结果:打印往下三条数据

ID: 1, USER_NAME:Jack Ma, USER_PASSWORD:123123
ID: 2, USER_NAME:Small Ma, USER_PASSWORD:312312123
ID: 3, USER_NAME:Jackson, USER_PASSWORD:532342

6、cursor.callproc()

作用:调用mysql中的存储过程

6.1、使用案例

  1. 创建mysql存储过程

    create
        definer = root@localhost procedure insertTEST_TABLE(_USER_NAME varchar(255), _USER_PASSWORD varchar(255))
    BEGIN
        insert into TEST_TABLE(`USER_NAME`,`USER_PASSWORD`) values(_USER_NAME,_USER_PASSWORD);
    end;
    
  2. 调用.callproc()方法

    cursor.callproc("insertTEST_TABLE",("Robert","4443222"))
    
  3. 结果:数据库成功插入数据

6.2、可选参数

调用cursor.callproc()时,必须传入mysql数据库中存储过程名字,args为可选参数

注意:args为一个元祖类型,不可直接传入(见使用案例)

def callproc(self, procname, args=()):
    """Execute stored procedure procname with args
		procname -- string, name of procedure to execute on server
		args -- Sequence of parameters to use with procedure
		Returns the original args.

7、cursor.scroll()

移动cursor光标

7.1、使用案例

cursor.execute("select * from TEST_TABLE")
results =  cursor.fetchmany(3)
for result in results:
    print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s" % \
          (result[0],result[1],result[2]))
    
# 使光标移动两位,后面的mode参数为optional
cursor.scroll(2,"relative")

# 继续向下fetch三位
results = cursor.fetchmany(3)
for result in results:
    print("ID: %s, USER_NAME:%s, USER_PASSWORD:%s" % \
          (result[0],result[1],result[2]))

运行结果:由于移动了两位光标,ID为4,5的数据并没有被展示出来(实际已被查出)

ID: 1, USER_NAME:Jack Ma, USER_PASSWORD:123123
ID: 2, USER_NAME:Small Ma, USER_PASSWORD:312312123
ID: 3, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 6, USER_NAME:Edison, USER_PASSWORD:345234
ID: 7, USER_NAME:Jackson, USER_PASSWORD:532342
ID: 8, USER_NAME:Edison, USER_PASSWORD:345234

7.2、可选参数

必须传入一个value值,通过源码得知,mode默认为relative

  • 如果mode为relative,移动cursor到相对位置
  • 如果mode为absolute,移动cursor到绝对位置
def scroll(self, value, mode='relative'):
  self._check_executed()
        if mode == 'relative':
            r = self.rownumber + value
        elif mode == 'absolute':
            r = value
        else:
            raise err.ProgrammingError("unknown scroll mode %s" % mode)

        if not (0 <= r < len(self._rows)):
            raise IndexError("out of range")
        self.rownumber = r

8、cursor.mogrify()

作用: 转换,把SQL语句和参数相结合,对其中特殊字符进行加\转义,返回一个execute()方法发送到数据库确切执行的字符串,避免注入语句生成。

一句话版本:得到真实获得执行的SQL语句

8.1、使用案例

sql = "select * from TEST_TABLE WHERE ID = %s"
mogrify = cursor.mogrify(sql,args=1)
print(mogrify)

控制台打印

select * from TEST_TABLE WHERE ID = 1

8.2、可选参数

query为必传参数,args默认为空

def mogrify(self, query, args=None):
    """
    Returns the exact string that is sent to the database by calling the
    execute() method.

    This method follows the extension to the DB API 2.0 followed by Psycopg.
    """
    conn = self._get_db()
    if PY2:  # Use bytes on Python 2 always
        query = self._ensure_bytes(query, encoding=conn.encoding)

    if args is not None:
        query = query % self._escape_args(args, conn)

    return query

以上为使用pymysql与数据库交互所需要的主要方法。此外,在db对象中,还有可以获得一些辅助信息,如:

  1. db.autocommit(True):打开事物自动提交
  2. db.affected_rows():获得受影响的行数
  3. db.get_host_info() db.get_server_info():获得host或者server信息

希望本文能够抛砖引玉,如果错误还望各位多多指正

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用Python连接MySQL数据库的基本步骤如下: 1. 安装MySQL驱动程序:可以使用`pip`命令安装`mysql-connector-python`或`pymysql`等驱动程序。 2. 导入MySQL驱动程序:使用`import`语句导入MySQL驱动程序,例如: ```python import mysql.connector ``` 3. 建立数据库连接使用`connect()`函数建立与MySQL数据库连接,指定主机名、用户名、密码、数据库名称等参数。例如: ```python mydb = mysql.connector.connect( host="localhost", user="root", passwd="password", database="mydatabase" ) ``` 4. 执行SQL语句:使用`cursor()`方法创建游标对象,使用`execute()`方法执行SQL语句。例如: ```python mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") ``` 5. 获取查询结果:使用`fetchall()`方法获取查询结果。例如: ```python myresult = mycursor.fetchall() for x in myresult: print(x) ``` 6. 关闭数据库连接使用`close()`方法关闭与MySQL数据库连接。例如: ```python mydb.close() ``` ### 回答2: 使用Python连接MySQL数据库的基本步骤如下: 1. 导入所需模块:首先需要导入`mysql.connector`模块,它是Python连接MySQL数据库的官方模块。 2. 建立连接使用`mysql.connector.connect()`函数连接MySQL数据库,需要提供数据库主机地址、用户名、密码等连接参数。 3. 创建游标对象:使用连接对象的`cursor()`方法创建游标对象,用于执行MySQL语句。 4. 执行SQL语句:通过游标对象的`execute()`方法执行SQL语句,可以执行查询、插入、更新等各种操作。 5. 获取执行结果:通过游标对象的`fetchall()`、`fetchone()`等方法获取执行结果,可以获取所有结果或者一条结果。 6. 提交操作:如果执行了插入、更新等操作,需要调用连接对象的`commit()`方法提交操作,才能真正将数据写入数据库。 7. 关闭连接使用连接对象的`close()`方法关闭连接,释放数据库资源。 下面是一个简单的例子: ``` import mysql.connector # 建立数据库连接 cnx = mysql.connector.connect( host='localhost', user='root', password='password', database='mydb') # 创建游标对象 cursor = cnx.cursor() # 执行查询语句 cursor.execute("SELECT * FROM mytable") # 获取查询结果 result = cursor.fetchall() # 打印查询结果 for row in result: print(row) # 关闭游标和连接 cursor.close() cnx.close() ``` 这是一个简单的连接MySQL数据库的过程,实际应用中根据具体需求可以进行更复杂的操作。 ### 回答3: 使用Python连接MySQL数据库的基本步骤包括以下几个步骤: 1. 安装MySQL驱动:首先需要在Python环境中安装MySQL驱动。常用的驱动有`mysql-connector-python`和`pymysql`,可以通过pip安装。 2. 导入相应模块:在Python程序中,需要导入MySQL驱动所对应的模块。如果使用`mysql-connector-python`,则导入`mysql.connector`;如果使用`pymysql`,则导入`pymysql`。 3. 建立数据库连接使用连接参数,如主机名、用户名、密码和数据库名,来建立与MySQL数据库连接。例如: ```python import mysql.connector cnx = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" ) ``` 4. 创建游标对象:通过数据库连接,创建一个游标对象。游标对象用于执行SQL语句并获取结果。例如: ```python cursor = cnx.cursor() ``` 5. 执行SQL语句:使用游标对象执行SQL语句,可以是查询、插入、更新或删除等操作。例如: ```python sql = "SELECT * FROM tablename" cursor.execute(sql) ``` 6. 处理结果:根据需要,可以通过游标对象获取查询结果、获取受影响的行数或获取插入后的自增ID等信息。例如: ```python result = cursor.fetchall() affected_rows = cursor.rowcount inserted_id = cursor.lastrowid ``` 7. 提交更改和关闭连接:如果有操作对数据库产生了更改,需要调用`commit()`方法提交更改。最后,使用`close()`方法关闭游标和数据库连接。例如: ```python cnx.commit() cursor.close() cnx.close() ``` 以上就是使用Python连接MySQL数据库的基本步骤。通过这些步骤,我们可以在Python程序中与MySQL数据库进行交互,执行各种数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值