Python Forum

Full Version: Query in sqlite database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a database named 'Appartement database' with the table 'Electricite'.
In this table I have three columns: Id ¦ date_facture_elec ¦ montant_facture_elec
In the column 'date_facture_elec' I have a date in format dd/mm/yy and in the column 'montant_facture_elec' I have the invoice amount.

I would like to sum up the values in the column 'montant_facture_elec' per year.
For that I wrote following script, but I always get 'None' as response.

 
    def sum_elec_year_2022():
        find_total_elec_2022 = "SELECT sum(montant_facture_elec) FROM Electricite WHERE strftime('%Y',date_facture_elec) = 2022"
        cursor.execute(find_total_elec_2022)
        # cursor.execute"SELECT * FROM dt_table WHERE strftime('%W',date) = strftime('%W','now')"
        total_elec_2022 = (cursor.fetchone()[0])
        print(total_elec_2022)
        label_total_elec_2022 = Label(window_electricite, text=("Le montant total est de " + str(total_elec_2022) + " €"))
        label_total_elec_2022.place(x=420, y=340)
Could you please help me to understand from the error comes.
Thank you
Try using the like clause

cursor.execute(f'select sum(montant_facture_elec) from test where date_facture_elec like "%2022"')
Thank you for your answer.
It works Smile