skip to Main Content

I am making a web application using HTML, python and a MYSQL database. I currently have two tables: one that stores users and the other that stores flashcards.

I want to add the feature of ‘practising’ flashcards by displaying a random flashcard from the database one at a time but am unsure how I could approach this.

Here is my flashcards table.

At first, I was thinking that I could generate a random number and display the card with the respective flashcard ID, but the ‘FlashID’ doesn’t reset per user.

SQLController.execute(f"SELECT count(*) FROM flashcards WHERE UserID = '{UserID}'")  
    MaxQuestionsList=(list(SQLController))
    MaxQuestions = str(MaxQuestionsList[0])
    

    RandCard = randint(1,int(MaxQuestions[1]))

    SQLController.execute(f"SELECT Question, Answer FROM flashcarcs WHERE FlashID = '{RandCard}'")

2

Answers


  1. I don’t know Python, but couldn’t you just do a random function on a primary key from your sql database? Like an id of some sort.

    You could do SELECT * FROM flashcards WHERE UserID= ‘{UserId]’

    then do like a randint based on the ID of the SQL Row.

    Login or Signup to reply.
  2.         import random
    
            UserID = 'your_user_id'
    
            SQLController.execute(f"SELECT count(*) FROM flashcards WHERE UserID = '{UserID}'")
            MaxQuestionsList = list(SQLController)
            MaxQuestions = MaxQuestionsList[0][0]  # Get the count of flashcards as an integer
    
            if MaxQuestions > 0:
    RandCard = random.randint(1, MaxQuestions)
    
    SQLController.execute(f"SELECT Question, Answer FROM flashcards WHERE UserID = '{UserID}' AND FlashID = {RandCard}")
    flashcard_data = SQLController.fetchone()
    
    if flashcard_data:
        question, answer = flashcard_data
        print(f"Question: {question}")
        print(f"Answer: {answer}")
    else:
        print("No flashcard found for the given random number.")
            else:
    print("No flashcards found for the user.")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search