Reputation: 129
My program is built to buy and sell (fake) shares of a stock, then add each transaction to a database. I'm using SQLAlchemy to create the database and tables, and so far it works great. There is one rather large issue though: Since I create the tables within my program (and my program is constantly looping), every time it loops the tables are recreated.
Tables:
class Wallet(Base):
__tablename__ = 'wallets'
id = Column(Integer, Sequence('wallet_id_seq'), primary_key=True)
name = Column(String(50))
balance = Column(Integer())
def __repr__(self):
return "<Wallet(name='%s', balance='%s')>" % (self.name, self.balance)
class Transaction(Base):
__tablename__ = 'transactions'
id = Column(Integer, Sequence('transaction_id_seq'), primary_key=True)
stock = Column(String(50))
symbol = Column(String(50))
buy_or_sell = Column(String(50))
price = Column(Integer())
ema = Column(Integer())
shares = Column(Integer())
time = Column(String(50))
def __repr__(self):
return "<Transaction(stock='%s', symbol='%s', buy_or_sell='%s', price='%s', ema='%s', shares='%s', time='%s')>" % (self.stock, self.symbol, self.buy_or_sell, self.price, self.ema, self.shares, self.time)
I tried to relocate the tables to another Python file and import them so I could loop the main program over and over without recreating them.
bin/
main.py
databases.py
__init__.py
But the issue is persisting. I'm guessing it's because every time classes Wallet
and Transaction
are imported it recreates the tables. Or maybe it's just that the database create_all function (Base.metadata.create_all(engine)
) is still in main.py and runs on every loop.
How do I create the database once and then update its rows/columns every time main.py runs?
Upvotes: 0
Views: 734
Reputation: 129
Credit to Jonas for answering my question. By creating the database in temporary memory it was being deleted every loop. The solution is using an actual file instead.
Upvotes: 1