-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_test.py
65 lines (51 loc) · 1.83 KB
/
json_test.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
import psycopg2
import json
def main():
with psycopg2.connect("host=localhost port=15432 "
"dbname=mydb user=postgres password=postgres") as conn:
#insertByObject(conn)
#insertByText(conn)
#select(conn)
insertByAlchemy()
def select(conn):
""" json列の取得 """
with conn.cursor() as cur:
# sqlの実行
cur.execute("SELECT * FROM json_test")
for row in cur.fetchall():
print(row[0], row[1], type(row[0]))
def insertByObject(conn):
""" libpqのJSONオブジェクトでのINSERT """
from psycopg2.extras import Json
with conn.cursor() as cur:
#json objectで設定
json_dict = {"a":30, "b": "extras.Json"}
cur.execute("INSERT INTO json_test(info) VALUES(%s)", [Json(json_dict)])
conn.commit();
def insertByText(conn):
""" SQL文中でキャスト """
with conn.cursor() as cur:
#textで設定
json_dict = {"a":40, "b": "json dumps"}
cur.execute("INSERT INTO json_test(info) VALUES(jsonb(%s))", [json.dumps(json_dict)])
conn.commit();
def insertByAlchemy():
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import sessionmaker
engine = create_engine('postgresql+psycopg2://postgres:postgres@localhost:15432/mydb')
Base = declarative_base()
class JsonTest(Base):
__tablename__ = 'json_test'
id = Column(Integer, primary_key=True)
info = Column(JSONB)
session = sessionmaker(bind=engine)()
jt = JsonTest()
jt.info= {"a": 123, "b": "SQLAlchemy"}
session.add(jt)
session.commit()
for row in session.query(JsonTest):
print(row.info)
if __name__ == "__main__":
main()