gpt4 book ai didi

python - MySQL、Python : SELECT WHERE LIKE Query

转载 作者:行者123 更新时间:2023-11-30 21:28:35 25 4
gpt4 key购买 nike

我正在创建一个控制台界面来添加、编辑和查看我的视频游戏收藏数据库中的条目。在编辑部分我添加了一个搜索功能,以便找到特定的游戏。在搜索的选择查询中,我有要从中提取的表变量和类似条件。

对于我使用的选择查询:

GameQuery = "SELECT Name, Sub_Name, Platform, Barcode, Edition, Overall_Condition, Region, Requirement, Optional FROM AllGames WHERE Name LIKE %s"

然后执行:

MediaCursor.execute(GameQuery, SearchCriteria)

但是如果我要搜索名称中包含撇号的游戏,例如汤姆克兰西的它只会执行 ...LIKE %s 因为名称中的撇号将其切断。然后我尝试执行它:

MediaCursor.execute(GameQuery,(SearchCriteria,))

这恰好适合我的情况。但是我也无法为表名使用变量,而这是我需要的。

我尝试过使用:

GameQuery = "SELECT Name, Sub_Name, Platform, Barcode, Edition, Overall_Condition, Region, Requirement, Optional FROM %s WHERE Name LIKE %s"

通过这次执行:

MediaCursor.execute(GameQuery, TableName,(SearchCriteria,))

但它不会用值替换 %s

然后我尝试在代码中针对这种情况使用 Python 元组

SearchCriteria = raw_input("Input The Game Name: ")
TableName = raw_input("Input The Platform: ")
qy = TableName, (SearchCriteria,)
GameQuery = "SELECT Name, Sub_Name, Platform, Barcode, Edition, Overall_Condition, Region, Requirement, Optional FROM %s WHERE Name LIKE %s" %qy
MediaCursor.execute(GameQuery)

但这只是在 MediaCursor.execute(GameQuery)

中给出了 靠近 ) 的语法错误

最佳答案

我修好了。我将 TableName 变量放在 GameQuery 行中并像这样执行它:

GameQuery = "SELECT Name, Sub_Name, Platform, Barcode, Edition, Overall_Condition, Region, Requirement, Optional FROM "+ TableName + "WHERE Name LIKE %s"

MediaCursor.execute(GameQuery,(SearchCriteria))

但是,谢谢大家的帮助。

关于python - MySQL、Python : SELECT WHERE LIKE Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com