Python 将输入从整数更改为字符串再更改为整数

Python 将输入从整数更改为字符串再更改为整数,python,sqlite,Python,Sqlite,我有一个表单,它以HH:MM:SS 10:00:00的格式发送一个字符串,它从数据库中检索数据,然后显示该时间的温度和湿度。但我希望如此,如果他们进入上午10点10:00:00,它将从10:00:00到10:59:59搜索数据库。我使用的是sqlite3,所以没有时间戳。这也是用python实现的。 所以我想问的是,是否有一种方法可以在00:59:59转换为整数,然后将其更改回字符串,以便在查询中使用。数据库中的时间也是文本形式,格式相同 #!/usr/bin/python import sql

我有一个表单,它以HH:MM:SS 10:00:00的格式发送一个字符串,它从数据库中检索数据,然后显示该时间的温度和湿度。但我希望如此,如果他们进入上午10点10:00:00,它将从10:00:00到10:59:59搜索数据库。我使用的是sqlite3,所以没有时间戳。这也是用python实现的。 所以我想问的是,是否有一种方法可以在00:59:59转换为整数,然后将其更改回字符串,以便在查询中使用。数据库中的时间也是文本形式,格式相同

#!/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('/var/www/readings.db')

with con:
    cur = con.cursor()
    if(dateValue and timeValue != None):

        cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading = ? LIMIT 1""",(dateValue, timeValue))
        read = cur.fetchall()

        if(len(read) > 0):

            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>Temperature %s</h2>" % (read[0][0])
            print "<h2>humidity %s</h2>" % (read[0][1])
            print "</body>"
            print "</html>"
        else:
            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>No entry for that date or time</h2>"
            print "</body>"
            print "</html>"


    elif(dateValue and timeValue == None):
        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>Please enter a date or time</h2>"
        print "</body>"
        print "</html>"

假设您有字符串格式的当前时间:

timeString = "10:59:16"
您可以使用split方法在冒号的每个实例上拆分此字符串:。这将返回一个包含3个元素的列表

timeList = timeString.split(":")
print(timeList) -> ["10","59","16"]
您可以存储这些元素并使用它们进行任何计算

hours = int(timeList[0]) -> 10
minutes = int(timeList[1]) -> 59
seconds = int(timeList[2]) -> 16
完成计算或调整变量后,可以通过串联将它们重新组合成字符串

timeString = str(hours) + ":" + str(minutes) + ":" + str(seconds)
print(timeString) -> "10:59:16"

我希望这有帮助。祝你好运,卡梅隆

假设您有字符串格式的当前时间:

timeString = "10:59:16"
您可以使用split方法在冒号的每个实例上拆分此字符串:。这将返回一个包含3个元素的列表

timeList = timeString.split(":")
print(timeList) -> ["10","59","16"]
您可以存储这些元素并使用它们进行任何计算

hours = int(timeList[0]) -> 10
minutes = int(timeList[1]) -> 59
seconds = int(timeList[2]) -> 16
完成计算或调整变量后,可以通过串联将它们重新组合成字符串

timeString = str(hours) + ":" + str(minutes) + ":" + str(seconds)
print(timeString) -> "10:59:16"

我希望这有帮助。祝你好运,卡梅隆

这似乎可以在SQLite查询中使用。如果时间戳是字符串,而不是

 cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading = ? LIMIT 1""",(dateValue, timeValue))
使用类似

cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading like ? LIMIT 1""",(dateValue, '{0:02}:__:__'.format(selected_hour))
其中,已将用户选择的小时值分配给所选的小时。此查询应选择与所选日期值匹配且小时与所选值匹配的所有记录

或者,我认为

cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading like ? LIMIT 1""",(dateValue, '{0:02}%'.format(selected_hour))

这似乎可以在SQLite查询中使用。如果时间戳是字符串,而不是

 cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading = ? LIMIT 1""",(dateValue, timeValue))
使用类似

cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading like ? LIMIT 1""",(dateValue, '{0:02}:__:__'.format(selected_hour))
其中,已将用户选择的小时值分配给所选的小时。此查询应选择与所选日期值匹配且小时与所选值匹配的所有记录

或者,我认为

cur.execute("""SELECT temperature, humidity FROM readings WHERE dateReading = ? AND timeReading like ? LIMIT 1""",(dateValue, '{0:02}%'.format(selected_hour))

也可以做到这一点。

发布您的代码,您尝试了什么?您可能可以求助于基本的字符串操作,因为您似乎只需要比较两个字符串的前3个字符?您可能可以求助于基本的字符串操作,因为您似乎只需要比较两个字符串的前3个字符。请接受我的答案:3。我是新来的,需要更多的代表来标记和使用奖金请接受我的答案:3。我是新来的,需要更多的代表来标记和使用奖金