Installation and simple query of learning notes of PythonORM framework SQLAlchemy

Source: Internet
Author: User
This article describes how to install and query an instance in the PythonORM framework SQLAlchemy learning notes, for more information, see Object Relational er (Object Relational Mapper) and SQLAlchemy (project homepage, I was trying to use Django's ORM module, but Django's modules were closely linked and could not be split separately, to a certain extent, it indicates that Django's self-contained ecosystem sacrifices the flexibility of assembly while bringing us a fast and convenient development environment.

I didn't really feel the benefits of SQLAlchemy when I first studied it. However, many of the companies that I introduced use this project, and the databases it supports are quite rich, so I think it is worthwhile to spend some time researching. Unfortunately, SQLAlchemy has a small amount of Chinese materials, which makes it difficult for us to use poor English.

The best way to study a project is to read its official documentation. of course, you can easily find the SQLAlchemy documentation (0.7 ). The document format is the same as that for most projects. download and install instructions, examples, and quick start tutorials are provided. However, I am still used to downloading a PDF file for further research.

Next I will take a note of my recent reading and learning. of course, this is for reference only. I may have some guesses and ideas in it, but I hope to point out some improper ideas.

1. install SQLAlchemy

The installation part is not described in detail. you can install it through easy_install or pip. the command is as follows:

The code is as follows:

Easy_install SQLAlchemy
# Or
Pip install SQLAlchemy


Of course, I am using a Windows environment, so I prefer to use setup. py to install, download the compressed package, decompress the package, switch to the directory at the command prompt, and then run the following command:

The code is as follows:


Python setup. py install


Note that C extensions will be compiled and installed by default. These C extensions will be directly compiled into binary native code and then processed for SQLAlchemy for dataset acceleration. this is a very good feature, unfortunately, Windows prompts that the compilation and installation of extensions failed. of course, this does not affect the use of SQLAlchemy, but is only used for performance optimization. These extensions are not required in the local development environment, if not, try the following command:

The code is as follows:


Pip install -- global-option = '-- without-cextensions 'sqlalchemy
# Or setup. py
Python setup. py -- without-cextensions install


Now, I will briefly introduce the installation part here. if you are interested in this part, you can proceed to the document.

Finally, check the installation results:

The code is as follows:


>>> Import sqlalchemy
>>> Sqlalchemy. _ version __
0.7.0

2. simple query

Just as any new language starts from the omnipotent 'Hello World', we should first try SQLAlchemy. because SQLAlchemy is used to manage databases, we need a database, since Python was used, when it comes to databases, the first thing to experiment with is SQLite3, which comes with Python. this time, we don't need to specify any database files for SQLite, directly create a memory-based database, that is to say, the data files are stored in the memory, which is convenient for the following test.

We use create_engine to create a database connection engine:

The code is as follows:


>>> From sqlalchemy import create_engine
>>> Engine = create_engine ('sqlite: //: memory: ', echo = True)


The first parameter of create_engine is 'sqlite: //: memory: '. we know that the database connection is established. what is the second parameter echo = True, in fact, if echo = True, SQLAlchemy will use the Python standard module logging to output logs. if you operate the interactive command console, some information will be output, here we may see some SQL statements generated by SQLAlchemy. this is necessary for us to learn and debug, so here we set it to True. otherwise, if you do not want SQLAlchemy to be so arrogant, you can set it to False so that you will not be able to see this information.

Create_engine () returns an Engine instance, which represents SQLAlchemy's core database interfaces. it hides the details of various database dialects (dialect, in fact, the underlying layer of SQLAlchemy is Python's DBAPI.

It should be noted that at this time there is no substantial connection to the database. When will the connection be established with the database? This will only happen when you first query the database. Er... This is a bit like Lazy Loading (Lazy Loading, delayed Loading). That is to say, we need to actually create a connection when operating the database. SQLAlchemy uses Lazyload in many places and will be introduced later.

Next we will execute the first SQL statement and establish a database connection:

The code is as follows:


>>> Engine.exe cute ("select 1"). scalar ()
1


When engine.exe cute is executed, the Engine finally establishes a physical database connection.

The Engine manages the database connection Pool. when the connection is established for the first time, SQLAlchemy puts established connections into the internal connection pool to facilitate reuse of subsequent data operation statements.

Of course, the usage of the Engine is not a wonderful ORM part of SQLAlchemy. we will introduce how to bind the Engine to the ORM and then use the object to operate the database part.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.