Filipe Ferminiano
Filipe Ferminiano

Reputation: 8791

Unable to insert from sqlalchemy to mysql

I'm trying to insert a row into a table of a mysql db with sql alchemy, but when I insert and go to the database check the data, I see nothing.

This is my code:

from sqlalchemy import create_engine from sqlalchemy import Table, MetaData from sqlalchemy.sql import table, column, select, update, insert from sqlalchemy.orm import sessionmaker

engine = create_engine(
      "mysql://user:password@host/schema")
connection = engine.connect()

metadata = MetaData(bind=engine)
prices = Table('historical_prices', metadata, autoload=True)
i = insert(prices)
i = i.values({
    "date": my_date, 
    "instrument": msg['instrument'],
    "open": msg['candles'][0]['openAsk'],
    "high": msg['candles'][0]['highAsk'],
    "low": msg['candles'][0]['lowAsk'],
    "close": msg['candles'][0]['closeAsk'],
    "volume": msg['candles'][0]['volume']
    })
Session = sessionmaker(bind=engine)
session = Session()
session.execute(i)

I receive no error message from this code. How can I fix this?

Upvotes: 0

Views: 1601

Answers (1)

Kaszanas
Kaszanas

Reputation: 475

As the right answer haven't been posted yet by the original person who came up with it. I am going forward to do so.

I don't know if it is because You've left it out, but You're missing a session.commit()

Hopefully this answer will be more visible for next people with the same problem.

Upvotes: 2

Related Questions