For making everything simpler we will use peewee orm with python sqlite3….
pip install peewee
First create and connect to our database
from peewee import *
# SQLite database setup with Peewee
db = SqliteDatabase('cv_database.db')
class User(Model):
username = CharField(unique=True)
# Add more user details as needed
class Meta:
database = db
class CV(Model):
user = ForeignKeyField(User, backref='cv')
education = TextField()
work_experience = TextField()
skills = TextField()
# Add more fields as needed
class Meta:
database = db
# Connect to the database and create tables if they don't exist
db.connect()
db.create_tables([User, CV], safe=True)
Add and retrieve data from the database
from peewee import *
import random
# SQLite database setup with Peewee
db = SqliteDatabase('cv_database.db')
class User(Model):
username = CharField(unique=True)
class Meta:
database = db
class CV(Model):
user = ForeignKeyField(User, backref='cv')
education = TextField()
work_experience = TextField()
skills = TextField()
class Meta:
database = db
# Connect to the database and create tables if they don't exist
db.connect()
db.create_tables([User, CV], safe=True)
# Function to generate random data for a CV
def generate_cv_data():
education = "Education details here"
work_experience = "Work experience details here"
skills = "Skills details here"
return education, work_experience, skills
# Generate and add data for 100 users
for i in range(1, 101):
username = f"user_{i}"
user = User.create(username=username)
education, work_experience, skills = generate_cv_data()
CV.create(user=user, education=education, work_experience=work_experience, skills=skills)
# Retrieve data for all users
for user in User.select():
print(f"Username: {user.username}")
# Retrieve CV for the current user
cv = CV.get(CV.user == user)
print(f"Education: {cv.education}")
print(f"Work Experience: {cv.work_experience}")
print(f"Skills: {cv.skills}")
print("-------------------")
Fix the issue: If user already exist then update the data
from peewee import *
import random
# SQLite database setup with Peewee
db = SqliteDatabase('cv_database.db')
class User(Model):
username = CharField(unique=True)
class Meta:
database = db
class CV(Model):
user = ForeignKeyField(User, backref='cv')
education = TextField()
work_experience = TextField()
skills = TextField()
class Meta:
database = db
# Connect to the database and create tables if they don't exist
db.connect()
db.create_tables([User, CV], safe=True)
# Function to generate random data for a CV
def generate_cv_data():
education = "Education details here"
work_experience = "Work experience details here"
skills = "Skills details here"
return education, work_experience, skills
# Generate and add data for 100 users
for i in range(1, 101):
username = f"user_{i}"
# Try to retrieve the user. If it doesn't exist, create it.
user, created = User.get_or_create(username=username)
education, work_experience, skills = generate_cv_data()
# If the user already existed, update the CV data
if not created:
cv = CV.get(CV.user == user)
cv.education = education
cv.work_experience = work_experience
cv.skills = skills
cv.save()
else:
CV.create(user=user, education=education, work_experience=work_experience, skills=skills)
# Retrieve data for all users
for user in User.select():
print(f"Username: {user.username}")
# Retrieve CV for the current user
cv = CV.get(CV.user == user)
print(f"Education: {cv.education}")
print(f"Work Experience: {cv.work_experience}")
print(f"Skills: {cv.skills}")
print("-------------------")
Add and remove user And also update user data
from peewee import *
import random
# SQLite database setup with Peewee
db = SqliteDatabase('cv_database.db')
class User(Model):
username = CharField(unique=True)
class Meta:
database = db
class CV(Model):
user = ForeignKeyField(User, backref='cv')
education = TextField()
work_experience = TextField()
skills = TextField()
class Meta:
database = db
# Connect to the database and create tables if they don't exist
db.connect()
db.create_tables([User, CV], safe=True)
# Function to generate random data for a CV
def generate_cv_data():
education = "Education details here"
work_experience = "Work experience details here"
skills = "Skills details here"
return education, work_experience, skills
# Generate and add data for 100 users
for i in range(1, 101):
username = f"user_{i}"
# Try to retrieve the user. If it doesn't exist, create it.
user, created = User.get_or_create(username=username)
education, work_experience, skills = generate_cv_data()
# If the user already existed, update the CV data
if not created:
cv = CV.get(CV.user == user)
cv.education = education
cv.work_experience = work_experience
cv.skills = skills
cv.save()
else:
CV.create(user=user, education=education, work_experience=work_experience, skills=skills)
# Function to add a new user
def add_user(username):
user, created = User.get_or_create(username=username)
if created:
education, work_experience, skills = generate_cv_data()
CV.create(user=user, education=education, work_experience=work_experience, skills=skills)
print(f"User '{username}' added successfully.")
else:
print(f"User '{username}' already exists.")
# Function to delete a user
def delete_user(username):
try:
user = User.get(User.username == username)
user.delete_instance()
print(f"User '{username}' deleted successfully.")
except User.DoesNotExist:
print(f"User '{username}' does not exist.")
# Function to update CV data for a user
def update_cv_data(username, education=None, work_experience=None, skills=None):
try:
user = User.get(User.username == username)
cv = CV.get(CV.user == user)
if education:
cv.education = education
if work_experience:
cv.work_experience = work_experience
if skills:
cv.skills = skills
cv.save()
print(f"CV data updated successfully for user '{username}'.")
except User.DoesNotExist:
print(f"User '{username}' does not exist.")
except CV.DoesNotExist:
print(f"CV data for user '{username}' does not exist.")
# Add a new user
add_user("user_101")
# Delete a user
delete_user("user_101")
# Update CV data for a user
update_cv_data("user_1", education="New education details")