gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:29:32 24 4
gpt4 key购买 nike

我正在尝试检查 Python 中是否存在数据库。我发现它可以使用以下 sql 语句来完成:错误:

[ERROR] 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 'TestLAU' at line 1")
Traceback (most recent call last):
File "/var/task/handler.py", line 63, in handler
cur.execute(f'SHOW DATABASES LIKE {event["db_name"]}')
File "/var/task/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/var/task/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/var/task/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/var/task/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/var/task/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/var/task/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/var/task/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/var/task/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)

最佳答案

您在语句中遗漏了一个撇号,您可以在 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"

关于python - 如何检查数据库是否存在于 PyMySQL 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57001469/

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