September 2013
Intermediate to advanced
350 pages
9h 38m
English
Now that our database has been created and populated, we can run queries to search for data that meets specified criteria. The general form of a query is as follows:
| | SELECT ColumnName , ... FROM TableName |
The TableName is the name of the table to get the data from and the column names specify which columns to get values from. For example, this query retrieves all the data in the table PopByRegion:
| | >>> cur.execute('SELECT Region, Population FROM PopByRegion') |
Once the database has executed this query for us, we can access the results one record at a time by calling the cursor’s fetchone method, just as we can read one line at a time from a file using readline:
| | >>> cur.fetchone() |
| | ('Central Africa', ... |
Read now
Unlock full access