2525INSERT_INTO_SQL = """
2626INSERT INTO news
2727(author,title,description,url,pub_date)
28- VALUES(?,?,?,?,?)
28+ VALUES(?,?,?,?,?);
2929"""
3030
3131
3232SELECT_FROM_SQL = """
33- SELECT * FROM news
33+ SELECT * FROM news;
3434"""
3535
3636DELETE_FROM_SQL = """
37- DELETE FROM news
37+ DELETE FROM news;
38+ """
39+
40+ SELECT_COUNT_SQL = """
41+ SELECT COUNT(*) FROM news;
3842"""
3943
4044
4751)
4852
4953
54+ def check_entities_count (conn ):
55+ return conn .cursor ().execute (SELECT_COUNT_SQL ).fetchone ()[0 ]
56+
57+
5058def create_connection (db_file : Path ):
5159 """
5260 Create db file
@@ -71,7 +79,7 @@ def create_table(conn, create_table_query):
7179 try :
7280 c = conn .cursor ()
7381 c .execute (create_table_query )
74- logging .info ("Table created successfully !" )
82+ logging .info (f "Table created successfully !" )
7583 except Error as create_table_err :
7684 logging .error (create_table_err )
7785
@@ -88,7 +96,9 @@ def insert_into(conn, data: tuple):
8896 cur = conn .cursor ()
8997 cur .execute (INSERT_INTO_SQL , data )
9098 conn .commit ()
91- logging .info ("Data inserted successfully !" )
99+ logging .info (
100+ f"Data inserted successfully ! Entities in db for now: { check_entities_count (conn )} "
101+ )
92102 return cur .lastrowid
93103 except Error as insert_err :
94104 logging .error (insert_err )
@@ -100,12 +110,7 @@ def send_all_news(conn):
100110 :param conn: Connection to the SQLite database
101111 :return:
102112 """
103- cur = conn .cursor ()
104- cur .execute (SELECT_FROM_SQL )
105-
106- rows = cur .fetchall ()
107-
108- return rows
113+ return conn .cursor ().execute (SELECT_FROM_SQL ).fetchall ()
109114
110115
111116def delete_all_news (conn ):
@@ -114,10 +119,11 @@ def delete_all_news(conn):
114119 :param conn: Connection to the SQLite database
115120 :return:
116121 """
117- cur = conn .cursor ()
118- cur .execute (DELETE_FROM_SQL )
122+ conn .cursor ().execute (DELETE_FROM_SQL )
119123 conn .commit ()
120- logging .info ("Database was purged !" )
124+ logging .info (
125+ f"Database was purged ! Entities in db for now: { check_entities_count (conn )} "
126+ )
121127
122128
123129if __name__ == "__main__" :
@@ -132,9 +138,13 @@ def delete_all_news(conn):
132138 if conn is not None :
133139 send_all_news (conn )
134140 if CURRENT_TIME == TIME_TO_PURGE :
135- logging .info ("Time to purge has come !" )
141+ logging .info (
142+ f"Time to purge has come ! Entities in db for now: { check_entities_count (conn )} "
143+ )
136144 delete_all_news (conn )
137145 else :
138- logging .info ("Still waiting for purging." )
146+ logging .info (
147+ f"Still waiting for purging. Entities in db for now: { check_entities_count (conn )} "
148+ )
139149 except sqlite3 .Error as sql_err :
140150 logging .error (f"Cannot create the database connection. Error: { sql_err } " )
0 commit comments