In this chapter, we are able to briefly attention on the SQL Expressions and their features.
SQL expressions are built the usage of corresponding methods relative to goal table object. For example, the INSERT statement is created by means of executing insert() approach as follows −
ins = students.insert()
The result of above method is an insert item that may be validated by using the usage of str() characteristic. The beneath code inserts details like scholar identity, call, lastname.
'INSERT INTO students (id, name, lastname) VALUES (:id, :name, :lastname)'
It is feasible to insert value in a specific field by values() technique to insert item. 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 real value (‘Karan’ in this situation). Instead, SQLALchemy generates a bind parameter that's visible in compiled shape of the assertion.
ins.compile().params
{'name': 'Karan'}
Similarly, methods like replace(), delete() and pick out() create UPDATE, DELETE and SELECT expressions respectively. We shall learn about them in later chapters.