简体   繁体   中英

How to execute simple command using mysql.connector (Python)

I installed the MySQL connector from Oracle, and entered the following commands in Python (currently running Python 2.7.6)

import mysql.connector

cnx=mysql.connector.connect(user='genome',host='genome-mysql.cse.ucsc.edu',database='hg19')
cursor=cnx.cursor()
query=('show tables')
cursor.execute(query)

Nothing happened! I expected to see a list of tables. Why? Incidentally, I tried this as well, with the same result:

query=('SELECT * FROM wgRna')
cursor.execute(query)

I know I have MySQL properly installed on my computer, because when I enter the same commands into the terminal everything is fine. Can someone explain to me what I'm doing wrong in Python?

You never did anything with the selected data; print the rows by iterating over the cursor after executing a query:

query = 'show tables'
cursor.execute(query)
for row in cursor:
    print row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM