zoukankan      html  css  js  c++  java
  • http://www.mysqltutorial.org/python-mysql-query/

    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. Connect to the MySQL Database, 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:

  • 相关阅读:
    svn导出文件夹到另外目录export
    关键词提取自动摘要相关开源项目,自动化seo
    高级前端面试题,部分答案待补充
    三句话感受一本书,让脑子变聪明的7本书,每本只需理解3句话!
    关于系统设置分辨率比例影响到网页宽度大小的问题
    QQ在线客服,默认到要加好友,授权也不起作用需要先开通QQ营销服务
    网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析
    让nodepad++编辑时链接能双击打开
    工作是一种修行,工作本身,就是一种修行(深度好文)
    foxmail收取163企业邮箱设置,不能直接用foxmail默认的配置,否则一直提示帐号密码错误
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/5937320.html
Copyright © 2011-2022 走看看