【问题标题】:How to use timestamp in MySQL stored procedure如何在 MySQL 存储过程中使用时间戳
【发布时间】:2012-07-29 11:52:39
【问题描述】:

我是 stackoverflow 的新手。 另外,请原谅我的英语。

我有一个关于在 MySQL 存储过程中使用时间戳的问题。 我正在使用 MySQL 5 和 MySQLdb Python 模块(Python 版本 2.6)。 这是创建我的存储过程的代码:

def create_procedure_sum_shift ():
    global cursor

    cursor.execute("CREATE PROCEDURE `sum_shift`\
    (IN s_Time DATETIME, IN e_Time DATETIME)\
    BEGIN\
    SET @q = concat('SELECT * FROM trig_test WHERE `t_stamp`>=',\
    s_Time,' AND `t_stamp` <= ', e_Time);\
    PREPARE query FROM @q;\
    EXECUTE query;\
    deallocate prepare query;\
    END;")

当我尝试创建这个 proc 时,一切正常。 但是,当我调用这个过程时,我得到了一个错误。 这是此过程的调用:

def call_sum_shift(startDate, endDate):
    global cursor
    cursor.execute("call sum_shift(DATE('%s'),DATE('%s'));" % (startDate, endDate))
    rows = cursor.fetchall()
    discr = cursor.description
    return rows,discr

startDate(和 endDate)我明白了:

time = datetime.datetime(2012, 07, 25, 8, 00, 00)
startDate = str (time)

这是一个错误:

..._mysql_exceptions.ProgrammingError:(1064,"You have an error...for the right syntax to    use near '00:00:00 AND `t_stamp` <= 2012-07-25 00:00:00` at line 1)

我的错误在哪里?如果可能,请举例说明在 MySQL 存储过程中使用“时间戳”。

提前致谢。

【问题讨论】:

    标签: mysql mysql-python


    【解决方案1】:

    使用concat,您正在生成一个查询字符串,其中包含需要包含在' 中的DateTime 文本表示。这还没有完成。

    【讨论】:

    猜你喜欢
    • 2017-02-15
    • 2011-09-25
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 2011-06-11
    相关资源
    最近更新 更多