根据日期显示sqlite3数据库的内容

时间:2019-03-04 07:11:13

标签: sqlite

我使用python 3.6和sqlite3,当我想读取2个变量之间的表内容时遇到问题 首先,当我使用两个字符串格式的日期(“ 2019-01-01”和“ 2019-05-20”)时,以下代码可以正常工作:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import time
import calendar
import sqlite3
import datetime

fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))

def CreateTable_releves():

    conn1_releves = sqlite3.connect('bdd/test.db')
    curseur1_releves = conn1_releves.cursor()
    curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
    curseur1_releves.close()


def Add_To_Table_releves():

    time_releves_get = str(date_choisie_ils35R)

    monitor1_releves_get = str(ent_releves_mesure_mon1_axe_loc35R.get())
    monitor2_releves_get = str(ent_releves_mesure_mon2_axe_loc35R.get())

    conn2_releves = sqlite3.connect('bdd/test.db')
    curseur2_releves = conn2_releves.cursor()
    curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
    conn2_releves.commit()
    curseur2_releves.close()

# CreateTable_releves()

def monitoring () :

    fen_monitoring=Toplevel(fen)
    fen_monitoring.title('ddm axe')
    fen_monitoring.geometry('450x400+300+150')

    s= Scrollbar(fen_monitoring)
    T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
    s.pack(side=RIGHT, fill=Y)
    T.pack(side=LEFT, fill=Y)
    s.config(command=T.yview)
    T.config(yscrollcommand=s.set)

    def update_releves():

        T.delete('1.0', END)
        T.update()

        conn = sqlite3.connect('bdd/test.db')
        curseur = conn.cursor()     
        for resultats in curseur.execute('SELECT * FROM releves  where  time_releves between "2019-01-01" AND "2019-05-20" ORDER BY time_releves DESC'):  

            contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"

            T.insert(END,contenu_table_releves)  
        T.after(1000,update_releves)
        curseur.close()

    update_releves()

bt=Button(fen,text='click on\nthe button', command=monitoring)
bt.pack()

这是顶层窗口内容的图像:

enter image description here

但是如果我通过在sql requet中输入两个变量(first_day)和(last_day)来修改代码,则:

for resultats in curseur.execute('SELECT * FROM releves  where  time_releves between "first_day" AND "last_day" ORDER BY time_releves DESC'):

有完整的修改代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import time
import calendar
import sqlite3
import datetime

fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))

#to calculate first and last day of the month:
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)
last_day=last_day_month.strftime('%Y-%m-%d')
first_day=start_month = datetime.datetime(now.year, now.month, 1).strftime('%Y-%m-%d')

def CreateTable_releves():

    conn1_releves = sqlite3.connect('bdd/test.db')
    curseur1_releves = conn1_releves.cursor()
    curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
    curseur1_releves.close()


def Add_To_Table_releves():

    time_releves_get = str(date_choisie_ils35R)

    monitor1_releves_get = str(ent_releves_mesure_mon1_axe_loc35R.get())
    monitor2_releves_get = str(ent_releves_mesure_mon2_axe_loc35R.get())

    conn2_releves = sqlite3.connect('bdd/test.db')
    curseur2_releves = conn2_releves.cursor()
    curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
    conn2_releves.commit()
    curseur2_releves.close()

# CreateTable_releves()

def monitoring () :

    fen_monitoring=Toplevel(fen)
    fen_monitoring.title('ddm axe')
    fen_monitoring.geometry('450x400+300+150')

    s= Scrollbar(fen_monitoring)
    T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
    s.pack(side=RIGHT, fill=Y)
    T.pack(side=LEFT, fill=Y)
    s.config(command=T.yview)
    T.config(yscrollcommand=s.set)

    def update_releves():

        T.delete('1.0', END)
        T.update()

        conn = sqlite3.connect('bdd/test.db')
        curseur = conn.cursor()     
        for resultats in curseur.execute('SELECT * FROM releves  where  time_releves between "first_day" AND "last_day" ORDER BY time_releves DESC'):  

            contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"

            T.insert(END,contenu_table_releves)  
        T.after(1000,update_releves)
        curseur.close()

    update_releves()

bt=Button(fen,text='click on\nthe button', command=monitoring)
bt.pack()

顶层窗口出现,但像下一张图片一样为空:

enter image description here

1 个答案:

答案 0 :(得分:0)

我修改了代码,以便通过集成小部件(条目,按钮)使代码更清晰,然后添加“ R Spark”的设定值。结果,我得到了下面的代码,目前看来已经可以了。 (我将尽快重做其他测试) 如果对此新产品有任何意见,欢迎您。

新代码:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
import time
import calendar
import sqlite3
import datetime

fen = Tk()
fen.title('ILS system ')
fen.resizable(0,0)
largeur=920
hauteur=660
fen.geometry('{}x{}+0+0'.format(largeur,hauteur))

#to calculate first and last day of the month:
now = datetime.datetime.now()
start_month = datetime.datetime(now.year, now.month, 1)
date_on_next_month = start_month + datetime.timedelta(35)
start_next_month = datetime.datetime(date_on_next_month.year, date_on_next_month.month, 1)
last_day_month = start_next_month - datetime.timedelta(1)
last_day=last_day_month.strftime('%Y-%m-%d')
first_day=start_month = datetime.datetime(now.year, now.month, 1).strftime('%Y-%m-%d')

def CreateTable_releves():

    conn1_releves = sqlite3.connect('bdd/test/test.db')
    curseur1_releves = conn1_releves.cursor()
    curseur1_releves.execute('''CREATE TABLE IF NOT EXISTS releves (id INTEGER PRIMARY KEY,time_releves TEXT NOT NULL, monitor1_releves TEXT NOT NULL,monitor2_releves TEXT NOT NULL)''')
    curseur1_releves.close()

def Add_To_Table_releves():

    time_releves_get = str(date_choisie.get())
    monitor1_releves_get = str(ent_mon1_mesure.get())
    monitor2_releves_get = str(ent_mon2_mesure.get())

    conn2_releves = sqlite3.connect('bdd/test/test.db')
    curseur2_releves = conn2_releves.cursor()
    curseur2_releves.execute('''INSERT INTO releves (time_releves,monitor1_releves,monitor2_releves) VALUES (?,?,?)''',(time_releves_get,monitor1_releves_get,monitor2_releves_get))
    conn2_releves.commit()
    curseur2_releves.close()

CreateTable_releves()

def monitoring () :

    fen_monitoring=Toplevel(fen)
    fen_monitoring.title('ddm axe')
    fen_monitoring.geometry('450x400+300+150')

    s= Scrollbar(fen_monitoring)
    T= Text(fen_monitoring,bg='powder blue',width=450,height=400)
    s.pack(side=RIGHT, fill=Y)
    T.pack(side=LEFT, fill=Y)
    s.config(command=T.yview)
    T.config(yscrollcommand=s.set)

    def update_releves():

        T.delete('1.0', END)
        T.update()

        conn = sqlite3.connect('bdd/test/test.db')
        curseur = conn.cursor()     
        for resultats in curseur.execute('SELECT * FROM releves where time_releves between ? AND ? ORDER BY time_releves DESC;', (first_day, last_day)):  

            contenu_table_releves= str(resultats[1])+"\t\t"+ str(resultats[2])+"\t\t"+ str(resultats[3])+"\n"

            T.insert(END,contenu_table_releves)  
        T.after(1000,update_releves)
        curseur.close()

    update_releves()

ent_mon1_mesure=StringVar()
ent_mon2_mesure=StringVar()
date_choisie=StringVar()

ent_mon1=Entry(fen,textvariable=ent_mon1_mesure)
ent_mon1.pack(side=LEFT,anchor=NW,padx=10,pady=10)

ent_mon2=Entry(fen,textvariable=ent_mon2_mesure)
ent_mon2.pack(side=LEFT,anchor=NW,padx=10,pady=10)

ent_time_releve=Entry(fen,textvariable=date_choisie)
ent_time_releve.pack(side=LEFT,anchor=NW,padx=10,pady=10)

bt_valider=Button(fen,text='valider', command=Add_To_Table_releves)
bt_valider.pack(side=LEFT,padx=10,pady=10)

bt_monitoring=Button(fen,text='afficher monitoring', command=monitoring)
bt_monitoring.pack(side=LEFT,padx=10,pady=10)

fen.mainloop()

感谢R Spark的帮助