当前位置: 动力学知识库 > 问答 > 编程问答 >

Fetching data from the table using python and mysql.connector

问题描述:

I am using "python3.4" and "Mysql.connector" used to connect database.And accessing the Xampp Mysql ,running the code in local host .

Below given code is what i used to connect databse and fetch data

 #!"C:\python34\python.exe"

import sys

import mysql.connector

print("Content-Type: text/html;charset=utf-8")

print()

/* code to connect datbase*/

conn = mysql.connector.connect(host='localhost',port='8051',

database='test',

user='root',

password='Tom')

cursor = conn.cursor()

if conn.is_connected():

print('sucessfull...Connected to MySQL database')

cursor.execute(" SELECT * FROM testtable1 ")

print ("<html>")

print("<p>")

print("hai")

print("</p>")

print("<body")

print ("<table border='2'>")

print ("<tr><td>")

for row in cursor.fetchall():

print (row)

print ("</td></tr>")

print ("</table>")

print ("</body>")

print ("</html>")

Output it's giving as :

sucessfull...Connected to MySQL database

I am not able to display data inside table .without table also it's unable to print the data whatever fetched from the table .

running the code in localhost as http://localhost:804/cgi-bin/test.py

Is any problem because of xampp or apache .

Please give some suggestion

网友答案:

Try this,

    for row in cursor.fetchall()
        print(row[1])

To read more about it, see this site

分享给朋友:
您可能感兴趣的文章:
随机阅读: