mardi 21 février 2017

Randomly get strings from a database using python

I have saved questions in a MySQL database. I want to fetch those questions randomly. How I can do this using Python?

Following is the what I have done so far.

Code:

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]




Aucun commentaire:

Enregistrer un commentaire