%d day of month: 00
%f fractional seconds: SS.SSS
%H hour: 00-24
%j day of year: 001-366
%J Julian day number (fractional)
%m month: 01-12
%M minute: 00-59
%s seconds since 1970-01-01
%S seconds: 00-59
%w day of week 0-6 with Sunday==0
%W week of year: 00-53
%Y year: 0000-9999
%% %
Connecting to Database using sqlite3 and applying query
import sqlite3
my_path='F:\\testing\\sqlite\\my_db.db' #Change the path of your database
try:
my_conn = sqlite3.connect(my_path) #
except sqlite3.Error as my_error:
print("error: ",my_error
else:
print("Connected to database successfully")
try:
query="SELECT * FROM dt_table" # SQL to use for getting data from table
r_set=my_conn.execute(query) # execute the query using the connection
for row in r_set:
print(row) # Print each row
except sqlite3.Error as my_error:
print("error: ",my_error)
Connecting to Database using SQLAlchemy and applying query
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
my_path='F:\\testing\\sqlite\\my_db.db' #Change the path
try:
my_conn = create_engine('sqlite:///'+ my_path)
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
try:
query="SELECT * FROM dt_table LIMIT 0,3" # Query to use
r_set=my_conn.execute(query)
for row in r_set:
print(row)
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
query="SELECT * FROM dt_table WHERE date=date('now')"
r_set=my_conn.execute(query)
for row in r_set:
print(row)
Output ( it will change based on your sql dump and system date value )
('0', '2020-08-19', 'Aug-2020')
For all the queries , the display part remain same and the SQL string query
only changes. So in all balance scripts only the query part is shown.
query="SELECT * FROM dt_table WHERE date=date('now','-1 day')"
query="SELECT * FROM dt_table WHERE date=date('now','1 day')"
query="SELECT * FROM dt_table WHERE date BETWEEN date('now','-2 day') AND date('now')"
query="SELECT * FROM dt_table WHERE date BETWEEN date('now') AND date('now','3 day')"
query="SELECT * FROM dt_table WHERE date BETWEEN date('now','-10 day') AND date('now','-5 day')"
query="SELECT * FROM dt_table WHERE date BETWEEN date('now','5 day') AND date('now','10 day')"
query="SELECT * FROM dt_table WHERE strftime('%m',date) = strftime('%m','now')"
query="SELECT * FROM dt_table WHERE strftime('%m',date) = strftime('%m','now','-1 month')"
query="SELECT * FROM dt_table WHERE date BETWEEN strftime('%Y-%m-01','now') and strftime('%Y-%m-%d','now')"
query="SELECT * FROM dt_table WHERE strftime('%m',date)='01'"
For December month
query="SELECT * FROM dt_table WHERE strftime('%m',date)='12'"
query="SELECT * FROM dt_table WHERE strftime('%Y',date) = strftime('%Y','now')"
query="SELECT * FROM dt_table WHERE strftime('%Y',date) = '2022'"
query="SELECT * FROM dt_table WHERE strftime('%W',date) = strftime('%W','now')"
query="SELECT * FROM dt_table WHERE CAST(strftime('%W',date) AS INTEGER) = CAST(strftime('%W','now') AS INTEGER) - 1 "
query="SELECT * FROM dt_table WHERE CAST(strftime('%W',date) AS INTEGER) = CAST(strftime('%W','now') AS INTEGER) + 1 "
query="SELECT * FROM dt_table WHERE CAST(strftime('%W',date) AS INTEGER) = 1 "
query="SELECT * FROM dt_table WHERE strftime('%W', date)=strftime('%W','now') and \
CAST(strftime('%w',date) AS INTEGER) BETWEEN 1 AND 5"
query="SELECT * FROM dt_table WHERE strftime('%W', date)=strftime('%W','now') and \
CAST(strftime('%w',date) AS INTEGER) BETWEEN 1 AND CAST(strftime('%w','now') AS INTEGER) "
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.