如何检查PyMySQL中是否存在数据库

时间:2019-07-12 06:42:52

标签: python mysql pymysql

我正在尝试检查Python中是否存在数据库。我发现可以使用以下sql语句完成此操作:Uncaught TypeError: old.remove is not a function 但是,在尝试执行它后,出现以下错误:

cur.execute(f'SHOW DATABASES LIKE {event["db_name"]}')

2 个答案:

答案 0 :(得分:0)

您在语句中遗漏了撇号,可以在%语句中使用LIKE来匹配更多结果,请尝试以下操作:

cur.execute(f"SHOW DATABASES LIKE '%{event["db_name"]}%;'")

有关LIKE运算符的更多信息:

LIKE 'a%'   Finds any values that start with "a"
LIKE '%a'   Finds any values that end with "a"
LIKE '%or%' Finds any values that have "or" in any position
LIKE '_r%'  Finds any values that have "r" in the second position
LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length
LIKE 'a%o'  Finds any values that start with "a" and ends with "o"

答案 1 :(得分:-1)

发现错误是由于以下事实引起的:我需要用引号将数据库名称引起来,以便以下SQL语句起作用:cur.execute(f'SHOW DATABASES LIKE "{event["db_name"]}"')