Cybersecurity App

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

import mysql.

connector

from mysql.connector import Error

def create_connection():

"""Create a database connection."""

try:

conn = mysql.connector.connect(

host="localhost",

user="root",

password="root",

database="cybersecurity_app"

if conn.is_connected():

return conn

except Error as e:

print(f"Error: {e}")

return None

def insert_question(cursor, question_text, option_1, option_2, option_3, option_4, correct_answer):

"""Insert a question into the quiz_questions table."""

query = """

INSERT INTO quiz_questions (question_text, option_1, option_2, option_3, option_4,


correct_answer)

VALUES (%s, %s, %s, %s, %s, %s)

"""

cursor.execute(query, (question_text, option_1, option_2, option_3, option_4, correct_answer))

def main():

conn = create_connection()

if conn is None:

print("Failed to create database connection.")

return

cursor = conn.cursor()

try:
# Sample quiz questions

questions = [

("What does HTTPS stand for?", "Hypertext Transfer Protocol Secure",

"Hypertext Transfer Protocol Standard", "Hightext Transfer Protocol Secure",

"Hypertext Transfer Protocol Service", 1),

("Which of the following is a type of malware?", "Firewall",

"Trojan Horse", "Router", "Switch", 2),

("What is the main purpose of a firewall?", "To cool down servers",

"To block unauthorized access", "To speed up the internet",

"To provide antivirus protection", 2),

("What is phishing?", "A method to catch fish",

"A technique to steal sensitive information", "A type of virus",

"A security protocol", 2),

("What does the term 'ransomware' refer to?", "A type of network",

"Malware that encrypts files and demands payment", "A software for protection",

"A system for data backup", 2)

for question in questions:

insert_question(cursor, *question)

conn.commit()

print("Sample questions inserted successfully!")

except Error as e:

print(f"Error inserting questions: {e}")

finally:

cursor.close()

conn.close()

if __name__ == "__main__":

main()

import mysql.connector

from mysql.connector import Error


def create_connection():

"""Create a database connection."""

try:

conn = mysql.connector.connect(

host="localhost",

user="root",

password="root",

database="cybersecurity_app"

if conn.is_connected():

return conn

except Error as e:

print(f"Error: {e}")

return None

def get_questions(cursor):

"""Fetch all quiz questions from the database."""

cursor.execute("SELECT * FROM quiz_questions")

return cursor.fetchall()

def take_quiz(cursor):

"""Conduct the quiz with the user."""

questions = get_questions(cursor)

score = 0

for question in questions:

print(f"\n{question[1]}")

print(f"1. {question[2]}")

print(f"2. {question[3]}")

print(f"3. {question[4]}")

print(f"4. {question[5]}")

try:

answer = int(input("Your answer (1-4): "))


if answer == question[5]:

score += 1

except ValueError:

print("Invalid input. Please enter a number between 1 and 4.")

print(f"\nQuiz completed! Your score is {score} out of {len(questions)}.")

def main():

conn = create_connection()

if conn is None:

print("Failed to create database connection.")

return

cursor = conn.cursor()

try:

take_quiz(cursor)

finally:

cursor.close()

conn.close()

if __name__ == "__main__":

main()

You might also like