import pymysql  # We will use pymysql to connect to the database
import os


    
#Credententials for the MySQL database, along with the name of the host to connect to, are passed in via environment variables. Note that there's a 30minute limit on the query execution times.
host = os.environ['MYSQL_HOST']
user = os.environ['MYSQL_USERNAME']
password = os.environ['MYSQL_PASSWORD']
conn = pymysql.connect(
    host=host,
    user=user,
    password=password
)
#The database names are 'dbname of wiki' followed by '_p'. So English Wikipedia is enwiki_p, commons is commonswiki_p, meta is metawiki_p, wikidata is wikidatawiki_p, Tamil Wikisource is tawikisource_p, etc. You can see the database schema here
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        SHOW Tables
    """)
    print(cur.fetchall())
#The database names are 'dbname of wiki' followed by '_p'. So English Wikipedia is enwiki_p, commons is commonswiki_p, meta is metawiki_p, wikidata is wikidatawiki_p, Tamil Wikisource is tawikisource_p, etc. You can see the database schema here
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        select page_id from page where page_title="Coffee"
    """)
    print(cur.fetchall())
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        select count(*) from revision where rev_page=604727
    """)
    print(cur.fetchall())
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        select rev_id, rev_page, rev_user from revision where rev_deleted!=0 limit 100
    """)
    print(cur.fetchall())
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        select count(*) from revision
    """)
    print(cur.fetchall())