· 6 years ago · Dec 28, 2019, 12:58 PM
1# Databases
2# Importing the modules we need:
3import sqlite3
4# For accounts:
5# Creating a connection object which represents the database:
6database = sqlite3.connect("DataBase.db")
7# Creating a cursor which will perform the commands:
8c = database.cursor()
9# We create a table:
10c.execute('''CREATE TABLE IF NOT EXISTS accounts ( username, password ) ''')
11# For messages:
12c.execute('''CREATE TABLE IF NOT EXISTS messages (account, person, messages)''')
13# __Classes__
14
15
16class Account:
17 def __init__(self, username, password, persons):
18 self.username = username
19 self.password = password
20 self.persons = persons
21
22
23class Messages:
24 def __init__(self, account, person, messages):
25 self.account = account
26 self.person = person
27 self.messages = messages
28# __Functions__
29
30
31def covertdata(data):
32 result = ''.join(data)
33 return result
34
35
36def createaccount():
37 username = input("Please enter a username: ")
38 password = input("Please enter a password: ")
39 c.execute("SELECT username FROM accounts WHERE username= '%s'" % username)
40 founduser = c.fetchone()
41 if founduser is None:
42 c.execute("INSERT INTO accounts VALUES ( '%s' , '%s') " % (username, password))
43 database.commit()
44 else:
45 print("Username already exists!")
46 createaccount()
47
48
49def login():
50 user = input("Please enter your username: ")
51
52 c.execute("SELECT username FROM accounts WHERE username='%s'" % user)
53 founduser = c.fetchone()
54 if founduser is None:
55 print("Username unknown!")
56 login()
57 result = covertdata(founduser)
58 if result == user:
59 passw = input("Please enter your password: ")
60 c.execute("SELECT password FROM accounts WHERE username='%s'" % user)
61 foundpassw = c.fetchone()
62 result = covertdata(foundpassw)
63 if passw == result:
64 print("Succesfully connected!")
65 print("========================")
66 c.execute("SELECT person FROM messages WHERE account = '%s'" % user)
67 for person in c.fetchall():
68 result = covertdata(person)
69 print(result)
70 else:
71 print("Wrong password!")
72 login()
73
74
75def begin():
76 while True:
77 print("====================")
78 command = input("Login(1)Sign Up(2): ")
79 if command == "1":
80 login()
81 elif command == "2":
82 createaccount()
83 else:
84 print("Unknown command!")
85 begin()
86
87
88begin()