[英]extract data between two time range from MySQL using python
I am trying to fetch data from mysql database through python and I want to print it, but it give me nothing. 我试图通过python从mysql数据库中获取数据,我想打印它,但是它什么也没给我。 My code is as follow
我的代码如下
import time
import datetime
import calendar
import mysql.connector
cnx = mysql.connector.connect(user='xxxx', password='xxxx',
database='robbinDatabase')
cursor = cnx.cursor()
tableData = cursor.execute('select * from robtable')
data = cursor.fetchall()
print 'data: ', data
the above code works quite well, but I dont want to fetch all data from a table. 上面的代码效果很好,但是我不想从表中获取所有数据。 I am trying to extract in a particular time range, so I try like this
我正在尝试在特定时间范围内进行提取,所以我尝试这样
cnx = mysql.connector.connect(user='xxxx', password='xxxx',
database='robbinDatabase')
cursor = cnx.cursor()
rowsdata = cursor.execute("SELECT * FROM robtable WHERE usec BETWEEN UNIX_TIMESTAMP('2016-02-25') AND UNIX_TIMESTAMP('2016-02-26 23:59:59')")
# rowsdata = cursor.execute('SELECT * FROM robtable WHERE usec BETWEEN UNIX_TIMESTAMP(1456411800000000) AND UNIX_TIMESTAMP(1456416000000000)')
# rowsdata = cursor.execute('SELECT * FROM robtable WHERE `robin.col1.data` BETWEEN FROM_UNIXTIME(1456411800) AND FROM_UNIXTIME(145642200)')
data = cursor.fetchall()
print data
and the result is 结果是
data: []
It return me nothing. 它什么也没回报。
the data in my table for usec columns and two other columns look like this 在我的表中,usec列和其他两个列的数据如下所示
usec robin.col1.data robin.col2.data
1456411800000000 10.90 8.02
1456412400000000 22.3 33
.
.
.
.
1469950200000000 12.22 32.43
usec is a column which contains timestamp but in epoch timeformat with microseconds. usec是一列,其中包含时间戳,但以纪元时间格式(含微秒)。 I also tried with other column eg
robin.col1.data
If somebody help me, I would be grateful. 我也尝试过其他列,例如
robin.col1.data
如果有人帮助我,我将不胜感激。
You should try: date(FROM_UNIXTIME(epoch) instaed of UNIX_TIMESTAMP('2016-02-25') 您应该尝试:date(FROM_UNIXTIME(epoch)insta of UNIX_TIMESTAMP('2016-02-25')
try run in your sql: SELECT date(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW()))); 尝试在您的sql中运行:SELECT date(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.