I've been trying for a while to show a table from my MySQL database in my browser using Python. I am using MySQL connector to create the connection with my database. (MySQLdb doesn’t work) The problem I have is that I can only display the first four columns. When I want to display an extra column (so 5 columns), nothing more is being displayed. I am stuck on this problem quite a while, maybe there is a limit on four columns in python??
(Do not mind the html ...)
Any help to solve this problem is welcome! This is my code with the print for the fifth column set as comment (#):
#!/Python27/python
#!/usr/bin/python
import mysql.connector
import sys
from mysql.connector import errorcode
print """Content-type: text/html\n
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang="nl">
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<title> mytitle </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table width=100%>
<tr>
<td class="title" colspan="10" align="center">
<img src="logo.gif"/>
<br/>
<h1 class="title"> myheader1 </h1>
<h2 class="subtitle"> myheader2 </h2>
</td>
<tr class="second">
<td class="menu">
<br/>
<div id="navigation">
<nav>
<ul>
<li><a href = "Home.py"> Home </a> </li> <br />
<li><a href = "Store.py"> Store </a> </li> <br />
<li><a href = "blog.html"> Blog </a> </li> <br />
<li><a href = "AboutUs.html"> About us </a> </li> <br />
</ul>
</nav>
</div>
</td>
<td class="main">
<br/>
<center> <strong><u> Store </u></strong></center>
<br/>
<center><table border="0"><tr><th> Refnr </th><th> Merk </th><th> Name </th><th> Description </th><th> Color </th><th></tr>
<tbody>"""
# create database connection
connection = mysql.connector.Connect(host='localhost',user='root',passwd='Mypasswd',database='Mydatabase' )
# get the cursor()
cur = connection.cursor()
# execute SQL query using execute() method.
cur.execute("""SELECT * FROM top""")
for row in cur.fetchall() :
Refnr = str(row[0])
Merk = str(row[1])
Name = str(row[2])
Description = str(row[3])
#Color = str(row[4])
print Refnr
print ' '
print Merk
print ' '
print Name
print ' '
print Description
print ' '
#print Color
print '<br>'
cur.close()
connection.close()
print """
</tbody>
</table>
</td>
</tr>
<tr>
<script type="text/javascript">
document.write(Date());
</script>
</table>
</body>
</html>
"""
print [d[0].decode('utf8') for d in cur.description]
after cur.execute.. line. What are you getting? – Alexander Afanasiev Apr 21 at 11:42color
(SELECT color FROM top
). What are you getting from print [row[0] for row in cur.fetchall()]? – Alexander Afanasiev Apr 22 at 9:25