To build a Cybersecurity Awareness App with Python and SQL connectivity, you’ll need a few essential components:
1. Database Setup: This stores user information, quiz questions, scores, and learning modules.
2. Python-SQL Connectivity: To handle data operations such as fetching quiz questions, saving user progress, and
storing scores.
3. App Interface: This can be built with a simple text-based interface or using GUI libraries like Tkinter.
Below is an outline of the basic code structure to get started:
1. Setting up SQL Database
Assume we're using MySQL. First, install the required package:
pip install mysql-connector-python
Then, in MySQL, create a database and tables:
CREATE DATABASE cybersecurity_app;
USE cybersecurity_app;
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
score INT DEFAULT 0
);
CREATE TABLE quiz_questions (
question_id INT PRIMARY KEY AUTO_INCREMENT,
question TEXT NOT NULL,
option1 VARCHAR(100),
option2 VARCHAR(100),
option3 VARCHAR(100),
option4 VARCHAR(100),
correct_option INT
);
2. Connecting Python to SQL Database
The following Python code connects to the database and interacts with it.
import mysql.connector
# Establishing the connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="cybersecurity_app"
cursor = conn.cursor()
3. Functions for the App
Here are a few core functions for managing users, fetching quiz questions, and saving scores.
Registering a New User
def register_user(username):
query = "INSERT INTO users (username) VALUES (%s)"
cursor.execute(query, (username,))
conn.commit()
print(f"User {username} registered successfully!")
Fetching Quiz Questions
def get_questions():
cursor.execute("SELECT * FROM quiz_questions")
questions = cursor.fetchall()
return questions
Taking the Quiz
def take_quiz(username):
questions = get_questions()
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]}")
answer = int(input("Your answer (1-4): "))
if answer == question[6]:
score += 1
# Update the score in the database
query = "UPDATE users SET score = %s WHERE username = %s"
cursor.execute(query, (score, username))
conn.commit()
print(f"\nQuiz completed! {username}, your score is {score}.")
Displaying User Scores
def display_scores():
cursor.execute("SELECT username, score FROM users ORDER BY score DESC")
results = cursor.fetchall()
print("\nUser Scores:")
for user in results:
print(f"Username: {user[0]}, Score: {user[1]}")
4. Main Program
Here’s the main function that puts it all together:
def main():
while True:
print("\n--- Cybersecurity Awareness App ---")
print("1. Register")
print("2. Take Quiz")
print("3. View Scores")
print("4. Exit")
choice = input("Choose an option: ")
if choice == "1":
username = input("Enter a username: ")
register_user(username)
elif choice == "2":
username = input("Enter your username: ")
take_quiz(username)
elif choice == "3":
display_scores()
elif choice == "4":
print("Exiting the app.")
break
else:
print("Invalid choice. Please try again.")
# Closing the connection
cursor.close()
conn.close()
# Run the app
main()
5. Testing and Enhancements
This is a basic framework, but you can add enhancements like:
GUI Interface using Tkinter or PyQt for a more user-friendly experience.
More Interactive Content: Add educational modules for cybersecurity tips before each quiz.
Enhanced Quiz: Include more complex question types, timed quizzes, and feedback on answers.
This setup provides a foundation for building and expanding a cybersecurity awareness app for students or users
interested in learning basic security practices.