--The expression language introduces functions that create SQL for various operations.
--The following zoo objects take an intermediate approach between the world of SQL databases and the world of Python data structures. (** I understand that there is a mixture of grammar similar to SQL statements (only uppercase → lowercase) and Python syntax such as select () **)
>>> import sqlalchemy as sa
>>> conn=sa.create_engine("sqlite://")
#Use an expression language instead of SQL to define the zoo table.
>>> meta=sa.MetaData()
>>> zoo=sa.Table("zoo",meta,
... sa.Column("critter",sa.String,primary_key=True),
... sa.Column("count",sa.Integer),
... sa.Column("damages",sa.Float)
... )
>>> meta.create_all(conn)
#Insert data using a representation language.
>>> conn.execute(zoo.insert(("bear",2,1000.0)))
<sqlalchemy.engine.result.ResultProxy object at 0x108f30590>
>>> conn.execute(zoo.insert(("weasel",1,2000.0)))
<sqlalchemy.engine.result.ResultProxy object at 0x108f306d0>
>>> conn.execute(zoo.insert(("duck",10,0)))
<sqlalchemy.engine.result.ResultProxy object at 0x108f303d0>
#Make a SELECT statement. zoo.select()Is a plain SQL Python SELECT*Equal to FROM zoo, it selects all the information in the table represented by the zoo object.
>>> result=conn.execute(zoo.select())
#Get information and store it in rows.
>>> rows=result.fetchall()
>>> print(rows)
[('bear', 2, 1000.0), ('weasel', 1, 2000.0), ('duck', 10, 0.0)]
--At the top layer of SQLAIchemy, ORM uses SQL representation, but the actual DB mechanism is not visible. If you define a class, ORM will process the data with the DB. -** The basic idea of object relation mapping is to be able to refer to objects in code and continue writing code in a Python-like way while using RDBMS. ** **
>>> import sqlalchemy as sa
>>> from sqlalchemy.ext.declarative import declarative_base
#Opening a connection.
>>> conn=sa.create_engine("sqlite:///zoo.db")
>>> Base=declarative_base()
#Enter the SQL AIchemy ORM.
>>> class Zoo(Base):
... __tablename__="zoo"
... critter=sa.Column("critter",sa.String,primary_key=True)
... count=sa.Column("count",sa.Integer)
... dameges=sa.Column("damages",sa.Float)
... def __init__(self,critter,count,damages):
... self.critter=critter
... self.count=count
... self.damages=damages
... def __repr__(self):
... return "<Zoo({},{},{})>".format(self.critter,self.count,self.damages)
...
#DB and table creation
>>> Base.metadata.create_all(conn)
#If you create a Python object, you can insert data into the zoo table.
>>> first=Zoo("duck",10,0.0)
>>> second=Zoo("bear",2,1000.0)
>>> third=Zoo("weasel",1,2000.0)
>>> first
<Zoo(duck,10,0.0)>
--Create a session to interact with the DB.
>>> from sqlalchemy.orm import sessionmaker
>>> Session =sessionmaker(bind=conn)
>>> session=Session()
#Write the three objects created earlier in the session to the DB.
#add()Function adds one object_all()The function adds a list.
>>> session.add(first)
>>> session.add_all([second,third])
#Finally, forcibly complete all processing.
>>> session.commit
<bound method Session.commit of <sqlalchemy.orm.session.Session object at 0x108f3f1d0>>
I am currently studying parallel processing in Chapter 11, but the content is quite rich. It doesn't come to my mind at all, but when I look at the Python documentation, there are parts that are written in more detail than the book, and I became able to understand multiprocessing that I did not understand yesterday, and I felt the importance of the documentation.
"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"
Recommended Posts