1. 数据库准备至少15个学生的信息,测试数据文件中准备30个学生的ID;[1,2,3,4,。。。。。30]
      针对Student进行查询操作,调用selectStudent方法,采用5个子线程进行处理
      待查询的学生ID存放在列表中,5个子线程各自分配一批学生ID进行查询操作,
      第一个子线程:获取ID%5==1的ID号
      第二个子线程:获取ID%5==2的ID号
      第三个子线程:获取ID%5==3的ID号
      第四个子线程:获取ID%5==4的ID号
      第五个子线程:获取ID%5==0的ID号
      如果查询到相应的结果,将学生ID记录到一个公共的列表中,没有查询到的学生ID记录到另外一个公共的列表中
      屏幕打印最终的查询结果情况。
      # foundList=[] notFoundList=[]_ ```python import threading class DBOperation: @staticmethod def openDB(host, user, pwd, port, db):

      1. import pymysql
      2. dbObj = pymysql.connect(host=host, user=user, password=pwd, port=int(port), db=db, charset='utf8')
      3. curObj = dbObj.cursor() # 生成游标对象
      4. return dbObj, curObj

      @staticmethod def readDB(curObj, sql):

      1. curObj.execute(sql)
      2. result = curObj.fetchall() # 提取查询结果的所有记录
      3. return result

    foundList=[] notFouldList=[] class Student: def init(self): self.dbObj, self.curObj = DBOperation.openDB(‘localhost’, ‘root’, ‘root’, 3306, ‘school’)

    1. def searchStu(self,stuList):
    2. for sid in stuList:
    3. sql='select * from student where sid='+str(sid)
    4. result=DBOperation.readDB(self.curObj,sql)
    5. if len(result)==0:
    6. notFouldList.append(sid)
    7. else:
    8. foundList.append(sid)

    if name == ‘main‘: list1=[] list2=[] list3=[] list4=[] list5=[] for sid in range(1,31): if sid%5==0: list1.append(sid) elif sid%5==1: list2.append(sid) elif sid%5==2: list3.append(sid) elif sid%5==3: list4.append(sid) elif sid%5==4: list5.append(sid) mylist=[list1,list2,list3,list4,list5] print(list1,list2,list3,list4,list5) thList=[] for i in range(5): th = threading.Thread(target=Student().searchStu,args=(mylist[i],)) thList.append(th)

    1. for th in thList:
    2. th.start()
    3. for th in thList:
    4. th.join()
    5. print(foundList, notFouldList)
    1. ```python
    2. import threading
    3. class DBOperation:
    4. @staticmethod
    5. def openDB(host, user, pwd, port, db):
    6. import pymysql
    7. dbObj = pymysql.connect(host=host, user=user, password=pwd, port=int(port), db=db, charset='utf8')
    8. curObj = dbObj.cursor() # 生成游标对象
    9. return dbObj, curObj
    10. @staticmethod
    11. def readDB(curObj, sql):
    12. curObj.execute(sql)
    13. result = curObj.fetchall() # 提取查询结果的所有记录
    14. return result
    15. foundList=[]
    16. notFouldList=[]
    17. class Student:
    18. def __init__(self):
    19. self.dbObj,self.curObj=DBOperation.openDB('localhost','root','root',3306,'school')
    20. def searchStu(self,stuList):
    21. for sid in stuList:
    22. sql='select * from student where sid='+str(sid)
    23. result=DBOperation.readDB(self.curObj,sql)
    24. if len(result)==0:
    25. notFouldList.append(sid)
    26. else:
    27. foundList.append(sid)
    28. if __name__ == '__main__':
    29. list1=[]
    30. list2=[]
    31. list3=[]
    32. list4=[]
    33. list5=[]
    34. for sid in range(1,31):
    35. if sid%5==0:
    36. list1.append(sid)
    37. elif sid%5==1:
    38. list2.append(sid)
    39. elif sid%5==2:
    40. list3.append(sid)
    41. elif sid%5==3:
    42. list4.append(sid)
    43. elif sid%5==4:
    44. list5.append(sid)
    45. print(list1,list2,list3,list4,list5)
    46. th1 = threading.Thread(target=Student().searchStu,args=(list1,))
    47. th2 = threading.Thread(target=Student().searchStu, args=(list2,))
    48. th3 = threading.Thread(target=Student().searchStu, args=(list3,))
    49. th4 = threading.Thread(target=Student().searchStu, args=(list4,))
    50. th5 = threading.Thread(target=Student().searchStu, args=(list5,))
    51. th1.start()
    52. th2.start()
    53. th3.start()
    54. th4.start()
    55. th5.start()
    56. th1.join()
    57. th2.join()
    58. th3.join()
    59. th4.join()
    60. th5.join()
    61. print(foundList, notFouldList)