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()
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()
Day 29 of #100DaysOfCode and #Python
— Durga Pokharel (@mathdurga) January 22, 2021
* 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
Top comments (0)