Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

sqlite - sqlite3 function inside python program

I'm using a function inside a python program that doesn't work as expected. I would like to call a sqlite3 function that give me the last record registrered every 2 seconds It works fine until midnight, then it continues reading the valuew of the same day, it doesn't change when a new day arrives.

the function is(data is today, ora is actual hour):

import sqlite3 from sqlite3 import Error import time

def leggi_tmp():

try:
    time.sleep(2)
    conn = sqlite3.connect('DB.db')
    cursor = conn.cursor()
    cursor.execute('''SELECT * FROM tmp_hr WHERE data = date('now') ORDER BY ora DESC LIMIT 1''')

    #Fetching 1st row from the table
    result = cursor.fetchone()
    tmpe = result[0]
    print(result)

    #Closing the connection
    conn.close()
except Error as e:
    print(e)
return tmpe

when I do:

while data.tm_hour in fase_1 and func2_letture.leggi_tmp() <= temp_min_giorno :

func2_letture.leggi_tmp() only reads the day when it is called the first time(but works as expected during the day), it doesn't read the new date when new day arrives

I can't understand where my mistake is...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I suspect that this is a timezone problem.
Add the 'localtime' modifier to the function date():

WHERE data = date('now', 'localtime')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...