- Install sqlalchemy:
pip install sqlalchemy. - Figure out what you want your columns to be in your database table.
You have to alter the file
models.pyso the attributes in the class match the columns you want in your data base. In this example the class that models your data is calledEmploymentInfo. If you change the nameEmploymentInfoinmodels.pythen also change the references to it inmake_db_table.pyandaddrows.py. - Create your database. In this example it's called "fed_employment".
With postgres you can go into your terminal, type
psqland then enterCREATE DATABASE fed_employment;(don't forget the semicolon, like I do so often). The database must be created before you runmake_db_table.py - Change database connection info in
settings.py. If you connect to postgres on your computer then the host is probably "localhost" and the port is probably "5432". - Run
python make_db_table.pyin the terminal. If there were no problems then you should see a bunch of jargon about sql and the last line should say something about an sqlalchemy engine commit. - Modify the
addrows.pyfile to add rows to the database.
from models import dbsession
third_employee = dbsession.query(EmployeeInfo).filter(employee_id == 3).first()
print(third_employee.employee_name)
>>> Bob
all_employees = dbsession.query(EmployeeInfo).all()
for employee in all_employees:
print(employee.id)
>>> 1
>>> 2
.
.
.from models import engine
res = engine.execute('SELECT * FROM employee_info;').fetchall()Here res is only a list of rows which are themselves lists. You can't
say something like res.employee_id because res is not an python object.
This is useful if you want to manipulate the data fast with Pandas.
from models import engine
df = pd.read_sql_query('SELECT * FROM "employee_info"', con=engine)