Search code examples
pythonmysqlsqlunix-timestamp

Python MySQL(MariaDB) query FROM_UNIXTIME I get count not result (Showing rows 0 - 0 )


I need to get result on python: 2018-12-10 06:15:36 but I get: 1 (count i think) as result when i use this:

On python:

>>> dateadd = g.cur.execute("SELECT FROM_UNIXTIME(date, '%Y-%m-%d %h:%i:%s') FROM videos WHERE id={}".format(id))
>>> print dateadd
1

On phpmyadmin:

SELECT FROM_UNIXTIME(date, '%Y-%m-%d %h:%i:%s') FROM videos WHERE id=1

Showing rows 0 - 0 (1 total, Query took 0.0014 seconds.)

FROM_UNIXTIME(date, '%Y-%m-%d %h:%i:%s')   date
2018-12-10 06:15:36                        1544465736

How do i get data time (2018-12-10 06:15:36) when I use print function on python ?

Solved:

>>> g.cur.execute("SELECT FROM_UNIXTIME(date, '%Y-%m-%d %h:%i:%s') FROM videos WHERE id={}".format(id))
>>> dateadd = g.cur.fetchone()
>>> print dateadd[0]
2018-12-16 10:53:16

Thanks to @kcorlidy


Solution

  • g.cur.execute will return the status whether executed successfully. You don't need to concern about it too much. After you executed successfully, you should use fetchone, fetchall, fetchmany to gain result. And one more thing you should know that using param is a better choice, it can avoid most of sql injection. Python MySQL Parameterized Queries

    g.cur.execute("SELECT FROM_UNIXTIME(date, '%Y-%m-%d %h:%i:%s') AS value FROM videos WHERE id=%s",(id))