Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query in sqlite database
#1
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
Reply
#2
Try using the like clause

cursor.execute(f'select sum(montant_facture_elec) from test where date_facture_elec like "%2022"')
frewil likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thumbs Up 
Thank you for your answer.
It works Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  wrong entries in sqlite database and tkinter epsilondatum 1 236 Apr-13-2024, 07:21 PM
Last Post: deanhystad
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,361 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  how to use variables when open a sqlite database? 53535 1 1,201 Apr-07-2022, 08:33 AM
Last Post: ndc85430
  Building SQLite Query OogieM 10 2,939 Mar-16-2022, 11:59 AM
Last Post: OogieM
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,097 Jun-29-2020, 08:51 PM
Last Post: buran
  Write SQLite query result to file hjk6734 1 1,931 May-27-2020, 12:17 PM
Last Post: menator01
  SQLite Query in Python rowyourboat 2 2,803 Apr-26-2019, 02:24 PM
Last Post: Larz60+
  SQLite Query multiple column search whacky7 6 8,373 Apr-01-2019, 09:29 PM
Last Post: whacky7
  Dataframe Data Query Database ab0217 0 2,273 Oct-16-2018, 02:26 AM
Last Post: ab0217
  SQlite database portable? PythonWriter 1 2,498 Aug-11-2018, 02:39 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020