简体   繁体   English

尝试使用 mysql 输入时不断出错

[英]Keep getting errors trying to use input with mysql

I keep getting this error when i run the function:当我运行 function 时,我不断收到此错误:

You have an error in your SQL syntax;您的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)检查与您的 MySQL 服务器版本相对应的手册,以获取在 '%s 附近使用的正确语法)

I am trying to input values taken from another dbs.我正在尝试输入从另一个数据库中获取的值。 Any help would be greatly appreciated....任何帮助将不胜感激....

def filmByID(results):
    

   sql = "select * from film where filmid in (%s)"
    
    with db:
        cursor = db.cursor()
        cursor.execute(sql, (results))
        films = cursor.fetchall()
        for film in films:
            print(film["FilmName"], film["FilmSynopsis"])

Your question appears to boil down to "how do I repeat the %s the right number of times, to match my results parameter?"您的问题似乎归结为“我如何重复 %s 正确的次数,以匹配我的results参数?”

sql_list = ','.join(['%s'] * len(results))

sql = f"select * from film where filmid in ({sql_list})"

This way, if your results parameter is, for example, [1, 2, 3] then you'll end up with...这样,如果您的results参数是,例如, [1, 2, 3]那么您最终会...

"select * from film where filmid in (%s,%s,%s)"

If the results parameter is a list, rather than a tuple, then you may also need to convert it to a tuple...如果results参数是一个列表,而不是一个元组,那么您可能还需要将其转换为一个元组......

cursor.execute(sql, tuple(results))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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