File size: 1,456 Bytes
adff838
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sqlite3

import streamlit
import pandas as pd

from UserDetail import UserDetail


class Database:
    def __init__(self):
        self.conn = sqlite3.connect('./db.sqlite')
        self.create_table()

    def __del__(self):
        self.conn.close()

    def create_table(self):
        c = self.conn.cursor()
        c.execute('''
            CREATE TABLE IF NOT EXISTS user_detail (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT,
                dob DATE,
                city TEXT
            )
        ''')
        self.conn.commit()
        c.close()

    def insert_user_detail(self, userDetail):
        c = self.conn.cursor()
        c.execute('''
                    INSERT INTO user_detail (name, dob, city) VALUES (?, ?, ?)
                ''', (userDetail.name, userDetail.dob, userDetail.city))
        self.conn.commit()
        user_id = c.lastrowid
        c.close()
        return user_id

    def get_user_detail(self, user_id):
        c = self.conn.cursor()
        c.execute('''
                        SELECT * FROM user_detail WHERE id = ?
                ''', user_id)
        user_detail = self.fetch_data_and_map_to_objects(c)
        c.close()
        return user_detail

    def fetch_data_and_map_to_objects(self, cursor):
        row = cursor.fetchone()

        name = row[1]
        dob = row[2]
        city = row[3]

        obj = UserDetail(name, dob, city)

        return obj