mercredi 2 décembre 2015

Retrieving data from sqlite with python

I'm trying to make a python file that connect to my small database that will retrieve the temperature and humidity depending on the date and time set in an html form.

  <script>
  $(function() {
  $( "#dateValue" ).datepicker();
  });
  </script>
  </head>
  <body>

    <form name="myForm" method="post" action="/cgi-bin/script1.py">
    <p>Date: <input type="text" name="dateValue" id="dateValue"></p>
    <p>Time: <select name="timeValue" id="timeValue">
      <option value="0">--All Day--</option>
      <option value="01">1am</option>
      <option value="02">2am</option>
      <option value="03">3am</option>
      <option value="04">4am</option>
      <option value="05">5am</option>
      <option value="06">6am</option>
      <option value="07">7am</option>
      <option value="08">8am</option>
      <option value="09">9am</option>
      <option value="10">10am</option>
      <option value="11">11am</option>
      <option value="12">12am</option>
      <option value="13">1pm</option>
      <option value="14">2pm</option>
      <option value="15">3pm</option>
      <option value="16">4pm</option>
      <option value="17">5pm</option>
      <option value="18">6pm</option>
      <option value="19">7pm</option>
      <option value="20">8pm</option>
      <option value="21">9pm</option>
      <option value="22">10pm</option>
      <option value="23">11pm</option>
      <option value="24">12pm</option>
     </select>
      <input type="submit" value="Enter"/>
    </form>

so once they input that data they are supposed to connect to a database which the name is readings.db and connect to the table weather which has the structure

ID dateReading timeReading temperature humidity

How do I connect to the database retrieve only the temperature and humidity for the date and time selected in the form. and how do i make it so if they chose 10am it selects all temperatures and humidity within 10am so from 10am to 11am type thing. and if there is no date or time in the database for that time then it says No data Found for the date and time.

This is my script so far

#!/usr/bin/python
import sqlite3 as lite
import sys 
import cgi, cgitb 

form = cgi.FieldStorage() 

dateValue = form.getvalue('dateValue')
timeValue  = form.getvalue('timeValue')

con = lite.connect('readings.db')

with con:
   cur = con.cursor()
   cur.execute('SELECT temperature, humidity FROM weather WHERE dateReading = dataValue AND timeReading = timeValue')

rows = cur.fetchall()

for row in rows:
    print row

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Hello - CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>received from the form %s %s</h2>" % (dateValue, timeValue)
print "</body>"
print "</html>"

The last litte bit is more of a test to make sure i was getting the data from the form

Aucun commentaire:

Enregistrer un commentaire