简体   繁体   English

通过python将Json字段存储在外部文件(.json)中

[英]store Json field in an external file (.json) by python

my table in postgresql v11 contains a json field, i would to store this data in an external file JSON using python (lib: SQLALSHEMY).我在 postgresql v11 中的表包含一个 json 字段,我会使用 python (lib: SQLALSHEMY) 将此数据存储在外部文件 JSON 中。 i try to do it but i have a difficulty:我尝试这样做,但我遇到了困难:

import psycopg2
from app import db
from models import Geotab 

from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.sql import select




try:
connection = psycopg2.connect(user = "postgres",
                              password = "admin",
                              host = "127.0.0.1",
                              port = "5432",
                              database = "catalogue")
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
except (Exception, psycopg2.Error) as error :
 print ("Error while connecting to PostgreSQL", error)



data=db.session.query(Geotab).\
   filter(Geotab.dataJson[''])   #.astext.cast(JSON))
print (data)

after executing code:执行代码后:

user=postgres password=admin host=127.0.0.1 port=5432 dbname=catalogue {'user': 'postgres', 'dbname': 'catalogue', 'host': '127.0.0.1', 'port': '5432', 'tty': '', 'options': '', 'sslmode': 'prefer', 'sslcompression': '0', 'krbsrvname': 'postgres', 'target_session_attrs': 'any'} You are connected to - ('PostgreSQL 11.4, compiled by Visual C++ build 1914, 64-bit',) SELECT geocat.id AS geocat_id, geocat.url AS geocat_url, geocat.date AS geocat_date, geocat."dataJson" AS "geocat_dataJson" FROM geocat WHERE geocat."dataJson" -> %(dataJson_1)s user=postgres password=admin host=127.0.0.1 port=5432 dbname=catalogue {'user':'postgres','dbname':'目录','host':'127.0.0.1','port':'5432 ', 'tty': '', 'options': '', 'sslmode': 'prefer', 'sslcompression': '0', 'krbsrvname': 'postgres', 'target_session_attrs': 'any'}连接到 - ('PostgreSQL 11.4, 由 Visual C++ build 1914, 64-bit',) SELECT geocat.id AS geocat_id, geocat.url AS geocat_url, geocat.date AS geocat_date, geocat."dataJson" AS "geocat_dataJson" FROM geocat WHERE geocat."dataJson" -> %(dataJson_1)s

geotab : class in models.py dataJson : column type json table name: geocat geotab:models.py 中的类 dataJson:列类型 json 表名:geocat

help me please请帮帮我

i find the solution, thank you:我找到了解决方案,谢谢:

from sqlalchemy import create_engine
from app import db
from models import Geotab 
from sqlalchemy import Integer
import json

from sqlalchemy.dialects.postgresql import JSON


engine = create_engine('postgresql://postgres:admin@127.0.0.1:5432/catalogue', echo = 
True)
conn = engine.connect()


data=db.session.query(Geotab).\
  filter(Geotab.id==1)
print (data)
result = db.session.query(Geotab).all()

metadata={}
for row in result:
   print ("dataJson:",row.dataJson)
   metadata=row.dataJson
with open('metadata.json', 'w') as outfile:
  json.dump(metadata, outfile)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM