-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_db.py
More file actions
executable file
·119 lines (95 loc) · 3.85 KB
/
sql_db.py
File metadata and controls
executable file
·119 lines (95 loc) · 3.85 KB
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
'''Create db and tables'''
import datetime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import (
create_engine,
Column,
Integer,
String,
DateTime,
ForeignKey
)
def connect_to_db(t_engine, n_db, login='root', password='', server='localhost'):
'''t_engine = type of database, n_db = name of database'''
db_name = n_db
if t_engine == 'sqlite':
db = create_engine('sqlite:///{0}.db'.format(db_name))
return db
elif t_engine == 'mysql':
db = create_engine('mysql://{0}:{1}@{2}'.format(login, password, server))
db.execute('CREATE DATABASE IF NOT EXISTS {0}'.format(db_name))
db.execute('USE {0}'.format(db_name))
return db
else:
print('wrong engine type!!!!')
quit()
# construct a base class for declarative class definitions
Base = declarative_base()
class Suppliers(Base):
'''Create suppliers table'''
__tablename__ = 'Suppliers'
SupplierID = Column(Integer, primary_key=True, unique=True)
CompanyName = Column(String(250))
ContactName = Column(String(120))
Adress = Column(String(250))
Phone = Column(String(250))
Country = Column(String(100))
def __repr_(self):
'''it`s how we represent table'''
return """id: '{0} CompanyName: '{1}' ContactName: '{2}'
Adress: '{3}' Phone: '{4}'Country: '{5}'""".format(
self.SupplierID, self.CompanyName, self.ContactName,
self.Adress, self.Phone, self.Country)
class Orders(Base):
'''Create orders table'''
__tablename__ = 'Orders'
OrderID = Column(Integer, primary_key=True)
CustomerID = Column(Integer)
OrderDate = Column(DateTime, default=datetime.datetime.utcnow)
ShippedDate = Column(DateTime, default=datetime.datetime.utcnow)
ShipName = Column(String(120))
ShipAdress = Column(String(250))
def __repr__(self):
return """id: '{0}' CustomerID: '{1}' OrderDate: '{2}'
ShippedDate: '{3}' ShipeName: '{4}' ShipAdress: '{5}'\n""".format(
self.OrderDate, self.CustomerID, self.OrderDate,
self.ShippedDate, self.ShipName, self.ShipAdress
)
class Categories(Base):
'''Create categories table'''
__tablename__ = 'Categories'
CategoryID = Column(Integer, primary_key=True, unique=True)
CategoryName = Column(String(80))
Description = Column(String(500))
def __repr___(self):
return """id: '{0}' CategoryName: '{1}' Description: '{2}'\n""".format(
self.CategoryID, self.CategoryName, self.Description
)
class Products(Base):
'''Create products table'''
__tablename__ = 'Products'
ProductID = Column(Integer, primary_key=True, unique=True)
ProductName = Column(String(100))
SupplierID = Column(Integer, ForeignKey(Suppliers.SupplierID))
CategoryID = Column(Integer, ForeignKey(Categories.CategoryID))
UnitPrice = Column(Integer)
def __repr_(self):
return """id: '{0}' ProductName: '{1}' SupplierID: '{2}' CategoryID: '{2}'
UnitPrice: '{3}'\n""".format(self.ProductID, self.ProductName, self.SupplierID,
self.CategoryID, self.UnitPrice
)
class OrderDetail(Base):
'''Create OderDetail table'''
__tablename__ = 'OrderDetails'
OrderDetID = Column(Integer, primary_key=True, unique=True)
ProductID = Column(ForeignKey('Products.ProductID'))
UnitPrice = Column(Integer)
Quantity = Column(Integer)
Discount = Column(Integer)
prod = relationship('Products', backref='Products')
def __repr_(self):
return """id: '{0}' ProductID: '{1}' UnitPrice: '{2}' Quantity: '{3}'
Discount: '{4}'\n""".format(self.OrderDetID, self.ProductID, self.UnitPrice,
self.Quantity, self.Discount
)