-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.py
50 lines (35 loc) · 1.03 KB
/
bank.py
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
from sqlalchemy import create_engine
from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker
import os
if os.path.exists("bank.db"):
os.remove("bank.db")
engine = create_engine('sqlite:///bank.db')
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
firstname = Column(String(50), nullable=False)
def __init__(self, firstname):
self.firstname = firstname
class Account(Base):
__tablename__ = 'accounts'
id = Column(Integer, primary_key=True)
user_id = Column(Integer)
type = Column(Boolean, nullable=False)
def amount(self):
return None
# engine = create_engine('mysql+mysqlconnector://test:12345678@localhost/test')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
m1 = User("Sean")
d1 = User("George")
ac = Account()
try:
session.add(m1)
session.add(d1)
session.commit()
except:
session.rollback()