简体   繁体   中英

I don't know how to write sql query in python by using sqlalchemy.create_engine

def check_data(self, code):
    if self.is_table_exist():
        sql = "SELECT check_item FROM '%s' WHERE code = '%s'"
        check = self.engine_stock_item_all.execute(sql % (self.today, str(code))).fetchall()[0][0]
        return check

def is_table_exist(self):
    sql = "SELECT 1 FROM information_schema.tables WHERE table_schema ='stock_item_all' AND table_name = '{}'"
    rows = self.engine_stock_item_all.execute(sql.format(self.today)).fetchall()
    if rows:
        return True
    else:
        return False

def make_daily_db(self):
    code_name = self.daily_item['code_name']
    df = pd.DataFrame(self.daily_item)
    df_temp = df[['date', 'open', 'high', 'low', 'close', 'market_cap', 'volume', 'issued_shares', 'outstanding_shares']]
    df_temp.to_sql(name=code_name, con=self.engine_daily_chart, if_exists='append')
    sql = "UPDATE '%s' SET check_item=1 WHERE code_name = '%s'"
    self.engine_stock_item_all.execute(sql % (self.today, code_name))

Hi, now I'm developing trading program in python(3.8). And If I run the program I got error like this.

Error

sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (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 ''20201130' WHERE code = '155660'' at line 1") [SQL: SELECT check_item FROM '20201130' WHERE code = '155660']

I debugged my code and I think I do something wrong at a method I defined check_data, is_table_exist and make_daily_db. I try many things to solve this problem all day, but I don't know how to change my code. Please someone who knows help me. I really need your helps.

Remove single Quotes

SELECT check_item FROM %s WHERE code = %s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM