使用python

时间:2017-04-27 10:03:15

标签: python mysql

我试图通过python从mysql数据库中获取数据,我想打印它,但它什么也没给我。我的代码如下

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

上面的代码工作得很好,但我不想从表中获取所有数据。我试图在特定的时间范围内提取,所以我尝试这样

   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

,结果是

data: []

它什么也没归我。

我的表中有关usec列和另外两列的数据如下所示

usec               robin.col1.data    robin.col2.data
1456411800000000    10.90             8.02
1456412400000000    22.3              33
.
.
.
.
1469950200000000    12.22             32.43    

usec是一个包含时间戳但在epoch timeformat中的列,以微秒为单位。我也尝试过其他专栏,例如robin.col1.data 如果有人帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

你应该尝试:     日期(FROM_UNIXTIME(时期)     instaed of UNIX_TIMESTAMP(' 2016-02-25')

尝试在你的sql中运行:     SELECT日期(FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())));