1

I am newbie to python ,trying with simple programs given below is a program which i tried to get the data from table and displaying it. Installed Python3.4 and mysql.connector 2.0.4, runningin localhost as http://localhost:804/cgi-bin/ex7.py

It's connecting to database but not fetching the data from table

 #!"C:\python34\python.exe"
    import sys
    import mysql.connector
    print("Content-Type: text/html;charset=utf-8")
    print()
    conn = mysql.connector.connect(host='localhost',port='8051',
                                           database='example',
                                           user='root',
                                           password='tiger')
   cursor = conn.cursor()
 if conn.is_connected():
        print('Connected to MySQL database')
    cursor.execute(""" SELECT * FROM album """)
    for row in cursor:
        print (row[1])

It's giving output as : Connected to MySQL database

not printing data from table Please suggest where went wrong

2
  • you are missing cursor = conn.cursor() part. But you should not really use mysql connector. Either you go with sqlachemy or pip install MySQL-python. Using MySQL-python is really simple. On this url you have example zetcode.com/db/mysqlpython (search page for "In the first example, we will get the version of the MySQL database.") Commented Jun 2, 2015 at 12:33
  • with including cursor = conn.cursor() ,it wont work.so many are using mysql connector.so i installed and trying with this.I have to use this only Commented Jun 2, 2015 at 12:45

2 Answers 2

4

you missed this part i think

conn = mysql.connector.MySQLConnection(host='localhost',port='8051',
                                       database='example',
                                       user='root',
                                       password='tiger')
cursor = conn.cursor()
if conn.is_connected():
    print('Connected to MySQL database')
cursor.execute(""" SELECT * FROM album """)
# fetch all of the rows from the query
data = cursor.fetchall ()

# print the rows
for row in data :
    print row[1]
Sign up to request clarification or add additional context in comments.

1 Comment

actually same output .If i print(cursor), it's giving as '' MySQLCursor: SELECT * FROM album" ,Is any problem in for loop
1
import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost', database='example', user='root', password='tiger')
    sql_select_Query = "SELECT * FROM album"
    cursor = connection.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()
    print("Total number of rows in album is: ", cursor.rowcount)
    print("\nPrinting each album record")
    for row in records:
        print("Id = ", row[0], )
        print("Name = ", row[1], "\n")
        # print("Price  = ", row[2])
        # print("Purchase date  = ", row[3], "\n")
except Error as e:
    print("Error reading data from MySQL table", e)
finally:
if connection.is_connected():
    connection.close()
    cursor.close()
    print("MySQL connection is closed")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.