我已将问题保存在MySQL数据库中。我想随机抽取这些问题。我如何使用Python来完成这个任务呢?
以下是我迄今所做的工作。
代码:
def get_questions():
db = MySQLdb.connect(host="localhost",
user="root",
passwd="",
db="questionsbank")
cur = db.cursor()
preferred_area = get_preferred_area()
if preferred_area == 'Java':
cur.execute("SELECT question from java_questions")
for row in cur.fetchall():
print row[0]发布于 2017-02-21 11:32:27
您可以使用随机模块,遵循这个链接,https://docs.python.org/2/library/random.html,cur.fetchall,返回一个带有随机模块的列表,您可以使用选择功能。
https://stackoverflow.com/questions/42366055
复制相似问题