博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
http://www.mysqltutorial.org/python-mysql-query/
阅读量:6889 次
发布时间:2019-06-27

本文共 4102 字,大约阅读时间需要 13 分钟。

This tutorial shows you how to query data from a MySQL database in Python by using MySQL Connector/Python API such as fetchone() , fetchmany() , and fetchall() .

To query data in a MySQL database from Python, you need to do the following steps:

  1. , you get a MySQLConnection object.
  2. Instantiate a  MySQLCursor object from the the MySQLConnection object.
  3. Use the cursor to execute a query by calling its  execute() method.
  4. Use fetchone() ,  fetchmany() or  fetchall() method to fetch data from the result set.
  5. Close the cursor as well as the database connection by calling the  close() method of the corresponding object.

We will show you how to use fetchone() , fetchmany() , and  fetchall() methods in more detail in the following sections.

Querying data with fetchone

The  fetchone() method returns the next row of a query result set or None in case there is no row left. Let’s take a look at the following code:

Let’s examine the code in detail:

  1. First, we connected to the database by create a new  MySQLConnection object
  2. Second, from the  MySQLConnection object, we instantiated a new  MySQLCursor object
  3. Third, we executed a query that selects all rows from the books table.
  4. Fourth, we called  fetchone() method to fetch the next row in the result set. In the  while loop block, we printed out the content of the row and move to the next row until all rows are fetched.
  5. Fifth, we closed both cursor and connection objects by invoking the  close() method of the corresponding object.

Querying data with fetchall

 In case the number of rows in the table is small, you can use the  fetchall() method to fetch all rows from the database table.  See the following code.

 

The logic is similar to the example with the  fetchone() method except for the  fetchall()method call part. Because we fetched all rows from the books table into the memory, we can get the total rows returned by using the  rowcount property of the cursor object.

Querying data with fetchmany

For a relatively big table, it takes time to fetch all rows and return the result set. In addition, fetchall() needs to allocate enough memory to store the entire result set in the memory. This is inefficient and not a good practice.

MySQL Connector/Python provides us with the  fetchmany() method that returns the next number of rows (n) of the result set, which allows us to balance between time and memory space. Let’s take a look at how do we use  fetchmany() method.

First, we develop a generator that chunks the database calls into a series of  fetchmany() calls as follows:

Second, we can use the  iter_row() generator to fetch 10 rows at a time as shown below:

转载地址:http://vgqbl.baihongyu.com/

你可能感兴趣的文章
C语言 文件操作| 文件打开
查看>>
高并发学习(一)高并发的问题/线程安全性/锁/可见性/有序性
查看>>
多重catch语句
查看>>
java对cookie的操作_01
查看>>
支付宝移动支付
查看>>
恋家-bs4
查看>>
服务治理利器Hystrix-理论篇
查看>>
Java 工厂方法模式
查看>>
coursera上的软件安全课程的课后阅读补充
查看>>
通过XMLHttpRequest和jQuery实现ajax的几种方式
查看>>
Why do I get the error "The target GatherAllFilesToPublish does not exist"?
查看>>
根据rowid删除最新数据(rowid最大为最新数据)(转)
查看>>
进程和线程的概念,区别(理论知识)
查看>>
Android eclipse环境搭建
查看>>
platform平台总线
查看>>
JS_小教程
查看>>
Could not load file or assembly 'Microsoft.AnalysisServices.SharePoint.Integration'
查看>>
Delphi中Chrome Chromium、Cef3学习笔记(六)
查看>>
oracle 11g RAC 安装前准备脚本
查看>>
【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之一
查看>>