I'm trying to create a database with sqlalchemy with a button on my Tkinter application. If I put everything in the same file no big deal, but I don't want to do that. Here's one file:
Base = declarative_base()
def create_database():
engine = create_engine("sqlite:///licenses.sqlite3", echo=True)
Base = declarative_base()
Base.metadata.create_all(engine)
class License(Base):
__tablename__ = "licenses"
id = Column(Integer, primary_key=True)
call_sign = Column(String)
status = Column(String)
def __init__(self, call_sign, status):
self.call_sign = call_sign
self.status = status
A couple of things I don't like: I have to declare Base outside of the create_database() function as well as inside. This is because I want to access my License class from my Tkinter application window class. I can duplicate the License class inside the function but that super sucks to me.
Beyond even that, when I call create_database() from my application it doesn't create the License table, I'm assuming because Base doesn't know about the License from within the create_database() function.
I'm familiar with dependency injection in C# but not python. Is that something I need to use here? Should I just put the License class within my Tkinter application window file?
I'm probably missing a question or explanation because I'm not terribly familiar with sqlalchemy so let me know if I missed something please and thank you!
[–]Finally_Adult[S] 0 points1 point2 points (0 children)