Lino Website

Documentation · Modules · adamo · Examples

query1.py

This example illustrates how to query an Adamo database and use a report to see the result on the console.

# list of all cities and number of inhabitants
from lino.apps.contacts.contacts_demo import startup
from lino.apps.contacts.contacts_tables import City
from lino.adamo.filters import NotEmpty

sess=startup()
qry=sess.query(City,"nation name inhabitants", orderBy="inhabitants")
qry.getColumnByName("inhabitants").addFilter(NotEmpty)
qry.show(width=50)

print
print qry.getSqlSelect()

demo.startup() starts a console Session, creates a demo Database on a temporary Connection, runs a Populator to fill the database with demo data. It then returns the Session instance which gives access to all this.

The Session instance has a query() method which returns a Query instance, in this example it is a query with leadTable Cities, three columns, and sorted by number of inhabitants.

Then we call Query.report() to print a DataReport whose total width we want to be 50 chars. (Default width on a console would be 79 chars.

Here is the output:

Cities where 'inhabitants' not empty
====================================
nation             |name               |inhabitan
                   |                   |ts       
-------------------+-------------------+---------
Estonia            |Ruhnu              |58       
Estonia            |Vigala             |1858     
Estonia            |Kilingi-Nõmme      |2490     
Belgium            |Raeren             |9933     
Belgium            |Kelmis             |10175    
Belgium            |Eupen              |17872    
Estonia            |Rakvere            |18096    
Estonia            |Viljandi           |20756    
Estonia            |Pärnu              |52000    
Belgium            |Verviers           |52739    
Estonia            |Kohtla-Järve       |70800    
Estonia            |Narva              |80300    
Belgium            |Mons               |90992    
Estonia            |Tartu              |109100   
Belgium            |Brugge             |116848   
Belgium            |Liège              |185608   
Belgium            |Charleroi          |200983   
Estonia            |Tallinn            |442000   
Belgium            |Bruxelles          |1004239  

SELECT nation_id, id, name, inhabitants FROM Cities WHERE inhabitants NOT NULL ORDER BY inhabitants

Refering articles:

Copyright 2001-2007 Luc Saffre.
http://lino.saffre-rumma.ee
Generated 2007-06-07 16:23:39