SQLite3 python extracting data by month - python

I have a table populated with values, one being date in the YYYY-MM-DD format. I'm trying to print all "cost" values by month. So,
def SumByMonth():
usrDate = raw_input('Enter month: ')
sql = '''SELECT cost FROM Finance WHERE strftime('%m', date) = ?''', (usrDate)
month_cost = [t[0] for t in cur.execute(sql)]
print month_cost
This code gives me this error:
ValueError: operation parameter must be str or unicode
So I figure the SQL command isn't actually extracting anything? What am I missing here?

There is a problem with line
sql = '''SELECT cost FROM Finance WHERE strftime('%m', date) = ?''', (usrDate)
You need to write SQL query in string or unicode format like the error states
Here is a correct code :
def SumByMonth():
usrDate = raw_input('Enter month: ')
month_cost = [t[0] for t in cur.execute("SELECT cost FROM Finance WHERE strftime('%m', date) = ?", (usrDate,))]
print month_cost

Related

Inputting dates from python to a database

I have the following query in which i'm trying to pass start dates and end dates in a sql query.
def get_data(start_date,end_date):
ic = Connector()
q = f"""
select * from example_table a
where a.date between {start_date} and {end_date}
"""
result = ic.query(q)
return result
df = pd.DataFrame(get_data('2021-01-01','2021-01-31'))
print(df)
which leads to the following error:
AnalysisException: Incompatible return types 'STRING' and 'BIGINT' of exprs 'a.date' and '2021 - 1 - 1'.\n (110) (SQLExecDirectW)")
I have also tried to parse the dates as follows:
import datetime
start_date = datetime.date(2021,1,1)
end_date = datetime.date(2021,5,13)
df = pd.DataFrame(get_data(start_date,end_date))
but i still get the same error.
Any help will be much appreciated.
It seems to me, that it is because how you inject your values into sql query they don't get recognized as date values. Database will likely 2021-01-01 interpret as mathematical expression with 2019 being the result.
You should try put parentheses around your values.
q = f"""
select * from example_table a
where a.date between '{start_date}' and '{end_date}'
"""
Or preferably if your db library allows it don't inject your values directly
q = """
select * from example_table a
where a.date between %s and %s
"""
result = ic.query(q, (start_date, end_date))
EDIT: Some database libraries may use place-holder with different format than %s. You should probably consult documentation of db library you are using.

Invalid Syntax (near date)

i found this code in stackoverflow. suits my requirements. i'm using an mysql db that consists of id, date, gender, name and surname. i have made few changes to the code so that i can obtain the date and other info between the start date and the end date.
import mysql
import mysql.connector
import command
import datetime
def findarbs(startdate, enddate):
con = mysql.connector.connect(user='root', password='root',
host='localhost',port='3306',database='testdb')
cur = con.cursor()
command = ("SELECT DISTINCT id, date FROM babyrop", WHERE 'date1'
> ('%s') AND 'date2' < ('%s'), ORDER BY date asc % (startdate,
enddate))
print ('command')
cursor.execute(command)
result = cur.fetchone()
print (result)
while result is not None:
print (result[1])
result = cursor.fetchone()
cur = con.cursor()
cur.execute('select id, date weight from babyrop')
data = cursor.fetchall()
cur.close()
con.close()
when i tried executing it i'm getting an error "invalid syntax" near date. let me know what corrections to are required.
You are terminating the string way too early. You also have invalid commas in the query.
command = ("SELECT DISTINCT id, date FROM babyrop", WHERE 'date1'
> ('%s') AND 'date2' < ('%s'), ORDER BY date asc % (startdate,
enddate))
"should" be
command = ("SELECT DISTINCT id, date FROM babyrop WHERE 'date1'
> ('%s') AND 'date2' < ('%s') ORDER BY date asc" % (startdate,
enddate))
I wrote "should" because you should not use string concatenation in queries since it exposes your code to SQL injection. You should use parametrized queries instead. It also recommended to use triple quotes for query strings so they can be broken apart nicely:
command = """SELECT DISTINCT id, date
FROM babyrop
WHERE 'date1' > ('%s') AND 'date2' < ('%s')
ORDER BY date asc"""
cursor.execute(command, (startdate, enddate))
Also, I'm pretty sure that you don't need the single quotes (') around the columns names and the placeholders in the where clause but that might be a MySQL thing.

how to find the sum of multiple numbers from a column in a sql database in python?

I have a database that has a bookings table in. One of the columns in the bookings table is 'incomes', and another one is 'date_of_booking,' which stores dates in 'DD/MM/YYYY' format. I am trying to write a feature that lets a user input a month, and from that will calculate all the incomes from that month. So far I have this:
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
With this code, I am able to output the date of bookings for a particular month. However I want to output the total incomes for a particular month. What do I need to add so that this works out the total incomes for a particular month and outputs it? Any help would be gladly appreciated, thanks.
Replace one statement.
SELECT sum(income) FROM bookings where SUBSTR(date,4,2)='04'
As in:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE bookings (date text, income real)')
c.execute('''INSERT INTO bookings VALUES ('01/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('15/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('22/04/2017', 19.22)''')
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sql = '''SELECT sum(income) FROM bookings where SUBSTR(date,4,2)="%.2i"''' % int(lookForMonth)
for row in c.execute(sql):
print (row)
Resulting output:
What month, please? (number from 1 through 12):4
(57.66,)
First of all, you want to select both in your sql statement.
sqlCmd = 'SELECT date_of_booking,incomes FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
income_sum = 0
for (row_date, row_income) in conn.execute(sqlCmd):
income_sum += row_income
print row_date
print income_sum
Then you can specify both date and income of the row in your loop like above.

how to search for a date in sqlite database using python?

I have a database (called 'all_bookings')that has a column called 'date', in a table called 'bookings' which stores dates in the 'dd/mm/yyyy' form, for example '16/10/2017.' I want to write a function in python that will search for all dates on a particular month, and output them. For example, if in the dates stored where '12/12/2017', '23/11/2018',and '19/12/2018', and I wanted to output all dates that are in December, how would I do this? I know how to search for a specific date, just not a particular month. Any help would be gladly appreciated. Thanks
The SUBSTR function will do what you want. Modified to do what was asked in the OP's comment.
import sqlite3
conn = sqlite3.connect(':memory:')
events = [
('12/12/2017', 'ev_1'),
('23/11/2018', 'ev_2'),
('19/12/2018', 'ev_3'),
]
conn.execute('CREATE TABLE bookings (date, event)')
conn.executemany('INSERT INTO bookings (date, event) values (?,?)', events)
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
Results are:
('12/12/2017',)
('19/12/2018',)

Python SQLite best way to use variables in query

Why do i get this error?
sqlite3.OperationalError: near "?": syntax error
when i run this:
c.execute('UPDATE ? SET Quantity = Quantity + ? WHERE Date = ?', (table, amount, date))
But not when i run this?
c.execute('UPDATE table1 SET Quantity = Quantity + ? WHERE Date = ?', (amount, date))
Variable value is:
table = 'table1'
amount = 20
Date = '12/5/2014'
I'm trying to dynamically create tables, but just doesn't work out.
You can't use placeholders for table names. You have to use normal Python string formatting or concatenation.

Categories

Resources