DEV Community

Durga Pokharel
Durga Pokharel

Posted on

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

Top comments (0)