import pywikibot
import pandas as pd
import pymysql  # We will use pymysql to connect to the database
import os
host = os.environ['MYSQL_HOST']
user = os.environ['MYSQL_USERNAME']
password = os.environ['MYSQL_PASSWORD']
conn = pymysql.connect(
    host=host,
    user=user,
    password=password
)
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        SELECT *
        FROM revision JOIN page 
            ON page.page_id = revision.rev_page
        WHERE page.page_namespace = 0 AND page.page_title = 'India' 
        ORDER BY revision.rev_timestamp DESC
        LIMIT 1
    """)
    print(cur.fetchall())
((782856612, 14533, 0, b'/* Economy */ Updated per-capita GDP estimates out to 2020', 31123481, b'Lkolbly', b'20170529164152', 0, 0, 187141, 782494271, b'oeikhljmtek5rjgpqbv66d2sohmgkbf', None, None, 14533, 0, b'India', b'', 6994, 0, 0, 0.245421638940854, b'20170529164152', b'20170529164153', 782856612, 187141, b'wikitext'),)
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        SELECT pp_value
        FROM page_props
        LIMIT 500
    """)
    print(cur.fetchall())
((b'Anarchy-symbol.svg',), (b'1',), (b'Q6199',), (b'Autism-stacking-cans_2nd_edit.jpg',), (b'1',), (b'Q38404',), (b'Albedo-e_hg.svg',), (b'Q101038',), (b'A_cursiva.gif',), (b'Q9659',), (b'Flag_of_Alabama.svg',), (b'Q173',), (b'Achilles',), (b'Achilles_departure_Eretria_Painter_CdM_Paris_851.jpg',), (b'Q41746',), (b'Lincoln, Abraham',), (b'Abraham_Lincoln_O-77_matte_collodion_print.jpg',), (b'1',), (b'Q91',), (b'Aristotle_Altemps_Inv8575.jpg',), (b'Q868',), (b'American In Paris, An',), (b'<i>An American in Paris</i>',), (b'Q853997',), (b'Academy Award For Best Art Direction',), (b'Q277751',), (b'Casey_Affleck_at_the_Manchester_by_the_Sea_premiere_(30199719155)_(cropped).jpg',), (b'Q19020',), (b'<i>Actrius</i>',), (b'Actrius_film_poster.jpg',), (b'Q2823770',), (b'Animalia (Book)',), (b'<i>Animalia</i> (book)',), (b'Animalia_(book_cover).jpg',), (b'Q4765006',), (b'Q119395',), (b"Belisaire_demandant_l'aumone_Jacques-Louis_David.jpg",), (b'Q167323',), (b'Rand, Ayn',), (b'Ayn_Rand.jpg',), (b'1',), (b'Q132524',), (b'Connes, Alain',), (b'Alain_Connes.jpg',), (b'Q313590',), (b'Dwan, Allan',), (b'Allan_Dwan_1920.jpg',), (b'Q959677',), (b'Flag_of_Algeria.svg',), (b'Q262',), (b'Atlas Shrugged Characters, List of',), (b'List of <i>Atlas Shrugged</i> characters',), (b'Q3253031',), (b'Q23404',), (b'Agricultural Science',), (b'Maler_der_Grabkammer_des_Sennudem_001.jpg',), (b'Q3606845',), (b'Alchemy_of_Happiness.png',), (b'Q39689',), (b'',), (b'Q226391',), (b'JohannesVermeer-TheAstronomer(1668).jpg',), (b'Q11063',), (b'',), (b'',), (b'US-ASCII_code_chart.png',), (b'Q8815',), (b'',), (b'Q218287',), (b'Animation',), (b'Animexample3edit.png',), (b'Q11425',), (b'Apollo_of_the_Belvedere.jpg',), (b'Q37340',), (b'Agassi, Andre',), (b'Andre_Agassi_Indian_Wells_2006.jpg',), (b'Q7407',), (b'Austroasiatic Languages',), (b'Austroasiatic-en.svg',), (b'Q33199',), (b'Afroasiatic Languages',), (b'Hamito-Semitic_languages.jpg',), (b'Q25268',), (b'Flag_of_Andorra.svg',), (b'Q228',), (b'Arithmetic Mean',), (b'Q19033',), (b'American_Football_Conference_logo.svg',), (b'Q276530',), (b'<i>Animal Farm</i>',), (b'Animal_Farm_-_1st_edition.jpg',), (b'Q1396889',), (b'Amphibians.png',), (b'1',), (b'Q10908',), (b'Flag_of_Alaska.svg',), (b'Q797',), (b'Chvojnica_hills_near_Unin.jpg',), (b'Q11451',), (b'Huxley, Aldous',), (b'Aldous_Huxley_psychical_researcher.png',), (b'Q81447',), (b'',), (b'Q154614',), (b'',), (b'Q319411',), (b'NSW_seabed_1.JPG',), (b'Q37868',), (b'Analysis Of Variance',), (b'Biologist_and_statistician_Ronald_Fisher.jpg',), (b'Q42297',), (b'Methane-2D-stereo.svg',), (b'Q41581',), (b'D.C._Court_of_Appeals.JPG',), (b'Q4781008',), (b'Q1920566',), (b'Appellate Court',), (b'Q4959031',), (b'Q865927',), (b'5_America,_the_Beautiful.png',), (b'Q463167',), (b'Assistive Technology',), (b'Hoergeraet_analog_050609.jpg',), (b'Q688498',), (b'Boulier1.JPG',), (b'Q12806',), (b'Zn_reaction_with_HCl.JPG',), (b'Q11158',), (b'Bitumen.jpg',), (b'Q167510',), (b'ANSI_logo.svg',), (b'Q180003',), (b'',), (b'Q651343',), (b'Apollo_11_first_step.jpg',), (b'1',), (b'Q43653',), (b'Apollo 08',), (b'NASA-Apollo8-Dec24-Earthrise.jpg',), (b'1',), (b'Q184201',), (b'Astronaut-EVA.jpg',), (b'Q11631',), (b'Modest Proposal, A',), (b'<i>A Modest Proposal</i>',), (b'A_Modest_Proposal_1729_Cover.jpg',), (b'Q264143',), (b'Lithium_paraffin.jpg',), (b'1',), (b'Q19557',), (b'Orbis_eruditi_literatura_\xc3\xa0_charactere_Samaritico_deducta_1689.jpg',), (b'Q9779',), (b'Atomic Number',), (b'Atomic_number_depiction.jpg',), (b'Q23809',), (b'An_\xc3\xa9corch\xc3\xa9_figure_(life-size),_lying_prone_on_a_table_Wellcome_L0020561.jpg',), (b'1',), (b'Q514',), (b'Q2000864',), (b'Tarkovsky, Andrei',), (b'Andrei_Tarkovsky.jpg',), (b'Q853',), (b'Alice_05a-1116x1492.jpg',), (b'Q1140419',), (b'',), (b'Q227922',), (b'Porc_formiguer.JPG',), (b'Q46212',), (b'Proteles_cristatus1.jpg',), (b'Q185295',), (b'Adobe_wall_(detail)_1.jpg',), (b'Q183496',), (b'Kjelkeoverisen.jpg',), (b'Q1436734',), (b'Asia_(orthographic_projection).svg',), (b'Q48',), (b'Flag_of_Aruba.svg',), (b'Q21203',), (b'Articles Of Confederation',), (b'Articles_page1.jpg',), (b'Q493620',), (b'',), (b'Q16253915',), (b'Q5258251',), (b'Atlantic_Ocean_location_map.svg',), (b'Q97',), (b'Schopenhauer, Arthur',), (b'Schopenhauer.jpg',), (b'Q38193',), (b'Flag_of_Angola.svg',), (b'Q916',), (b'Demographics Of Angola',), (b'Angola_demography.png',), (b'Q2402452',), (b'Coat_of_arms_of_Angola.svg',), (b'Q1155473',), (b'Economy Of Angola',), (b'Marginal_Avenida_4_de_Fevreiro_Luanda_March_2013_14.JPG',), (b'Q1064113',), (b'Transport In Angola',), (b'Q3997810',), (b'Flag_of_Angola.svg',), (b'Q542209',), (b'Foreign Relations Of Angola',), (b'Coat_of_arms_of_Angola.svg',), (b'Q2687348',), (b'Johnston, Albert Sidney',), (b'ASJohnston.jpg',), (b'Q361140',), (b'Android (Robot)',), (b'Q181787',), (b'Flag_of_Alberta.svg',), (b'Q1951',), (b'List Of Anthropologists',), (b'',), (b'Q1279970',), (b'Georgia_Aquarium_-_Giant_Grouper.jpg',), (b'Q127282',), (b'Einstein, Albert',), (b'Einstein_1921_by_F_Schmutzer_-_restoration.jpg',), (b'1',), (b'Q937',), (b'Flag_of_Afghanistan.svg',), (b'Q889',), (b'Flag_of_Albania.svg',), (b'Q222',), (b'Allah3.svg',), (b'1',), (b'Q234801',), (b'<i>Algorithms</i> (journal)',), (b'Q4724371',), (b'Flag_of_Azerbaijan.svg',), (b'1',), (b'Q227',), (b'Amateur Astronomy',), (b'Astronomy_Amateur_3_V2.jpg',), (b'Q560575',), (b'Shihonage.jpg',), (b'1',), (b'Q43114',), (b'Art-portrait-collage_2.jpg',), (b'Q735',), (b'Itagnostus_interstrictus_(White,_1874)_-_8_mm_1.JPG',), (b'Q134109',), (b'Q8452',), (b'Abstract (Law)',), (b'Q4669920',), (b'AmericanRevolutionaryWarMon.jpg',), (b'Q40949',), (b'Amperemeter_hg.jpg',), (b'Q25272',), (b'Euclid_flowchart.svg',), (b'Q8366',), (b'Annual Plant',), (b'Doperwt_rijserwt_peulen_Pisum_sativum.jpg',), (b'Q192691',), (b'Q1425109',), (b'',), (b'Q102520',), (b'Q518968',), (b'Alexander the Great',), (b'Alexander_the_Great_mosaic.jpg',), (b'1',), (b'Q8409',), (b'Korzybski, Alfred',), (b'Alfred_Korzybski.jpg',), (b'Q333402',), (b'Asteroids (Video Game)',), (b'<i>Asteroids</i> (video game)',), (b'Asteroids-arcadegame.jpg',), (b'1',), (b'Q748890',), (b'Asperge_in_bloei_Asparagus_officinalis.jpg',), (b'Q26229',), (b'AlismaPlant1.jpg',), (b'Q27341',), (b'Umbella.jpg',), (b'Q21138',), (b'A_sunflower.jpg',), (b'Q21730',), (b'(253)_mathilde.jpg',), (b'Q3863',), (b'Q504275',), (b"Vasil_Levski's_Affidavit.jpg",), (b'Q454263',), (b'Aries (Constellation)',), (b'Aries_IAU.svg',), (b'1',), (b'Q10584',), (b'Aquarius (Constellation)',), (b'Aquarius_IAU.svg',), (b'Q10576',), (b'Anime_eye.svg',), (b'Q1107',), (b'',), (b'Q747726',), (b'Ankara_2016_genel_kolaj\xc4\xb1.jpg',), (b'Q3640',), (b'Arabic Language',), (b'Dispersi\xc3\xb3n_lengua_\xc3\xa1rabe.png',), (b'Q13955',), (b'Hitchcock, Alfred',), (b'Hitchcock,_Alfred_02.jpg',), (b'Q7374',), (b'01-COBRA-SUCURI-3M-WAGNER-MEIER_MG_2458.JPG',), (b'Q3306164',), (b'',), (b'',), (b'Altaic Languages',), (b'Lenguas_altaicas.png',), (b'Q37845',), (b'Q306626',), (b'',), (b'',), (b'Axiom Of Choice',), (b'Axiome_du_choix.png',), (b'Q179692',), (b'Attila The Hun',), (b'Eugene_Ferdinand_Victor_Delacroix_Attila_fragment.jpg',), (b'Q36724',), (b'Aegean_Sea_map.png',), (b'Q34575',), (b'Clockwork Orange, A',), (b'<i>A Clockwork Orange</i> (novel)',), (b'Clockwork_orange.jpg',), (b'Q692557',), (b'KeizersgrachtReguliersgrachtAmsterdam.jpg',), (b'Q727',), (b'Clockwork Orange, A',), (b'Museum Of Work',), (b'Strykj\xc3\xa4rnet_Motala_str\xc3\xb6m_Norrk\xc3\xb6ping_april_2005.jpg',), (b'Q6941060',), (b'Audi-Logo_2016.svg',), (b'Q23317',), (b'Collection_of_military_aircraft.jpg',), (b'Q11436',), (b'Nobel, Alfred',), (b'Alfred_Nobel3.jpg',), (b'Q23810',), (b'Bell, Alexander Graham',), (b'Alexander_Graham_Bell.jpg',), (b'Q34286',), (b'Modern-day_Turkey_and_Europe_NASA_modified.png',), (b'Q51614',), (b'Apple_Headquarters_in_Cupertino.jpg',), (b'Q312',), (b'Aberdeenshire_in_Scotland.svg',), (b'Q189912',), (b'Aztlan Underground',), (b'Q4832994',), (b'CivilWarUSAColl.png',), (b'Q8676',), (b'Warhol, Andy',), (b'Andy_Warhol_1975.jpg',), (b'Q5603',), (b'Arslan, Alp',), (b'BnF_Fr232_fol323_Alp_Arslan_Romanus.jpg',), (b'Q200193',), (b'American_Film_Institute_(AFI)_logo.svg',), (b'Q207460',), (b'Kurosawa, Akira',), (b'Akirakurosawa-onthesetof7samurai-1953-page88.jpg',), (b'Q8006',), (b'Egypt.Giza.Sphinx.02.jpg',), (b'1',), (b'Q11768',), (b'AnalogBrothersPromoPhoto.jpg',), (b'Q4751040',), (b'Motor Neurone Disease',), (b'Polio_spinal_diagram.PNG',), (b'Q11707850',), (b'09-Pismo.svg',), (b'Q185087',), (b'09-Pismo.svg',), (b'Q335806',), (b'ABBA_-_TopPop_1974_5.png',), (b'Q18233',), (b'Q2721121',), (b'',), (b'',), (b'Q298627',), (b'Apple_Newton-IMG_0454-cropped.jpg',), (b'Q4147219',), (b'Van Vogt, A. E.',), (b'A._E._van_Vogt,_ca._1963.jpg',), (b'Q315059',), (b'Kournikova, Anna',), (b'Anna_Kournikova-Bagram_Airfield_2009.jpg',), (b'Q131120',), (b'Jakob, Alfons Maria',), (b'Alfons_Maria_Jakob.JPG',), (b'Q63336',), (b'1',), (b'Q288928',), (b'Argon_discharge_tube.jpg',), (b'1',), (b'Q696',), (b'Arsen_1a.jpg',), (b'Q871',), (b'Antimony-4.jpg',), (b'1',), (b'Q1099',), (b'Actinium.jpg',), (b'1',), (b'Q1121',), (b'Americium_microscope.jpg',), (b'1',), (b'Q1872',), (b'1',), (b'Q999',), (b'Helium_atom_QM.svg',), (b'1',), (b'Q9121',), (b'Arable Land',), (b'040719_172_dorset_marnhull2.jpg',), (b'Q843920',), (b'Aluminium-4.jpg',), (b'Q663',), (b'Q379404',), (b'Canterbury-cathedral-wyrdlight.jpg',), (b'Q193312',), (b'Kaijser, Arne',), (b'Q4794599',), (b'Ksamil_Islets.jpg',), (b'Q33837',), (b'Q482980',), (b'Markov, Andrey',), (b'AAMarkov.jpg',), (b'Q176659',), (b'Edvard_Munch_-_The_Scream_-_Google_Art_Project.jpg',), (b'Q1522565',), (b'A._Morison_"Physiognomy_of_mental_diseases",_cases_Wellcome_L0022722_(cropped).jpg',), (b'Q154430',), (b'Milne, A. A.',), (b'Milne-Shadowland-1922.jpg',), (b'Q207036',), (b'Asociacion Alumni',), (b'Alumni_rugby_logo.svg',), (b'Q948693',), (b'Q17736',), (b'Q9887',), (b'Toffler, Alvin',), (b'Alvin_Toffler_02.jpg',), (b'Q297425',), (b'Amazing Spider-Man',), (b'<i>The Amazing Spider-Man</i>',), (b'AmazingSpider-Man1.jpg',), (b'Q944598',), (b'',), (b'Q294600',), (b'Antigua And Barbuda',), (b'Flag_of_Antigua_and_Barbuda.svg',), (b'Q781',), (b'Azincourt_1.jpg',), (b'Q238339',), (b'Speer, Albert',), (b'Bundesarchiv_Bild_146II-277,_Albert_Speer.jpg',), (b'1',), (b'Q60045',), (b'Asteracea_poster_3.jpg',), (b'Q25400',), (b'Umbelliferae-apium-daucus-foeniculum-eryngium-petroselinum.jpg',), (b'Q145794',), (b'Neuron_Hand-tuned.svg',), (b'Q178999',), (b'Aramaic Alphabet',), (b'Q26978',), (b'1',), (b'Q3391185',), (b'Q424242',), (b'Q213373',), (b'1',), (b'Alhazred, Abdul',), (b'Q2068019',), (b'Lovelace, Ada King, Countess Of',), (b'Ada_Lovelace_portrait.jpg',), (b'Q7259',), (b'1',), (b'Derleth, August',), (b'August_Derleth_portrait_in_youth.png',), (b'Q509002',), (b'Mont_Blanc_oct_2004.JPG',), (b'Q1286',), (b'Camus, Albert',), (b'Albert_Camus,_gagnant_de_prix_Nobel,_portrait_en_buste,_pos\xc3\xa9_au_bureau,_faisant_face_\xc3\xa0_gauche,_cigarette_de_tabagisme.jpg',), (b'Q34670',), (b'Christie, Agatha',), (b'Agatha_Christie.png',), (b'Q35064',), (b'Plague, The',), (b'<i>The Plague</i>',), (b'La_Peste.jpg',), (b'Q320372',), (b'Camus, Albert',), (b'Applied Ethics',), (b'Q538733',), (b'Absolute Value',), (b'Khoang_cach_tren_duong_thang_thuc.png',), (b'Q120812',), (b'Analog Signal',))
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        SHOW Tables 
    """)
    print(cur.fetchall())
(('abuse_filter',), ('abuse_filter_action',), ('abuse_filter_log',), ('aft_article_answer',), ('aft_article_answer_text',), ('aft_article_feedback',), ('aft_article_feedback_properties',), ('aft_article_feedback_ratings_rollup',), ('aft_article_feedback_select_rollup',), ('aft_article_field',), ('aft_article_field_group',), ('aft_article_field_option',), ('aft_article_filter_count',), ('aft_article_revision_feedback_ratings_rollup',), ('aft_article_revision_feedback_select_rollup',), ('archive',), ('archive_userindex',), ('article_feedback',), ('article_feedback_pages',), ('article_feedback_properties',), ('article_feedback_ratings',), ('article_feedback_revisions',), ('article_feedback_stats',), ('article_feedback_stats_types',), ('category',), ('categorylinks',), ('change_tag',), ('ep_articles',), ('ep_cas',), ('ep_courses',), ('ep_events',), ('ep_instructors',), ('ep_oas',), ('ep_orgs',), ('ep_revisions',), ('ep_students',), ('ep_users_per_course',), ('externallinks',), ('filearchive',), ('filearchive_userindex',), ('flaggedimages',), ('flaggedpage_config',), ('flaggedpage_pending',), ('flaggedpages',), ('flaggedrevs',), ('flaggedrevs_promote',), ('flaggedrevs_statistics',), ('flaggedrevs_stats',), ('flaggedrevs_stats2',), ('flaggedrevs_tracking',), ('flaggedtemplates',), ('geo_tags',), ('global_block_whitelist',), ('image',), ('imagelinks',), ('interwiki',), ('ipblocks',), ('ipblocks_ipindex',), ('iwlinks',), ('l10n_cache',), ('langlinks',), ('linter',), ('localisation',), ('localisation_file_hash',), ('logging',), ('logging_logindex',), ('logging_userindex',), ('mark_as_helpful',), ('math',), ('module_deps',), ('msg_resource_links',), ('oldimage',), ('oldimage_userindex',), ('ores_classification',), ('ores_model',), ('page',), ('page_assessments',), ('page_assessments_projects',), ('page_props',), ('page_restrictions',), ('pagelinks',), ('pagetriage_log',), ('pagetriage_page',), ('pagetriage_page_tags',), ('pagetriage_tags',), ('pif_edits',), ('povwatch_log',), ('povwatch_subscribers',), ('protected_titles',), ('recentchanges',), ('recentchanges_userindex',), ('redirect',), ('revision',), ('revision_userindex',), ('site_identifiers',), ('site_stats',), ('sites',), ('tag_summary',), ('templatelinks',), ('transcode',), ('updatelog',), ('updates',), ('user',), ('user_former_groups',), ('user_groups',), ('user_properties',), ('user_properties_anon',), ('valid_tag',), ('watchlist_count',), ('wbc_entity_usage',), ('wikilove_image_log',), ('wikilove_log',))
with conn.cursor() as cur:
    cur.execute('use enwiki_p')
    cur.execute("""
        SELECT *
        FROM article_feedback_pages
        LIMIT 1
    """)
    print(cur.fetchall())
ERROR:root:An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line string', (1, 4))

---------------------------------------------------------------------------
InternalError                             Traceback (most recent call last)
<ipython-input-24-12d332f91582> in <module>()
      5         FROM article_feedback_pages
      6         LIMIT 1
----> 7     """)
      8     print(cur.fetchall())

/srv/paws/lib/python3.4/site-packages/pymysql/cursors.py in execute(self, query, args)
    164         query = self.mogrify(query, args)
    165 
--> 166         result = self._query(query)
    167         self._executed = query
    168         return result

/srv/paws/lib/python3.4/site-packages/pymysql/cursors.py in _query(self, q)
    320         conn = self._get_db()
    321         self._last_executed = q
--> 322         conn.query(q)
    323         self._do_get_result()
    324         return self.rowcount

/srv/paws/lib/python3.4/site-packages/pymysql/connections.py in query(self, sql, unbuffered)
    850                 sql = sql.encode(self.encoding, 'surrogateescape')
    851         self._execute_command(COMMAND.COM_QUERY, sql)
--> 852         self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    853         return self._affected_rows
    854 

/srv/paws/lib/python3.4/site-packages/pymysql/connections.py in _read_query_result(self, unbuffered)
   1051         else:
   1052             result = MySQLResult(self)
-> 1053             result.read()
   1054         self._result = result
   1055         if result.server_status is not None:

/srv/paws/lib/python3.4/site-packages/pymysql/connections.py in read(self)
   1334     def read(self):
   1335         try:
-> 1336             first_packet = self.connection._read_packet()
   1337 
   1338             if first_packet.is_ok_packet():

/srv/paws/lib/python3.4/site-packages/pymysql/connections.py in _read_packet(self, packet_type)
   1008 
   1009         packet = packet_type(buff, self.encoding)
-> 1010         packet.check_error()
   1011         return packet
   1012 

/srv/paws/lib/python3.4/site-packages/pymysql/connections.py in check_error(self)
    391             errno = self.read_uint16()
    392             if DEBUG: print("errno =", errno)
--> 393             err.raise_mysql_exception(self._data)
    394 
    395     def dump(self):

/srv/paws/lib/python3.4/site-packages/pymysql/err.py in raise_mysql_exception(data)
    105         errval = data[3:].decode('utf-8', 'replace')
    106     errorclass = error_map.get(errno, InternalError)
--> 107     raise errorclass(errno, errval)

InternalError: (1356, "View 'enwiki_p.article_feedback_pages' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them")