Created for
CREATE TABLE users (
id serial primary key,
name character varying,
address character varying
)
class Users:
def __init__(self, name, address):
self.name = name
self.address = address
root@localhost:SimpleCompanyDB> SELECT * from company;
+--------------+----------------+
| company_id | company_name |
|--------------+----------------|
| 1 | Google |
| 2 | Facebook |
| 4 | Microsoft |
| 5 | Microsoft |
+--------------+----------------+
4 rows in set
Time: 0.002s
mariadb root@localhost:SimpleCompanyDB> SELECT * from employee;
+---------------+-----------------+
| employee_id | employee_name |
|---------------+-----------------|
| 1 | Ivan Petrov |
| 2 | Maria Popova |
| 3 | Georgi Ivanov |
+---------------+-----------------+
3 rows in set
Time: 0.005s
mariadb root@localhost:SimpleCompanyDB> SELECT * from company_employee
+---------------+--------------+
| employee_id | company_id |
|---------------+--------------|
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
| 3 | 2 |
+---------------+--------------+
4 rows in set
The SQLAlchemy flow:
engine = create_engine(
dialect+driver://username:password@host:port/database)
from sqlalchemy import create_engine
# connect to Mysql/MariaDB:
mysql_engine = create_engine(
"mysql+pymysql://user:pass@localhost/test")
# connect to PostgreSQL:
pg_engine = create_engine('postgresql://usr:pass@localhost/sqlalchemy')
Engine Configuration @http://docs.sqlalchemy.org
from sqlalchemy import create_engine
#Create engine with MySQL dialect:
engine = create_engine(
'mysql+pymysql://pythontest:123@localhost/SimpleCompanyDB?charset=utf8',
connect_args = {
# 'port': 3306
},
# echo='debug',
echo_pool=True
)
# test the connection - get tables names:
print("Tables in SimpleCompanyDB: ",engine.table_names())
# Tables in SimpleCompanyDB: ['company', 'company_employee', 'employee']
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Address(Base):
__tablename__ = 'address'
# define columns for the table address.
id = Column(Integer, primary_key=True)
street_name = Column(String(250))
street_number = Column(String(250))
post_code = Column(String(250), nullable=False)
person_id = Column(Integer, ForeignKey('person.id'))
person = relationship(Person)
Example of One To Many relationship:
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
pets = relationship("Pet")
class Pet(Base):
__tablename__ = 'pet'
id = Column(Integer, primary_key=True)
person_id = Column(Integer, ForeignKey('person.id'))
company_employee = Table('company_employee', Base.metadata,
Column('employee_id', Integer, ForeignKey('employee.employee_id')),
Column('company_id', Integer, ForeignKey('company.company_id'))
)
class Employee(Base):
__tablename__ = 'employee'
employee_id = Column(Integer, primary_key=True)
companies = relationship("Company", secondary=company_employee)
class Company(Base):
__tablename__ = 'company'
id = Column(Integer, primary_key=True)
import MySQLdb
mysql_cn= MySQLdb.connect(host='myhost',
port=3306,user='myusername', passwd='mypassword',
db='information_schema')
df_mysql = pd.read_sql('select * from VIEWS;', con=mysql_cn)
print 'loaded dataframe from MySQL. records:', len(df_mysql)
mysql_cn.close()
These slides are based on
customised version of
framework