myslq报错处理, 错误信息: Warning: (1292, "Truncated incorrect DOUBLE value: '2015-01-05'")

错误信息: Warning: (1292, "Truncated incorrect DOUBLE value: '2015-01-05'")

这个报错今晚查了好久,才发现这个解决办法,所以记录一下,如果看到我的文章不懂啥意思的可以私聊我。

调用语句:

if __name__ == '__main__':
    con = pymysql.connect("localhost", "root", "123456", "test", charset='utf8')
    cursor = con.cursor()
    test = Data()
    try:
        test.select('2015-01-05', '000004')
        for n in cursor.fetchall():
            print(n[1])
    except:
        print("查询数据失败")

 

改正前:

class Data():
    def select(self, date, code):

        print(date, type(date))
        sql_select = """select code, date from tick_data where date = %s and code = %s""" %  (date, code)
        cursor.execute(sql_select )
        print("查询数据成功")

改正后:

class Data():
    def select(self, date, code):

        date = "'" + date + "'"
        print(date, type(date))
        sql_select = """select code, date from tick_data where date = %s and code = %s""" %  (date, code)
        cursor.execute(sql_select )
        print("查询数据成功")

改正后和改正前的区别:

就加了一行代码--------    date = "'" + date + "'"

你可能感兴趣的:(mysql)