Category: sqlalchemy

  • Khóa học miễn phí SQLAlchemy – Introduction nhận dự án làm có lương

    SQLAlchemy – Introduction



    SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license.

    SQLAlchemy is famous for its object-relational mapper (ORM), using which, classes can be mapped to the database, thereby allowing the object model and database schema to develop in a cleanly decoupled way from the beginning.

    As size and performance of SQL databases start to matter, they behave less like object collections. On the other hand, as abstraction in object collections starts to matter, they behave less like tables and rows. SQLAlchemy aims to accommodate both of these principles.

    For this reason, it has adopted the data mapper pattern (like Hibernate) rather than the active record pattern used by a number of other ORMs. Databases and SQL will be viewed in a different perspective using SQLAlchemy.

    Michael Bayer is the original author of SQLAlchemy. Its initial version was released in February 2006. Latest version is numbered as 1.2.7, released as recently as in April 2018.

    What is ORM?

    ORM (Object Relational Mapping) is a programming technique for converting data between incompatible type systems in object-oriented programming languages. Usually, the type system used in an Object Oriented (OO) language like Python contains non-scalar types. These cannot be expressed as primitive types such as integers and strings. Hence, the OO programmer has to convert objects in scalar data to interact with backend database. However, data types in most of the database products such as Oracle, MySQL, etc., are primary.

    In an ORM system, each class maps to a table in the underlying database. Instead of writing tedious database interfacing code yourself, an ORM takes care of these issues for you while you can focus on programming the logics of the system.

    SQLAlchemy – Environment setup

    Let us discuss the environmental setup required to use SQLAlchemy.

    Any version of Python higher than 2.7 is necessary to install SQLAlchemy. The easiest way to install is by using Python Package Manager, pip. This utility is bundled with standard distribution of Python.

    pip install sqlalchemy
    

    Using the above command, we can download the latest released version of SQLAlchemy from and install it to your system.

    In case of anaconda distribution of Python, SQLAlchemy can be installed from conda terminal using the below command −

    conda install -c anaconda sqlalchemy
    

    It is also possible to install SQLAlchemy from below source code −

    python setup.py install
    

    SQLAlchemy is designed to operate with a DBAPI implementation built for a particular database. It uses dialect system to communicate with various types of DBAPI implementations and databases. All dialects require that an appropriate DBAPI driver is installed.

    The following are the dialects included −

    • Firebird
    • Microsoft SQL Server
    • MySQL
    • Oracle
    • PostgreSQL
    • SQLite
    • Sybase

    To check if SQLAlchemy is properly installed and to know its version, enter the following command in the Python prompt −

    >>> import sqlalchemy
    >>>sqlalchemy.__version__
    ''1.2.7''
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Expression Language nhận dự án làm có lương

    SQLAlchemy Core – Expression Language



    SQLAlchemy core includes SQL rendering engine, DBAPI integration, transaction integration, and schema description services. SQLAlchemy core uses SQL Expression Language that provides a schema-centric usage paradigm whereas SQLAlchemy ORM is a domain-centric mode of usage.

    The SQL Expression Language presents a system of representing relational database structures and expressions using Python constructs. It presents a system of representing the primitive constructs of the relational database directly without opinion, which is in contrast to ORM that presents a high level and abstracted pattern of usage, which itself is an example of applied usage of the Expression Language.

    Expression Language is one of the core components of SQLAlchemy. It allows the programmer to specify SQL statements in Python code and use it directly in more complex queries. Expression language is independent of backend and comprehensively covers every aspect of raw SQL. It is closer to raw SQL than any other component in SQLAlchemy.

    Expression Language represents the primitive constructs of the relational database directly. Because the ORM is based on top of Expression language, a typical Python database application may have overlapped use of both. The application may use expression language alone, though it has to define its own system of translating application concepts into individual database queries.

    Statements of Expression language will be translated into corresponding raw SQL queries by SQLAlchemy engine. We shall now learn how to create the engine and execute various SQL queries with its help.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí SQLAlchemy – Home nhận dự án làm có lương

    SQLAlchemy Tutorial

    SQLAlchemy Tutorial







    SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license. SQLAlchemy is famous for its object-relational mapper (ORM), using which classes can be mapped to the database, thereby allowing the object model and database schema to develop in a cleanly decoupled way from the beginning.

    Audience

    This tutorial is designed for all those Python programmers who would like to understand the ORM framework with SQLAlchemy and its API.

    Prerequisites

    Before you start proceeding with this tutorial, we assume you have a good understanding of the Python programming language. A basic understanding of relational databases, DB-API, and SQL is desired to understand this tutorial.

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Creating Table nhận dự án làm có lương

    SQLAlchemy Core – Creating Table



    Let us now discuss how to use the create table function.

    The SQL Expression Language constructs its expressions against table columns. SQLAlchemy Column object represents a column in a database table which is in turn represented by a Tableobject. Metadata contains definitions of tables and associated objects such as index, view, triggers, etc.

    Hence an object of MetaData class from SQLAlchemy Metadata is a collection of Table objects and their associated schema constructs. It holds a collection of Table objects as well as an optional binding to an Engine or Connection.

    from sqlalchemy import MetaData
    meta = MetaData()
    

    Constructor of MetaData class can have bind and schema parameters which are by default None.

    Next, we define our tables all within above metadata catalog, using the Table construct, which resembles regular SQL CREATE TABLE statement.

    An object of Table class represents corresponding table in a database. The constructor takes the following parameters −

    Name Name of the table
    Metadata MetaData object that will hold this table
    Column(s) One or more objects of column class

    Column object represents a column in a database table. Constructor takes name, type and other parameters such as primary_key, autoincrement and other constraints.

    SQLAlchemy matches Python data to the best possible generic column data types defined in it. Some of the generic data types are −

    • BigInteger
    • Boolean
    • Date
    • DateTime
    • Float
    • Integer
    • Numeric
    • SmallInteger
    • String
    • Text
    • Time

    To create a students table in college database, use the following snippet −

    from sqlalchemy import Table, Column, Integer, String, MetaData
    meta = MetaData()
    
    students = Table(
       ''students'', meta,
       Column(''id'', Integer, primary_key = True),
       Column(''name'', String),
       Column(''lastname'', String),
    )
    

    The create_all() function uses the engine object to create all the defined table objects and stores the information in metadata.

    meta.create_all(engine)
    

    Complete code is given below which will create a SQLite database college.db with a students table in it.

    from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
    engine = create_engine(''sqlite:///college.db'', echo = True)
    meta = MetaData()
    
    students = Table(
       ''students'', meta,
       Column(''id'', Integer, primary_key = True),
       Column(''name'', String),
       Column(''lastname'', String),
    )
    meta.create_all(engine)
    

    Because echo attribute of create_engine() function is set to True, the console will display the actual SQL query for table creation as follows −

    CREATE TABLE students (
       id INTEGER NOT NULL,
       name VARCHAR,
       lastname VARCHAR,
       PRIMARY KEY (id)
    )
    

    The college.db will be created in current working directory. To check if the students table is created, you can open the database using any SQLite GUI tool such as SQLiteStudio.

    The below image shows the students table that is created in the database −

    Students Table

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Using Textual SQL nhận dự án làm có lương

    SQLAlchemy Core – Using Textual SQL



    SQLAlchemy lets you just use strings, for those cases when the SQL is already known and there isn’t a strong need for the statement to support dynamic features. The text() construct is used to compose a textual statement that is passed to the database mostly unchanged.

    It constructs a new TextClause, representing a textual SQL string directly as shown in the below code −

    from sqlalchemy import text
    t = text("SELECT * FROM students")
    result = connection.execute(t)
    

    The advantages text() provides over a plain string are −

    • backend-neutral support for bind parameters
    • per-statement execution options
    • result-column typing behaviour

    The text()function requires Bound parameters in the named colon format. They are consistent regardless of database backend. To send values in for the parameters, we pass them into the execute() method as additional arguments.

    The following example uses bound parameters in textual SQL −

    from sqlalchemy.sql import text
    s = text("select students.name, students.lastname from students where students.name between :x and :y")
    conn.execute(s, x = ''A'', y = ''L'').fetchall()
    

    The text() function constructs SQL expression as follows −

    select students.name, students.lastname from students where students.name between ? and ?
    

    The values of x = ’A’ and y = ’L’ are passed as parameters. Result is a list of rows with names between ‘A’ and ‘L’ −

    [(''Komal'', ''Bhandari''), (''Abdul'', ''Sattar'')]
    

    The text() construct supports pre-established bound values using the TextClause.bindparams() method. The parameters can also be explicitly typed as follows −

    stmt = text("SELECT * FROM students WHERE students.name BETWEEN :x AND :y")
    
    stmt = stmt.bindparams(
       bindparam("x", type_= String),
       bindparam("y", type_= String)
    )
    
    result = conn.execute(stmt, {"x": "A", "y": "L"})
    
    The text() function also be produces fragments of SQL within a select() object that
    accepts text() objects as an arguments. The “geometry” of the statement is provided by
    select() construct , and the textual content by text() construct. We can build a statement
    without the need to refer to any pre-established Table metadata.
    
    from sqlalchemy.sql import select
    s = select([text("students.name, students.lastname from students")]).where(text("students.name between :x and :y"))
    conn.execute(s, x = ''A'', y = ''L'').fetchall()
    

    You can also use and_() function to combine multiple conditions in WHERE clause created with the help of text() function.

    from sqlalchemy import and_
    from sqlalchemy.sql import select
    s = select([text("* from students")])
    .where(
       and_(
          text("students.name between :x and :y"),
          text("students.id>2")
       )
    )
    conn.execute(s, x = ''A'', y = ''L'').fetchall()
    

    Above code fetches rows with names between “A” and “L” with id greater than 2. The output of the code is given below −

    [(3, ''Komal'', ''Bhandari''), (4, ''Abdul'', ''Sattar'')]
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Selecting Rows nhận dự án làm có lương

    SQLAlchemy Core – Selecting Rows



    In this chapter, we will discuss about the concept of selecting rows in the table object.

    The select() method of table object enables us to construct SELECT expression.

    s = students.select()
    

    The select object translates to SELECT query by str(s) function as shown below −

    ''SELECT students.id, students.name, students.lastname FROM students''
    

    We can use this select object as a parameter to execute() method of connection object as shown in the code below −

    result = conn.execute(s)
    

    When the above statement is executed, Python shell echoes following equivalent SQL expression −

    SELECT students.id, students.name, students.lastname
    FROM students
    

    The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method.

    row = result.fetchone()
    

    All selected rows in the table can be printed by a for loop as given below −

    for row in result:
       print (row)
    

    The complete code to print all rows from students table is shown below −

    from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
    engine = create_engine(''sqlite:///college.db'', echo = True)
    meta = MetaData()
    
    students = Table(
       ''students'', meta,
       Column(''id'', Integer, primary_key = True),
       Column(''name'', String),
       Column(''lastname'', String),
    )
    
    s = students.select()
    conn = engine.connect()
    result = conn.execute(s)
    
    for row in result:
       print (row)
    

    The output shown in Python shell is as follows −

    (1, ''Ravi'', ''Kapoor'')
    (2, ''Rajiv'', ''Khanna'')
    (3, ''Komal'', ''Bhandari'')
    (4, ''Abdul'', ''Sattar'')
    (5, ''Priya'', ''Rajhans'')
    

    The WHERE clause of SELECT query can be applied by using Select.where(). For example, if we want to display rows with id >2

    s = students.select().where(students.c.id>2)
    result = conn.execute(s)
    
    for row in result:
       print (row)
    

    Here c attribute is an alias for column. Following output will be displayed on the shell −

    (3, ''Komal'', ''Bhandari'')
    (4, ''Abdul'', ''Sattar'')
    (5, ''Priya'', ''Rajhans'')
    

    Here, we have to note that select object can also be obtained by select() function in sqlalchemy.sql module. The select() function requires the table object as argument.

    from sqlalchemy.sql import select
    s = select([users])
    result = conn.execute(s)
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Connecting to Database nhận dự án làm có lương

    SQLAlchemy Core – Connecting to Database



    In the previous chapter, we have discussed about expression Language in SQLAlchemy. Now let us proceed towards the steps involved in connecting to a database.

    Engine class connects a Pool and Dialect together to provide a source of database connectivity and behavior. An object of Engine class is instantiated using the create_engine() function.

    The create_engine() function takes the database as one argument. The database is not needed to be defined anywhere. The standard calling form has to send the URL as the first positional argument, usually a string that indicates database dialect and connection arguments. Using the code given below, we can create a database.

    >>> from sqlalchemy import create_engine
    >>> engine = create_engine(''sqlite:///college.db'', echo = True)
    

    For a MySQL database, use the below command −

    engine = create_engine("mysql://user:pwd@localhost/college",echo = True)
    

    To specifically mention DB-API to be used for connection, the URL string takes the form as follows −

    dialect[+driver]://user:password@host/dbname
    

    For example, if you are using PyMySQL driver with MySQL, use the following command −

    mysql+pymysql://<username>:<password>@<host>/<dbname>
    

    The echo flag is a shortcut to set up SQLAlchemy logging, which is accomplished via Python’s standard logging module. In the subsequent chapters, we will learn all the generated SQLs. To hide the verbose output, set echo attribute to None. Other arguments to create_engine() function may be dialect specific.

    The create_engine() function returns an Engine object. Some important methods of Engine class are −

    Sr.No. Method & Description
    1

    connect()

    Returns connection object

    2

    execute()

    Executes a SQL statement construct

    3

    begin()

    Returns a context manager delivering a Connection with a Transaction established. Upon successful operation, the Transaction is committed, else it is rolled back

    4

    dispose()

    Disposes of the connection pool used by the Engine

    5

    driver()

    Driver name of the Dialect in use by the Engine

    6

    table_names()

    Returns a list of all table names available in the database

    7

    transaction()

    Executes the given function within a transaction boundary


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí SQL Expressions nhận dự án làm có lương

    SQLAlchemy Core – SQL Expressions



    In this chapter, we will briefly focus on the SQL Expressions and their functions.

    SQL expressions are constructed using corresponding methods relative to target table object. For example, the INSERT statement is created by executing insert() method as follows −

    ins = students.insert()
    

    The result of above method is an insert object that can be verified by using str() function. The below code inserts details like student id, name, lastname.

    ''INSERT INTO students (id, name, lastname) VALUES (:id, :name, :lastname)''
    

    It is possible to insert value in a specific field by values() method to insert object. The code for the same is given below −

    >>> ins = users.insert().values(name = ''Karan'')
    >>> str(ins)
    ''INSERT INTO users (name) VALUES (:name)''
    

    The SQL echoed on Python console doesn’t show the actual value (‘Karan’ in this case). Instead, SQLALchemy generates a bind parameter which is visible in compiled form of the statement.

    ins.compile().params
    {''name'': ''Karan''}
    

    Similarly, methods like update(), delete() and select() create UPDATE, DELETE and SELECT expressions respectively. We shall learn about them in later chapters.


    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Executing Expression nhận dự án làm có lương

    SQLAlchemy Core – Executing Expression



    In the previous chapter, we have learnt SQL Expressions. In this chapter, we shall look into the execution of these expressions.

    In order to execute the resulting SQL expressions, we have to obtain a connection object representing an actively checked out DBAPI connection resource and then feed the expression object as shown in the code below.

    conn = engine.connect()
    

    The following insert() object can be used for execute() method −

    ins = students.insert().values(name = ''Ravi'', lastname = ''Kapoor'')
    result = conn.execute(ins)
    

    The console shows the result of execution of SQL expression as below −

    INSERT INTO students (name, lastname) VALUES (?, ?)
    (''Ravi'', ''Kapoor'')
    COMMIT
    

    Following is the entire snippet that shows the execution of INSERT query using SQLAlchemy’s core technique −

    from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
    engine = create_engine(''sqlite:///college.db'', echo = True)
    meta = MetaData()
    
    students = Table(
       ''students'', meta,
       Column(''id'', Integer, primary_key = True),
       Column(''name'', String),
       Column(''lastname'', String),
    )
    
    ins = students.insert()
    ins = students.insert().values(name = ''Ravi'', lastname = ''Kapoor'')
    conn = engine.connect()
    result = conn.execute(ins)
    

    The result can be verified by opening the database using SQLite Studio as shown in the below screenshot −

    SQLite Studio

    The result variable is known as a object. It is analogous to the DBAPI cursor object. We can acquire information about the primary key values which were generated from our statement using ResultProxy.inserted_primary_key as shown below −

    result.inserted_primary_key
    [1]
    

    To issue many inserts using DBAPI’s execute many() method, we can send in a list of dictionaries each containing a distinct set of parameters to be inserted.

    conn.execute(students.insert(), [
       {''name'':''Rajiv'', ''lastname'' : ''Khanna''},
       {''name'':''Komal'',''lastname'' : ''Bhandari''},
       {''name'':''Abdul'',''lastname'' : ''Sattar''},
       {''name'':''Priya'',''lastname'' : ''Rajhans''},
    ])
    

    This is reflected in the data view of the table as shown in the following figure −

     Table Data View

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí Using Aliases nhận dự án làm có lương

    SQLAlchemy Core – Using Aliases



    The alias in SQL corresponds to a “renamed” version of a table or SELECT statement, which occurs anytime you say “SELECT * FROM table1 AS a”. The AS creates a new name for the table. Aliases allow any table or subquery to be referenced by a unique name.

    In case of a table, this allows the same table to be named in the FROM clause multiple times. It provides a parent name for the columns represented by the statement, allowing them to be referenced relative to this name.

    In SQLAlchemy, any Table, select() construct, or other selectable object can be turned into an alias using the From Clause.alias() method, which produces an Alias construct. The alias() function in sqlalchemy.sql module represents an alias, as typically applied to any table or sub-select within a SQL statement using the AS keyword.

    from sqlalchemy.sql import alias
    st = students.alias("a")
    

    This alias can now be used in select() construct to refer to students table −

    s = select([st]).where(st.c.id>2)
    

    This translates to SQL expression as follows −

    SELECT a.id, a.name, a.lastname FROM students AS a WHERE a.id > 2
    

    We can now execute this SQL query with the execute() method of connection object. The complete code is as follows −

    from sqlalchemy.sql import alias, select
    st = students.alias("a")
    s = select([st]).where(st.c.id > 2)
    conn.execute(s).fetchall()
    

    When above line of code is executed, it generates the following output −

    [(3, ''Komal'', ''Bhandari''), (4, ''Abdul'', ''Sattar''), (5, ''Priya'', ''Rajhans'')]
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc