SQLALCHEMY – returning a pretty format

Question:

I am trying to create a basic CRUD application with python using sqlalchemy. It is just a virtual contact book, that runs in the terminal.

I have managed to create this and get it working successfully, but I am having trouble with the following:

Whenever I run one of the sqlalchemy functions to access the database, I get the result, but also receive something that looks like this:

2022-11-14 19:45:37,694 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-11-14 19:45:37,698 INFO sqlalchemy.engine.Engine SELECT count(*) AS count_1 
FROM (SELECT contacts.name AS contacts_name 
FROM contacts) AS anon_1
2022-11-14 19:45:37,698 INFO sqlalchemy.engine.Engine [generated in 0.00020s] ()

I don’t want this to displayed to the user when using the application!

How do I remove this so the user does not see this?

My sqlalchemy model looks like this:

from sqlalchemy import Column, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


Base = declarative_base()


class Contact(Base):
    __tablename__ = "contacts"

    name = Column("name", String, primary_key=True)
    number = Column("number", String)
    email = Column("email", String)

    def __init__(self, name, number, email):
        self.name = name
        self.number = number
        self.email = email

    def __repr__(self):
        return f"n{self.name.title()}: ntNumber: {self.number}ntEmail: {self.email}"


engine = create_engine("sqlite:///contacts.db", echo=True)
Base.metadata.create_all(bind=engine)

Session = sessionmaker(bind=engine)
session = Session()

and my sqlalchemy functions look like this:

from utils.SQLDB import Contact, session


def add_contact(name, number, email):
    """Adds a new contact to the database."""
    contact = Contact(name.title(), number, email)
    session.add(contact)
    session.commit()
    print(f"{contact.name.title()} has been added!")


def delete_contact(name):
    """Deletes a specific contact from the database."""
    session.query(Contact).filter(Contact.name == name).delete()
    session.commit()
    print(f"{name} has been deleted.")


def show_all_contacts():
    """Shows all contacts in alphabetical order."""
    results = session.query(Contact).order_by("name").all()
    for person in results:
        print(person)


def search_contact(name):
    """Searches for contacts who have the given string combination in their name."""
    result = session.query(Contact).filter(
        Contact.name.like(f"%{name}%")).all()
    for contact in result:
        print(contact)


def update_contact(name, new_name=Contact.name, new_number=Contact.number, new_email=Contact.email):
    """Updates, name, numbers and email adresses."""
    session.query(Contact).filter(Contact.name == name).update(
        {Contact.name: new_name, Contact.number: new_number, Contact.email: new_email})
    session.commit()
    print(f"{Contact.name} has been updated.")


def count_contacts():
    """Return the total number of contacts in the virtual contact book."""
    count = session.query(Contact.name).count()
    return count

I am new to sqlalchemy and I am still trying to get my head around it (I’m quite new coding as a whole really) so any help would be greatly appreciated – especially if it can be ‘dumbed-down’ a bit to help me understand it a bit easier!!

Asked By: ScottMButler

||

Answers:

Try setting echo to False like this:

engine = create_engine("sqlite:///contacts.db", echo=False)

sqlalchemy.create_engine.params.echo

Answered By: Ian Wilson