-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_postgres.py
77 lines (58 loc) · 2.21 KB
/
query_postgres.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
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
# content of this file will come from the app.py
# https://stackoverflow.com/questions/8551952/how-to-get-last-record
# https://stackoverflow.com/questions/2633218/how-can-i-select-all-rows-with-sqlalchemy
# https://www.cloudbees.com/blog/unleash-the-power-of-storing-json-in-postgres/
from sqlalchemy import create_engine, inspect
from sqlalchemy import Column, String, Integer, DateTime, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from sqlalchemy.dialects.postgresql import JSON, JSONB
# get the ip address of the connection
import socket
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
db_string = "postgres://testdb:testdb@"+str(get_ip_address())+":5432/flask_viz"
db = create_engine(db_string)
base = declarative_base()
class DB_Class(base):
__tablename__ = 'flask_viz_table'
id = Column(Integer, primary_key=True)
json_column = Column(JSONB)
Session = sessionmaker(db)
session = Session()
# base.metadata.create_all(db)
def query():
# film_1 = session.query(DB_Class).first() # first entry query
film_1 = session.query(DB_Class).order_by(DB_Class.id.desc()).first() # last entry query
print(film_1)
print("latest_id: ", film_1.id)
print(film_1.json_column)
print(type(film_1.json_column))
# a = film_1.json_column
# print(a["Board_1"]["value_00"]["01_add"])
# print(type(a["Board_1"]["value_00"]["01_add"]))
query()
def query_all():
film_1 = session.query(DB_Class).all()
for x in film_1:
print(x.id)
query_all()
def query_id():
film_1 = session.query(DB_Class).filter_by(id="129").first()
print(film_1.id)
print(film_1.json_column)
print("hallo----------")
query_id()
# way to query all
# for class_instance in session.query(DB_Class).all():
# print(vars(class_instance))
# min_id = session.query(func.min(DB_Class.id)).scalar()
# print("before del: ",min_id)
# session.query(DB_Class).filter(DB_Class.id==min_id).delete()
# session.commit()
# print("after del: ",session.query(func.min(DB_Class.id)).scalar())
# rows = session.query(DB_Class).count()
# print("rows: ", rows)