Home > Enterprise >  Python sqlalchemy how to check if db empty?
Python sqlalchemy how to check if db empty?

Time:10-25

Im trying to make a function that will paste information into a database incase its empty i got it to work if i use

customer_name = "Ilya"
    customer_details = session.query(db.Customers).filter(
    db.Customers.name == customer_name).first()

but the problem is if the customer under the name "Ilya" gets removed it will readd all of information again how do i make it only work if the database is empty?

from sqlalchemy.orm import sessionmaker


def db_load_example_data(app, db,session):
    customer_name = "Ilya"
    customer_details = session.query(db.Customers).filter(
    db.Customers.name == customer_name).first()
    if customer_details:
        pass
    else:
        #books
        book_01 = db.Books(name="The Teachings Of Don Juan: A Yaqui Way of Knowledge", author="Carlos Castaneda", year_published=1968, type=1)
        book_02 = db.Books(name="Journeys out of the body", author="Robert Monroe", year_published=1971, type=2)
        book_03 = db.Books(name="Lucid Dreaming The power of being aware and awake in your dreams", author="Stephen LaBerge", year_published=1985, type=3)

        # customers
        custormer_00 = db.Customers(name="Ilya", city="TelAviv", age=22)
        custormer_01 = db.Customers(name="Jack", city="RoshHain", age=35)
        custormer_02 = db.Customers(name="Michael", city="Haifa", age=38)
        
        # loans
        loan_01 = db.Loans(customer_id="1", book_id="1", loan_date="2022-10-06", return_date="2022-10-08")

        with app.app_context():
            db.base.metadata.create_all(db.engine)
            Session = sessionmaker(bind=db.engine)  # initialize sessionmaker
            session = Session()  # make Session object
            session.add(custormer_00)
            session.add(custormer_01)
            session.add(custormer_02)
            session.add(book_01)
            session.add(book_02)
            session.add(book_03)
            session.add(loan_01)
            session.commit()

this is my db

# import statements
from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.ext.declarative import declarative_base

# create engine
engine = create_engine('sqlite:///library.sqlite', echo=True,
                       connect_args={'check_same_thread': False})
base = declarative_base()

# Books Table


class Books(base):

    __tablename__ = 'Books'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    author = Column(String)
    year_published = Column(String)
    type = Column(Integer)

    def __init__(self, name, author, year_published, type):
        self.name = name
        self.author = author
        self.year_published = year_published
        self.type = int(type)

# Customers Table


class Customers(base):
    __tablename__ = 'Customers'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    city = Column(String)
    age = Column(Integer)

    def __init__(self, name, city, age):
        self.name = name
        self.city = city
        self.age = int(age)

# Loans Table


class Loans(base):
    __tablename__ = 'Loans'
    id = Column(Integer, primary_key=True)
    customer_id = Column(Integer, ForeignKey(
        "Customers.id", ondelete="CASCADE"), nullable=False)
    book_id = Column(Integer, ForeignKey(
        "Books.id", ondelete="CASCADE"), nullable=False)
    loan_date = Column(String)
    return_date = Column(String)

    def __init__(self, customer_id, book_id, loan_date, return_date):
        self.customer_id = int(customer_id)
        self.book_id = int(book_id)
        self.loan_date = loan_date
        self.return_date = return_date

CodePudding user response:

I realize this might be very obvious, but why do you filter your query?

What if you did

session.query(db.Customers).first()

instead of

session.query(db.Customers).filter(db.Customers.name == customer_name).first()

Then you could check if there are any users in the database instead of checking for a specific user.

  • Related