Python PyMySQL:查询数据


#Python PyMySQL#


简介

执行 select 后,可以通过 cursor.fetchone、cursor.fetchmany、cursor.fetchall 获取数据。注意,查询结果中的每一条数据只能被fetch一次,被取出后。下一次 fetch 就拿不到数据了。

示例

建表和数据准备

在 blog 库下建表:

create table `user_info` (
    `id` bigint unsigned not null auto_increment,
    `name` varchar(45) not null default '',
    primary key (`id`)
) engine = InnoDB default charset = utf8mb4;

插入数据,数据内容如下:

mysql> select * from user_info;
+----+---------+
| id | name    |
+----+---------+
|  2 | name001 |
|  3 | name002 |
|  4 | name003 |
|  5 | name004 |
|  6 | name005 |
|  7 | name006 |
+----+---------+
6 rows in set (0.01 sec)

指定 id 查询

import pymysql

conn = pymysql.connect(host='127.0.0.1',
                       user='root',
                       password='123456',
                       port=3306,
                       database='blog',
                       autocommit=True
                       )
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
affect_rows = cursor.execute('select id, name from user_info where id=%s', [2])

print('影响行数: ', affect_rows)

print('fetchone 结果: ', cursor.fetchone())
print('fetchall 结果: ', cursor.fetchall())

cursor.close()
conn.close()

运行结果示例:

影响行数:  1
fetchone 结果:  {'id': 2, 'name': 'name001'}
fetchall 结果:  []

可以看到 fetchall 的时候没有数据。

如果将 fetch 的顺序调整成:

print('fetchall 结果: ', cursor.fetchall())
print('fetchone 结果: ', cursor.fetchone())

运行结果是:

fetchall 结果:  [{'id': 2, 'name': 'name001'}]
fetchone 结果:  None

根据 id 范围查询

import pymysql

conn = pymysql.connect(host='127.0.0.1',
                       user='root',
                       password='123456',
                       port=3306,
                       database='blog',
                       autocommit=True
                       )
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
affect_rows = cursor.execute('select id, name from user_info where id>%s', [2])

print('影响行数: ', affect_rows)

print('fetchall 结果: ', cursor.fetchall())
print('fetchone 结果: ', cursor.fetchone())

cursor.close()
conn.close()

运行结果示例:

影响行数:  5
fetchall 结果:  [{'id': 3, 'name': 'name002'}, {'id': 4, 'name': 'name003'}, {'id': 5, 'name': 'name004'}, {'id': 6, 'name': 'name005'}, {'id': 7, 'name': 'name006'}]
fetchone 结果:  None

如果只用fetchone 不停的取数据:

import pymysql

conn = pymysql.connect(host='127.0.0.1',
                       user='root',
                       password='123456',
                       port=3306,
                       database='blog',
                       autocommit=True
                       )
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
affect_rows = cursor.execute('select id, name from user_info where id>%s', [2])

print('影响行数: ', affect_rows)

print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())
print('fetchone 结果: ', cursor.fetchone())

cursor.close()
conn.close()

运行结果如下:

影响行数:  5
fetchone 结果:  {'id': 3, 'name': 'name002'}
fetchone 结果:  {'id': 4, 'name': 'name003'}
fetchone 结果:  {'id': 5, 'name': 'name004'}
fetchone 结果:  {'id': 6, 'name': 'name005'}
fetchone 结果:  {'id': 7, 'name': 'name006'}
fetchone 结果:  None
fetchone 结果:  None

当 fetchone 取不到数据时会返回 None,当 fetchall 取不到数据时会返回空list。


( 本文完 )