My Code:
date_today = datetime.date.today()
date_curr_year = date_today.year
date_curr_month = date_today.month
date_curr_month_1d = datetime.date(day=1, month=date_curr_month, year=date_curr_year)
date_prev_month = date_curr_month_1d - datetime.timedelta(days=1)
string_date_prev_month = date_prev_month.strftime("%Y-%m-1")
string_date_curr_month_1d = date_curr_month_1d.strftime("%Y-%m-%d")
try:
#Query:
q_getstuff = "SELECT `timestamp`, `number`, `url`, `procedure` FROM transaction WHERE `number` IS NOT NULL AND (`timestamp` >= \'" + string_date_prev_month + "\' AND `timestamp` < \'" + string_date_curr_month_1d + "\')"
print q_getstuff
#Database Connection
con = mdb.connect(hostname, username, password, database)
cur = con.cursor()
cur.execute(q_getstuff)
result = cur.fetchall()
numrows = cur.rowcount
for row in result:
print row
except mdb.Error, e:
print "Error " + str(e.args[0]) + " " + str(e.args[1])
sys.exit(1)
con.close()
The whole date crap is simply to get the first date of this month, and the first date of the previous month. The query is supposed to get the data from last month. If there's a better way to do this, please let me know.
Here's the result of the print q_getstuff:
SELECT `timestamp`, `number`, `url`, `procedure` FROM transaction
WHERE `number` IS NOT NULL AND (`timestamp` >= '2015-02-1' AND
`timestamp` < '2015-03-01')
Take note that the field names are enclosed in tildes. It seems right, no errors. If I print out numrows, it returns 2, as in, 2 rows.
The Problem:
I copy the output of print q_getstuff, and just straight up paste it (no edits) into MySQL, and it returns 78 rows.
So obviously something's wrong with the code, but I'm not sure what's happening. Where did I go wrong?