[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)

8.3.6.2 SQL representation language

--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)]


8.3.6.3 ORM (Object-Relational Mapping)

--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>>


Impressions

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.

References

"Introduction to Python3 by Bill Lubanovic (published by O'Reilly Japan)"

Recommended Posts

[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.1-8.2.5)
[Introduction to Python3, Day 17] Chapter 8 Data Destinations (8.3-8.3.6.1)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
[Introduction to Python3 Day 13] Chapter 7 Strings (7.1-7.1.1.1)
[Introduction to Python3 Day 15] Chapter 7 Strings (7.1.2-7.1.2.2)
[Introduction to Python3 Day 21] Chapter 10 System (10.1 to 10.5)
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Python3 Day 23] Chapter 12 Become a Paisonista (12.1 to 12.6)
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Introduction to Python3 Day 8] Chapter 4 Py Skin: Code Structure (4.1-4.13)
[Technical book] Introduction to data analysis using Python -1 Chapter Introduction-
[Introduction to Python3 Day 1] Programming and Python
[Introduction to Python3 Day 2] Chapter 2 Py Components: Numbers, Strings, Variables (2.1)
[Introduction to Python3 Day 4] Chapter 2 Py Components: Numbers, Strings, Variables (2.3.7-2.4)
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Data Scientists] Basics of Python ♬
[Introduction to Python3 Day 7] Chapter 3 Py Tools: Lists, Tuples, Dictionaries, Sets (3.3-3.8)
[Introduction to Python3 Day 10] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.4-5.7)
[Introduction to Python3 Day 9] Chapter 5 Py's Cosmetic Box: Modules, Packages, Programs (5.1-5.4)
[Introduction to Python3 Day 6] Chapter 3 Py tool lists, tuples, dictionaries, sets (3.2.7-3.2.19)
Introduction to Python language
Introduction to OpenCV (python)-(2)
Introduction to effectiveness verification Chapter 3 written in Python
[Introduction to Python] How to handle JSON format data
Introduction to Effectiveness Verification Chapter 2 Written in Python
Introduction to Python Django (2) Win
Python for Data Analysis Chapter 4
[Chapter 5] Introduction to Python with 100 knocks of language processing
Reading Note: An Introduction to Data Analysis with Python
Introduction to serial communication [Python]
[Chapter 3] Introduction to Python with 100 knocks of language processing
[Chapter 2] Introduction to Python with 100 knocks of language processing
[Introduction to Python] <list> [edit: 2020/02/22]
Python for Data Analysis Chapter 2
Introduction to Python (Python version APG4b)
An introduction to Python Programming
Introduction to Python For, While
Python for Data Analysis Chapter 3
[Chapter 4] Introduction to Python with 100 knocks of language processing
20200329_Introduction to Data Analysis with Python Second Edition Personal Summary
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Python] Introduction to graph creation using coronavirus data [For beginners]
[Introduction to Python] How to get data with the listdir function
[Introduction to Udemy Python 3 + Application] 58. Lambda
[Introduction to Udemy Python 3 + Application] 31. Comments
[Python] How to FFT mp3 data
Data Scientist Training Course Chapter 2 Day 2
Introduction to Python Numerical Library NumPy
Practice! !! Introduction to Python (Type Hints)
Data Scientist Training Course Chapter 3 Day 3
Data Scientist Training Course Chapter 4 Day 1
[Introduction to Udemy Python 3 + Application] 57. Decorator
Data Scientist Training Course Chapter 3 Day 1 + 2
[Introduction to Python] How to parse JSON
[Introduction to Udemy Python 3 + Application] 56. Closure
Introduction to Protobuf-c (C language ⇔ Python)
[Introduction to Udemy Python3 + Application] 59. Generator
[Introduction to Python] Let's use pandas