python mariadb & html tables
Larry Martell
larry.martell at gmail.com
Tue Sep 15 16:45:07 EDT 2020
On Tue, Sep 15, 2020 at 1:35 PM SS <sami.strat at gmail.com> wrote:
>
> On Tuesday, September 15, 2020 at 2:52:35 PM UTC-4, Larry.Martell at gmail.com wrote:
> > On Tue, Sep 15, 2020 at 11:45 AM SS <sami.... at gmail.com> wrote:
> > >
> > > I'm trying to create an table in html from a Maria DB table, from a python script. I'm getting some unexpected results.
> > >
> > > The environment is Centos 7, I'm using Python3 with apache.
> > >
> > > Here is copy of the script I'm using:
> > >
> > > ******* SCRIPT START *********
> > >
> > > import mysql.connector
> > > import cgitb
> > > cgitb.enable()
> > >
> > > mydb = mysql.connector.connect(
> > > host="localhost",
> > > user="root",
> > > password="somepassword",
> > > database="somedb"
> > > )
> > >
> > > mycursor = mydb.cursor()
> > >
> > > mycursor.execute("select name, provisioned_space, used_space, memory_size, cpus, ip_address, host_cpu, host_mem from projecttable where name like '%newproject%'")
> > >
> > > myresult = mycursor.fetchall()
> > >
> > > print "Content-type: text/plain:charset=utf-8"
> > > print
> > > for x in myresult:
> > > print(x)
> > >
> > >
> > > ******* SCRIPT STOPS *********
> > >
> > > It works. But I get alot of extraneous data I don't need or want. Here is an example of the output:
> > >
> > > ********* OUTPUT STARTS ***********
> > >
> > > Content-type: text/plain:charset=utf-8
> > >
> > > (u'host1.mydomain.com', u'106.11 GB', u'32.72 GB', u'1 GB', u'1', u'172.18.33.62', u'Running', u'16 MHz')
> > > (u'hopst2.mydomain.com', u'106.08 GB', u'56.87 GB', u'1 GB', u'1', u'172.17.4.82', u'Running', u'0 Hz')
> > >
> > > ********* OUTPUT STOPS ***********
> > >
> > > Is this typical of Python? Do I need create another script to clean up the output? Or is there a better way to extract data from a MariaDB instance, and put it in an HTML table? Any advise would be appreciated.
> > What is the extraneous data?
>
> The extraneous data is:
>
> (u'host1.mydomain.com', u'106.11 GB', u'32.72 GB', u'1 GB', u'1', u'172.18.33.62', u'Running', u'16 MHz')
>
> It is all the u characters, the single quotes, and the parenthesis. Ultimately, I would like to create an html table in an html page, from a a MariaDB table.
That is how the data is printed - those things are not part of the
data. To generate HTML you will have to iterate over the result set
and create the needed markup. E.g.:
html = "<table>"
for row in myresult:
html += "<tr>"
for item in row:
html += "<td>%s</td>" % item
html += "</tr>"
html += "</table>"
More information about the Python-list
mailing list