File size: 4,805 Bytes
66ec538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from colorama import Fore, Back, Style, init

init()

#conn = sqlite3.connect('Mydatabase.db')

def CreateDatabse(conn):
    cursor = conn.cursor()
    try:
        
        cursor.execute('''

            CREATE TABLE Friends (

                Userid TEXT PRIMARY KEY NOT NULL,

                Fullname TEXT NOT NULL,

                Role TEXT NOT NULL

            )

        ''')
        cursor.execute('''

            CREATE TABLE Cards (

                Cardid INTEGER PRIMARY KEY AUTOINCREMENT,

                fk_Userid TEXT NOT NULL,

                Description TEXT NOT NULL,

                FOREIGN KEY(fk_Userid) REFERENCES Friends(Userid)

            )

        ''')
        CommitAndClose(conn)
        print(Style.BRIGHT + Fore.GREEN + "Tables created successfully")
        
    except Exception as e:
        print(Style.BRIGHT + Fore.RED + "Error creating tables")
        print(Style.BRIGHT+ Fore.RESET +str(e))


def InsertUser(conn,userid,fullname,role):
    cursor = conn.cursor()
    try:
        cursor.execute("""

            INSERT INTO Friends

                (Userid, Fullname, Role)

            VALUES (?,?,?)""",
            (userid,fullname,role))
        print(Style.BRIGHT + Fore.GREEN + "User inserted successfully")
    except Exception as e:
        print(Style.BRIGHT + Fore.RED + "Error inserting data")
        print(Style.BRIGHT+ Fore.RESET +str(e))
    CommitAndClose(conn)

def InsertCard(conn,userid,description):
    cursor = conn.cursor()
    try:
        cursor.execute("""

            INSERT INTO cards

                (fk_Userid, Description)

            VALUES (?,?)""",
            (userid,description))
        print(Style.BRIGHT + Fore.GREEN + "Card inserted successfully")
    except Exception as e:
        print(Style.BRIGHT + Fore.RED + "Error inserting data")
        print(Style.BRIGHT+ Fore.RESET +str(e))
    CommitAndClose(conn)
   
def FetchAllUsers(conn):
    cursor = conn.cursor()
    rows = cursor.execute('SELECT * FROM Friends').fetchall()
    print(Style.BRIGHT + Fore.GREEN + "Users fetched successfully")   
    FormatedRows = [{"Userid":row[0],"Fullname":row[1],"Role":row[2]} for row in rows]
    CommitAndClose(conn)
    return  FormatedRows

def FetchAllCards(conn):
    cursor = conn.cursor()
    rows = cursor.execute('SELECT * FROM Cards').fetchall()
    print(Style.BRIGHT + Fore.GREEN + "Cards fetched successfully"+Style.RESET_ALL)  
    FormatedRows = [{"Cardid":row[0],"Userid":row[1],"Description":row[2]} for row in rows]
    
    for row in FormatedRows:
        #print(row)
        userid = row['Userid']
        userinfo =  cursor.execute('SELECT * FROM Friends Where Userid = ?', (userid,)).fetchone()
        #print(userinfo)
        row["Userinfo"] = {"FullName":userinfo[1],"Role":userinfo[2]}
    CommitAndClose(conn)
    return  FormatedRows

def FetchUser(conn,userid):
    cursor = conn.cursor()
    row = cursor.execute('SELECT * FROM Friends Where Userid = ?', (userid,)).fetchone()
    print(Style.BRIGHT + Fore.GREEN + f"{userid} fetched successfully")   
    CommitAndClose(conn)
    return  row

def FetchCard(conn,cardid):
    cursor = conn.cursor()
    row = cursor.execute('SELECT * FROM Cards Where Cardid = ?', (cardid,)).fetchone()
    print(Style.BRIGHT + Fore.GREEN + f"{cardid} fetched successfully")   
    CommitAndClose(conn)
    return  row
        
def UpdateCard(conn,cardid,description):    
    cursor = conn.cursor()
    try:
        cursor.execute("""

            UPDATE cards

            SET Description = ?

            WHERE Cardid = ?""",
            (description,cardid))
        print(Style.BRIGHT + Fore.GREEN + "Card updated successfully")
    except Exception as e:
        print(Style.BRIGHT + Fore.RED + "Error updating data")
        print(Style.BRIGHT+ Fore.RESET +str(e))
    CommitAndClose(conn)
        
         
def CommitAndClose(conn):
    conn.commit() 
    conn.close()
    print(Style.BRIGHT + Fore.GREEN + "Database closed successfully" + Style.RESET_ALL)
    
#CreateDatabse()
"""InsertUser(conn,"user01","John Doe","Frontend Developer")

InsertUser(conn,"user02","Jane Doe","Backend Developer")

InsertUser(conn,"user03","Jack Doe","Fullstack Developer")

InsertUser(conn,"user04","Jill Doe","Devops Developer")

InsertCard(conn,"user01","Noureddine is a Fullstack Developer")

InsertCard(conn,"user02","Best one")

InsertCard(conn,"user03","Awesome")

InsertCard(conn,"user04","Good and rich profile")

InsertCard(conn,"user04","Good and Strong background")"""

#print(Style.RESET_ALL+str(FetchCard(conn,1)))
#print(Style.RESET_ALL+str(FetchUser(conn,"user01")))
#CommitAndClose(conn)