Reputation: 105
I have created a database using SQLAlchemy which column names business_id , useful.
class Review(Base):
__tablename__ = 'rev'
id=Column(Integer,primary_key=True)
business_id = Column(String(50))
useful = Column(Integer)
# initial_rating=Column(Integer)
engine = create_engine('sqlite:///sqlalchemy_try.db')
Base.metadata.create_all(engine)
Now i have a json file and i want to load the business id and useful present in json file into this database.I have opened the json file but dont know how to load it in this database:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy_declarative import Base, Review
engine = create_engine('sqlite:///sqlalchemy_try.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
with open('rev.json') as f:
data=f.read()
jsondata=json.loads(data)
Upvotes: 1
Views: 3022
Reputation: 40884
I'd suppose something like this:
r = Review(int(jsondata['id']), jsondata['business_id'], int(jsondata['useful']))
session.add(r)
In the end, add session.commit()
.
Upvotes: 1