-
需要用到
pandas、SQLAlchemy包。此外,本实验采用PostgreSQL,需要psycopg2包。在Anaconda环境下安装即可:conda install pandas sqlalchemy psycopg2
-
启动数据库
localhost/sqlzoo。 -
通过
sqlalchemy形式的DB URI连接sqlzoo数据库。
-
pandas,SQLAlchemyare required. Also, PostgreSQL is applied in this case, sopsycopg2is also required. You can use conda/pip to install them.conda install pandas sqlalchemy psycopg2
-
Boot the database
localhost/sqlzoo -
Connect to the sqlzoo database by
sqlalchemycompatible DB URI
macOS Mojave不能安装
mysql-python,只能装pymysql。因此,如果用MySQL的话,URI要写成mysql+pymysq://...。 macOS Majave cannot installmysql-pythoncurrently. Installpymysqlinsstead and write the URI in the form ofmysql+pymysq://....
import getpass
import psycopg2
from sqlalchemy import create_engine
import pandas as pd
pwd = getpass.getpass()
engine = create_engine(
'postgresql+psycopg2://postgres:%s@localhost/sqlzoo' % (pwd))可以针对pandas做一些设置,比如将长度显示行数的上限为10。 Do some configuration after connection, e.g., set the display upper limit of rows = 10.
pd.set_option('display.max_rows', 10)pd.read_sql_table('world', engine)| name | continent | area | population | gdp | capital | tld | flag |
|---|---|---|---|---|---|---|---|
| Afghanistan | Asia | 652230.0 | 32225560.0 | 21992000000.0 | Kabul | .af | //upload.wikimedia.org/wikipedia/commons/9/9a/Flag_of_Afghanistan.svg |
| Albania | Europe | 28748.0 | 2845955.0 | 13039000000.0 | Tirana | .al | //upload.wikimedia.org/wikipedia/commons/3/36/Flag_of_Albania.svg |
| Algeria | Africa | 2381741.0 | 43000000.0 | 167555000000.0 | Algiers | .dz | //upload.wikimedia.org/wikipedia/commons/7/77/Flag_of_Algeria.svg |
| Andorra | Europe | 468.0 | 77543.0 | 3278000000.0 | Andorra la Vella | .ad | //upload.wikimedia.org/wikipedia/commons/1/19/Flag_of_Andorra.svg |
| Angola | Africa | 1246700.0 | 31127674.0 | 126505000000.0 | Luanda | .ao | //upload.wikimedia.org/wikipedia/commons/9/9d/Flag_of_Angola.svg |
| Antigua and Barbuda | Caribbean | 442.0 | 96453.0 | 1248000000.0 | St. John's | .ag | //upload.wikimedia.org/wikipedia/commons/8/89/Flag_of_Antigua_and_Barbuda.svg |
| Argentina | South America | 2780400.0 | 44938712.0 | 637486000000.0 | Buenos Aires | .ar | //upload.wikimedia.org/wikipedia/commons/1/1a/Flag_of_Argentina.svg |
| Armenia | Eurasia | 29743.0 | 2957500.0 | 11536000000.0 | Yerevan | .am | //upload.wikimedia.org/wikipedia/commons/2/2f/Flag_of_Armenia.svg |
| Australia | Oceania | 7692024.0 | 25690023.0 | 1408675000000.0 | Canberra | .au | //upload.wikimedia.org/wikipedia/commons/8/88/Flag_of_Australia_%28converted%29.svg |
| Austria | Europe | 83871.0 | 8902600.0 | 416835000000.0 | Vienna | .at | //upload.wikimedia.org/wikipedia/commons/4/41/Flag_of_Austria.svg |
195 rows, truncated to displaylimit of 10
world表一共195行,但由于pandas全局设置了display.max_rows,默认只显示了前10行。 There are a total of 195 rows in world. But only 10 rows are displayed due to the global setting SqlMagic.displaylimit.
| Title | Link |
|---|---|
| 00 SELECT basics | GitHub Gitee |
| 01 SELECT name | GitHub Gitee |
| 02 SELECT from World | GitHub Gitee |
| 03 SELECT from Nobel | GitHub Gitee |
| 04 SELECT within SELECT | GitHub Gitee |
| 05 SUM and COUNT | GitHub Gitee |
| 06 JOIN | GitHub Gitee |
| 07 More JOIN operations | GitHub Gitee |
| 08 Using Null | GitHub Gitee |
| 08+ Numeric Examples | GitHub Gitee |
| 09- Window function | GitHub Gitee |
| 09 Self join | GitHub Gitee |
| 09+ COVID-19 | GitHub Gitee |
| Title | Link |
|---|---|
| 11 Module Feedback | GitHub Gitee |
| 12-1 Helpdesk - Easy | GitHub Gitee |
| 12-2 Helpdesk - Medium | GitHub Gitee |
| 12-3 Helpdesk - Hard | GitHub Gitee |
| 13-1 Guest House - Easy | GitHub Gitee |
| 13-2 Guest House - Medium | GitHub Gitee |
| 13-3 Guest House - Hard | GitHub Gitee |
| 14-1 AdventureWorks - Easy | GitHub Gitee |
| 14-2 AdventureWorks - Medium | GitHub Gitee |
| 14-3 AdventureWorks - Hard | GitHub Gitee |
| 14-4 AdventureWorks - Resit | GitHub Gitee |
| 15-1 University Timetables - Easy | GitHub Gitee |
| 15-2 University Timetables - Medium | GitHub Gitee |
| 15-3 University Timetables - Hard | GitHub Gitee |
| 15-4 University Timetables - Resit | GitHub Gitee |
| 16-1 Musicians - Easy | GitHub Gitee |
| 16-2 Musicians - Medium | GitHub Gitee |
| 16-3 Musicians - Hard | GitHub Gitee |
| 17-0 Dressmaker - Samples | GitHub Gitee |
| 17-1 Dressmaker - Easy | GitHub Gitee |
| 17-2 Dressmaker - Medium | GitHub Gitee |
| 17-3 Dressmaker - Hard | GitHub Gitee |
| 18-1 Congestion - Easy | GitHub Gitee |
| 18-2 Congestion - Medium | GitHub Gitee |
| 18-3 Congestion - Hard | GitHub Gitee |
| Title | Link |
|---|---|
| 19 White Christmas | GitHub Gitee |