代码如下:
fetchone
from DrugRelationshipData.setting.DBsetting import *
# 查找数据库中某个值
class SeletMysqlData:
def select_data(self,sql):
cursor.execute(sql) #需要运行的sql
while 1: # 全表循环执行
try:
re = cursor.fetchone() # 一次查询一条数据结果
# re = cursor.fetchall()
if re is None:
break
return re
except:
print("获取数据报错")
break
if __name__ == "__main__":
selectDrugstoreIDsql = "SELECT id,pharmacy_number FROM drugstore WHERE `category` = '%s';"
selectDrugstoreIDData = ("1")
sql = (selectDrugstoreIDsql % selectDrugstoreIDData)
drugstore_id = SeletMysqlData().select_data(sql)
print(drugstore_id)
运行结果:
(5, 'CAYD')
fetchall
from DrugRelationshipData.setting.DBsetting import *
# 查找数据库中某个值
class SeletMysqlData:
def select_data(self,sql):
cursor.execute(sql) #需要运行的sql
while 1: # 全表循环执行
try:
# re = cursor.fetchone() # 查询一条数据结果
re = cursor.fetchall() # 展示全表查询的多条数据结果
if re is None:
break
return re
except:
print("获取数据报错")
break
if __name__ == "__main__":
selectDrugstoreIDsql = "SELECT id,pharmacy_number FROM drugstore WHERE `category` = '%s';"
selectDrugstoreIDData = ("1")
sql = (selectDrugstoreIDsql % selectDrugstoreIDData)
drugstore_id = SeletMysqlData().select_data(sql)
print(drugstore_id)
运行结果:
((5, 'CAYD'), (9, 'SMYY'), (10, 'CSYD'))
数据库查询结果:
总结:
cursor.fetchone():只展示最前面的一条数据,展示一个元祖
cursor.fetchall():展示全表所有查询结果数据,展示多维元祖(嵌套元祖)