你的位置:首页 > 数据库

[数据库]http://www.mysqltutorial.org/python


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:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
 
 
def query_with_fetchone():
    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM books")
 
        row = cursor.fetchone()
 
        while row is not None:
            print(row)
            row = cursor.fetchone()
 
    except Error as e:
        print(e)
 
    finally:
        cursor.close()
        conn.close()
 
 
if __name__ == '__main__':
    query_with_fetchone()



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.

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
 
 
def query_with_fetchall():
    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM books")
        rows = cursor.fetchall()
 
        print('Total Row(s):', cursor.rowcount)
        for row in rows:
            print(row)
 
    except Error as e:
        print(e)
 
    finally:
        cursor.close()
        conn.close()
 
 
if __name__ == '__main__':
    query_with_fetchall()



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:

 
1
2
3
4
5
6
7

def iter_row(cursor, size=10):
    while True:
        rows = cursor.fetchmany(size)
        if not rows:
            break
        for row in rows:
            yield row



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

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

def query_with_fetchmany():
    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)
        cursor = conn.cursor()
 
        cursor.execute("SELECT * FROM books")
 
        for row in iter_row(cursor, 10):
            print(row)
 
    except Error as e:
        print(e)
 
    finally:
        cursor.close()
        conn.close()






海岛自助游攻略2015怎样去海岛旅游特价海岛游深圳到海岛旅游报价到海岛旅游要多少钱南宁大明山怎么去?南宁武鸣大明山开车需要多久? 南宁大明山风景区什么时间最好玩?武鸣大明山四季游玩特色? 探春闻香韵 江苏最全赏梅地图出炉 南宁大明山一日游行程安排?武鸣大明山游玩需要多少时间? 长隆万圣节夜场门票价格?广州长隆万圣节刺激吗? 长隆万圣节全日门票价格?广州长隆万圣节门票多少钱? 长隆万圣节门票团购价格?广州长隆万圣节优惠票预订? 长隆欢乐世界万圣节活动介绍?广州长隆万圣节是几号到几号? 欢乐海岸麦鲁小城团购票价?深圳欢乐海岸麦鲁小城好玩不? 呀诺达9月优惠活动?三亚呀诺达中秋节有什么活动? 深圳麦鲁小城玩多久?深圳麦鲁小城儿童体验馆有哪些职业? 龙门温泉瑶族风情园在哪里?瑶族风情园旅游攻略? 义江缘附近有什么好玩的?桂林义江缘旅游攻略? 广州长隆水上乐园时间?长隆水上乐园还有夜场吗? 义江缘有什么项目?桂林义江缘详细介绍? 长隆水上乐园闭园时间?广州长隆水上乐园什么时候闭园? Q65110A7886 Datasheet Q65110A7886 Datasheet Q65110A7887 Datasheet Q65110A7887 Datasheet Q65110A7892 Datasheet Q65110A7892 Datasheet 芳村客运站订票电话 芳村客运站订票电话 芳村客运站订票电话 广州宝墨园好玩吗 广州宝墨园好玩吗 广州宝墨园好玩吗 香港最好的购物中心 香港最好的购物中心 香港最好的购物中心