fetchone、fetchall 亦凉 2023-07-05 14:57 42阅读 0赞 **fetchone(): 该方法获取下一个查询结果集。结果集是一个对象,读取一行结果,读取完指向下一行,到空为止 fetchall():接收全部的返回结果行,到空为止 fetchone() : 返回单个的元组,也就是一条记录(row),如果没有结果 则返回 None fetchall() : 返回多个元组,即返回多个记录(rows),如果没有结果 则返回 () 需要注明:在MySQL中是NULL,而在Python中则是None** 举例: #### ①fetchone #### import pymysql do = pymysql.connect("localhost","root","testdb","test",charset='utf8') cursor = do.cursor() cursor.execute("show tables") print(cursor.fetchone()) do.close() #返回一维元组(id,title) ('part',) #### ②fetchall: #### import pymysql do = pymysql.connect("localhost","root","testdb","test",charset='utf8') cursor = do.cursor() cursor.execute("show tables") print(cursor.fetchall()) do.close() #显示的是二维元组((id,title),title) (('part',), ('project',), ('spj',), ('student',), ('supp',), ('teacher',), ('testdb_1',), ('user',)) 划重点:fentchone和fetchall只会从没有读取的数据的顶 行接着读取数据 #### 举例: #### 测试一 #导入库 import pymysql do = pymysql.connect("localhost","root","testdb","test",charset='utf8') cursor = do.cursor() cursor.execute("show tables") print(cursor.fetchone()) print(cursor.fetchone()) do.close() ('part',) ('project',) 测试二 import pymysql do = pymysql.connect("localhost","root","testdb","test",charset='utf8') cursor = do.cursor() cursor.execute("show tables") print(cursor.fetchone()) print(cursor.fetchone()) do.close() ![在这里插入图片描述][20200216005116835.png] [20200216005116835.png]: https://img-blog.csdnimg.cn/20200216005116835.png
还没有评论,来说两句吧...