-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
56 lines (40 loc) · 1.49 KB
/
database.py
File metadata and controls
56 lines (40 loc) · 1.49 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
from sqlalchemy import MetaData, Table, Column, Text, Integer, DateTime
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy as db
from config import DATABASE_CONNECTION_URI
import json
from datetime import datetime
engine = db.create_engine(DATABASE_CONNECTION_URI)
class Database():
def __init__(self):
self.connection = engine.connect()
self.Session = sessionmaker(engine)
self.session = self.Session()
def insertSolutions(self, n, solutions, boards):
serial_boards = json.dumps(boards)
timestamp = datetime.now()
solution = Solutions(N=n, date=timestamp,
sols=solutions, boards=serial_boards)
self.session.add(solution)
self.session.commit()
print('Solution Saved Successfully')
Base = declarative_base()
class Solutions(Base):
"""Model for saving solutions"""
__tablename__ = "solutions"
id = Column(Integer, primary_key=True)
date = Column(DateTime)
N = Column(Integer)
sols = Column(Integer)
boards = Column(Text)
def __repr__(self):
return "<Solutions(N='%d' = sols='%d'. Boards: '%s'>" % (self.N,
self.sols,
self.boards)
Base.metadata.create_all(engine)
def main():
Database()
if __name__ == "__main__":
# execute only if run as a script
main()