Solution ModuleNotFoundError: No module named 'psycopg2',
To solve this problem try following command :
pip install psycopg2
Hope this can help,
Solution ModuleNotFoundError: No module named 'psycopg2',
To solve this problem try following command :
pip install psycopg2
Hope this can help,
SQLAlchemy is an awesome Object-Relational Mapping (ORM) library that allows us to interact with databases using Python. I will provide you with my step-by-step notes on how to create a database using Python and Alchemy.
Before you begin you have to make sure you have the necessary tools installed.
pip install sqlalchemy
)Awesome. Now let’s begin!
Let's start by creating a new Python script and importing the necessary modules.
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Create a database connection using SQLAlchemy’s create_engine
function. Replace 'your_database_url'
with the URL of your database.
# PostgreSQL database URL
database_url = 'postgresql://username:password@host/database_name'
# MySQL database URL
database_url = 'mysql+pymysql://username:password@host/database_name'
For MySQLite, he URL would be:
engine = create_engine('sqlite:///your_database_name.db')
Note: Replace 'username'
, 'password'
, 'host'
, and 'database_name'
with your MySQL or PostgreSQL credentials and database information. It's good to remember the database needs to be installed and running before you are connecting to it.
To create your data model, make a Python class that inherits from the Base. Each attribute of the class represents a column in the relevant database table. You can include additional columns as you see fit.
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(50), unique=True, nullable=False)
email = Column(String(100), unique=True, nullable=False
password = Column(String(100), unique=True, nullable=False)
created_at = Column(DateTime, default=datetime.datetime.utcnow)
With the data model defined, create the database tables using the create_all
method of the Base
class:
Base.metadata.create_all(engine)
To insert data into your database, first, create a session using SQLAlchemy’s sessionmaker
:
Session = sessionmaker(bind=engine)
session = Session()
Hurray! Now, you can create objects and add them to your database.
# Example: Inserting a new user into the database
new_user = User(username='Sandy', email='sandy@gmail.com', password='cool-password')
session.add(new_user)
session.commit()
To retrieve data from the database, use the session’s query
method:
# Example: Querying all users from the database
all_users = session.query(User).all()
# Example: Querying a specific user by their username
user = session.query(User).filter_by(username='Sandy').first()
Remember to close the session once you’re done working with the database:
session.close()
If we were to compare SQLAlchemy and plain SQL, we could notice the difference in how they interact with data and the advantages they offer.
2. Portability:
3. Safety and security:
4. Code maintainability and readability:
5. Database schema management:
Migration scripts are SQL scripts made by users in ApexSQL Source Control. They help configure changes, handle overrides and more.
After everything has been said and done, our SQLAlchemy code should resemble the following example:
# Step 1: Import the necessary modules
from sqlalchemy import create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
# Step 2: Establish a database connection
database_url = 'sqlite:///your_database_name.db'
# Create an engine to connect to a SQLite database
engine = create_engine(database_url)
#will return engine instance
Base = declarative_base()
# Step 3: Define your data model
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(50), unique=True, nullable=False)
email = Column(String(100), unique=True, nullable=False)
created_at = Column(DateTime, default=datetime.datetime.utcnow)
# Step 4: Create the database tables
Base.metadata.create_all(engine)
# Step 5: Insert data into the database
Session = sessionmaker(bind=engine)
session = Session()
# Example: Inserting a new user into the database
new_user = User(username='Sandy', email='sandy@gmail.com', password='cool-password')
session.add(new_user)
session.commit()
# Step 6: Query data from the database
# Example: Querying all users from the database
all_users = session.query(User).all()
# Example: Querying a specific user by their username
user = session.query(User).filter_by(username='Sandy').first()
# Step 7: Close the session
session.close()
Great job! You have successfully mastered the art of creating a database with Python and SQLAlchemy.
Things to remember:
If you are looking for a flexible library to manage databases in your Python projects, SQLAlchemy is a great choice. It provides a user-friendly and robust interface to handle all your database-related tasks with ease.
Source : https://medium.com/@sandyjtech/creating-a-database-using-python-and-sqlalchemy-422b7ba39d7e
command for running FastAPI with customs port, ok let see and try implement command below :
uvicorn main:app --port 7878
Our FastAPI now running on port 7878
To host on a local network, first ensure that your IP address is added to the ALLOWED_HOSTS
of settings.py
.
ALLOWED_HOSTS = ['192.168.1.X']
Note: Your IP address can be locate via running
ipconfig
in the command prompt
Ensure you also execute runserver
on ip address 0.0.0.0:8000
. For example:
python manage.py runserver 0.0.0.0:8000
When connecting with other devices put the IP address and the port number of the host in the URL within the browser. Like so:
192.168.1.X:8000/<app_name>/other_pages
Source : https://stackoverflow.com/questions/68713009/cant-access-django-server-remotely