2

I'm trying to get information from a database I have. But everytime I search it says there is nothing that follows the query. This is what I have.

import datetime
import MySQLdb

db = MySQLdb.connect(user='root', passwd='', host='localhost', db = 'data') or die ('Error while connecting')
cursor = db.cursor()
sqlread = "SELECT * FROM humidity WHERE Date_Hour BETWEEN %s AND %s"

ts1 = ('%s/%s/%s 07:00:00') % (now.year, now.month, now.day)
ts2 = ('%s/%s/%s 03:00:00') % (now.year, now.month, now.day+1)

tsdata = (ts1,ts2)
cursor.excecute(sqlread,tsdata)
db.commit()
result = cursor.fetchall()
print result

Therefore, the results is 0. But I made the same search on phpMyAdmin and it worked. What I'm doing wrong?

UPDATE:

Here is my result as Jason commented. enter image description here However, I think Jason is right. I'm doing a GUI that make some charts. And I have 2 windows. One is making a graph with an animation using matplotlib, the program make the graphics with new data that is recolected from the database, so it is always asking. The other window just make one query but it doesn't work, so it could be possible because I'm asking "twice"?.

1 Answer 1

2

Your db.commit() might be throwing it off. However, lots of factors could play a part in this. You might also consider printing out your SQL query to see what is being put so like this:

Try setting up your code like this, it should lead you in the right direction:

import MySQLdb as mdb
conn = mdb.connect('localhost', 'user', 'password', 'database_name')
try:
    cur = conn.cursor()
    ts1 = ('%s/%s/%s 07:00:00') % (now.year, now.month, now.day)
    ts2 = ('%s/%s/%s 03:00:00') % (now.year, now.month, now.day + 1)
    sqlread = "SELECT * FROM my_table where blah between {} and {}".format(ts1, ts2)
    print(sqlread)
    cur.execute(sqlread)
    res = cur.fetchall()
except mdb.Error as e:
    pass
finally:
    if conn:
        conn.close()
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Jason, thanks for your time. I updated my post, maybe you're right.
@AlejandroHarrisBonet How is your date stored in the database? Is it with - or /? What happens if you change your date to Year-Month-Day instead of Year/Month/Day.
Hi Jason, Actually I'm saving it as a string. YYYY/MM/DD, I don't know if maybe I should do it as a DATETIME format, because I was having some issues before trying to collect the date from the database.
Yes, you cannot do a between or any date range function on a string without casting it. If you change your data to a datetime, your queries will probably start working :)
Yeah, I think the same. But the strange thing is that it works with the other graph I'm doing. It's like MySQL recognize the same structure but using strings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.