-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use SQLAlchemy #16
Labels
Comments
What's the difference between SQLAlchemy and SQLAlchemy Core?SQLAlchemy is a popular Python library that provides both SQLAlchemy Core and SQLAlchemy ORM. Here are the key differences between the two: SQLAlchemy Core:
from sqlalchemy import create_engine
engine = create_engine('mysql://scott:tiger@localhost/test')
connection = engine.connect()
result = connection.execute("select username from users")
for row in result:
print("username:", row['username'])
connection.close() SQLAlchemy ORM:
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String)
# Query all users
users = session.query(User).all()
for user in users:
print("username:", user.username) In summary, if you need to programmatically build queries at runtime, use SQLAlchemy Core. If you want to build your application with a model-based approach and have database-backed objects, use SQLAlchemy ORM. Sources: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sources
https://docs.sqlalchemy.org/en/20/orm/index.html
The text was updated successfully, but these errors were encountered: