pymysql error

Asked 2 years ago, Updated 2 years ago, 94 views

The following sql full text fails:

"Select Price_Div.Ticker, Price_Div.Date, Price_Div.Price_Div from Price_Div where ((Price_Div.Ticker in ('000080 KS Equity', '001530 KS Equity', '001750 KS Equity', '002960 KS Equity', '003000 KS Equity', '003540 KS Equity', '003545 KS Equity', '003720 KS Equity', '004150 KS Equity', '005010 KS Equity', '005945 KS Equity', '005960 KS Equity', '006805 KS Equity', '006840 KS Equity', '007370 KS Equity', '008730 KS Equity', '009200 KS Equity', '009835 KS Equity', '010960 KS Equity', '014190 KS Equity', '014470 KS Equity', '014530 KS Equity', '016610 KS Equity', '017670 KS Equity', '018120 KS Equity', '023900 KS Equity', '029530 KS Equity', '030200 KS Equity', '030790 KS Equity', '032640 KS Equity', '033780 KS Equity', '034230 KS Equity', '036640 KS Equity', '040420 KS Equity', '041650 KS Equity', '042700 KS Equity', '049770 KS Equity', '052860 KS Equity', '054040 KS Equity', '065690 KS Equity', '075130 KS Equity', '078935 KS Equity', '084670 KS Equity', '092440 KS Equity', '095340 KS Equity', '095720 KS Equity', '101930 KS Equity', '115310 KS Equity', '120115 KS Equity', '131180 KS Equity')) and (2011-04-01 00:00:00 <=Price_Div.Date<= 2011-05-01 00:00:00)) order by Price_Div.Date, Price_Div.Ticker"

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00 and 2011-05-01 00:00:00)) order by Price_Div.Date, Price_Div.Ticker' at line 1")

The code is as follows:

try:
    with conn.cursor() as cursor:

        sql = """Select Price_Div.Ticker, Price_Div.Date, Price_Div.Price_Div from Price_Div where ((Price_Div.Ticker in ' + str(tuple(Portfolioset[i])) + ') and ('+ str(Datelist[i]) + ' <=Price_Div.Date<= '+ str(Datelist[i+1])+')) order by Price_Div.Date, Price_Div.Ticker"""
        cursor.execute(sql)
        rows3 = cursor.fetchall()

    conn.commit()

finally:

    conn.close() 

Is there an error because I wrote an inequality sign on the date?

pymysql

2022-09-21 10:52

1 Answers

Write the date values 2011-04-0100:00:00 and 2011-05-0100:00:00 in quotation marks.

Even if the above problem is solved, if the data type of Price_Div.Date is datetime, it will not be properly compared. (If it's not datetime, but varchar, it's a problem as it is, but...)

So change the date value corresponding to the input to datetime. MySql has DATE_FORMAT and STR_TO_DATE functions.


2022-09-21 10:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.