DEV Community

Durga Pokharel
Durga Pokharel

Posted on

4 2

Day 29 Of 100DayOfCode: Made a Dummy Database For School

Today I continue to learn database on python from Coursera.
Try to write dummy database for school and put some student data and teacher data in respective table from python.

Dummy Database

Code to made student table is given below. My code start with importing sqlite3. At first create a table having name Student. If that name table already exist drop it out. Rest of the code is given below.

import sqlite3

conn = sqlite3.connect('school.sqlite')
cur = conn.cursor()



cur.executescript('''
DROP TABLE IF EXISTS Student;

CREATE TABLE Student (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name TEXT,
    dob TEXT,
    gender TEXT
);

''')

lines = open("student_data.txt").read().splitlines()
for data in lines:
    data= data.split(",")
    print(data)
    cur.execute('''INSERT OR IGNORE INTO Student (name, dob, gender) 
        VALUES (?,?,? )''',(data[0], data[2], data[3]))

    conn.commit()
Enter fullscreen mode Exit fullscreen mode

As in student table same procedure were followed to make teacher table.

cur.executescript('''
DROP TABLE IF EXISTS Teacher;

CREATE TABLE Teacher (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name TEXT,
    dob TEXT,
    gender TEXT
);
''')
lines=open("teacher.txt").read().splitlines()
for data in lines:
    data = data.split(",")

    cur.execute('''INSERT OR IGNORE INTO Teacher (name, dob, gender) 
        VALUES (?,?,? )''',(data[0], data[1], data[2]))

    conn.commit()
Enter fullscreen mode Exit fullscreen mode

Day 29 of #100DaysOfCode and #Python
* Worked More On Database On Python
* Made a Dummy Database For School And Put Some Student Data And Teacher Data In Respective Table From Python. pic.twitter.com/RtVUyd9pAZ

— Durga Pokharel (@mathdurga) January 22, 2021

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay