Skip to content

Instantly share code, notes, and snippets.

@danilobatistaqueiroz
Last active May 22, 2019 21:18
Show Gist options
  • Select an option

  • Save danilobatistaqueiroz/c2cd50261aeccbe345da5e989c0940c8 to your computer and use it in GitHub Desktop.

Select an option

Save danilobatistaqueiroz/c2cd50261aeccbe345da5e989c0940c8 to your computer and use it in GitHub Desktop.
sqlalchemy

Install sql alchemy

Create and use a virtual environment to avoid libraries conflict between projects:

creating_a_virtualenv

Install alchemy library:

install_alchemy

Import libraries:

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relation, sessionmaker

Connect:

engine = create_engine('mysql+pymysql://maria:123@localhost/brlabs?host=localhost?port=3306')
Base.metadata.create_all(engine)

Map tables to classes:

class Filme(Base):
    __tablename__ = 'filmes'
...
class Diretor(Base):
    __tablename__ = 'diretores'
...

run the script, and you'll see in mysql console:

mysql_console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment