使用多个参数格式化字符串,以便 MySQL 可以处理它们
Format string with multiple parameters so that MySQL can proces them
我想知道如何格式化由 API 编辑的 return 字符串,以便 Mysql 可以处理它。
这是字符串 my API returns: bad_string = "History,Romance,Business & Money"
尝试 1:
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string)
Returns 此消息:
Traceback (most recent call last):
File "C:\PATH", line 23, in <module>
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\cursor_cext.py", line 259, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
尝试 2:
bad_string_2 = "'Romance', 'History', 'Business & Money'"
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string_2)
Returns 此消息:
Traceback (most recent call last):
File "C:PATH", line 21, in <module>
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string_2)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\cursor_cext.py", line 246, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\connection_cext.py", line 520, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
这个有效:
cursor.execute("SELECT * FROM book WHERE scrape_category IN ('Romance', 'History', 'Business & Money') ORDER BY RAND() LIMIT 15")
当我这样做时查询有效:
cursor.execute("SELECT * FROM book WHERE scrape_category IN ('Romance', 'History', 'Business & Money') ORDER BY RAND() LIMIT 15")
如何格式化字符串并得到 MySQL return 结果?
您的字符串值为:
"History,Romance,Business & Money"
你需要这个:'Romance', 'History', 'Business & Money'
试试 bad_string = " 'Romance', 'History', 'Business & Money' "
希望对您有所帮助...
试试这样的:
filter ="History,Romance,Business & Money".split(',')
sqlcommand = "SELECT * FROM book WHERE scrape_category IN ({0})".format(
', '.join(['%s'] * len(filter)))
print(sqlcommand)
cursor.execute(sqlcommand, filter)
我想知道如何格式化由 API 编辑的 return 字符串,以便 Mysql 可以处理它。
这是字符串 my API returns: bad_string = "History,Romance,Business & Money"
尝试 1:
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string)
Returns 此消息:
Traceback (most recent call last):
File "C:\PATH", line 23, in <module>
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\cursor_cext.py", line 259, in execute
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
尝试 2:
bad_string_2 = "'Romance', 'History', 'Business & Money'"
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string_2)
Returns 此消息:
Traceback (most recent call last):
File "C:PATH", line 21, in <module>
cursor.execute("SELECT * FROM book WHERE scrape_category IN (%s) ORDER BY RAND() LIMIT 15", bad_string_2)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\cursor_cext.py", line 246, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "C:\PATH\AppData\Roaming\Python\Python37\site-packages\mysql\connector\connection_cext.py", line 520, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
这个有效:
cursor.execute("SELECT * FROM book WHERE scrape_category IN ('Romance', 'History', 'Business & Money') ORDER BY RAND() LIMIT 15")
当我这样做时查询有效:
cursor.execute("SELECT * FROM book WHERE scrape_category IN ('Romance', 'History', 'Business & Money') ORDER BY RAND() LIMIT 15")
如何格式化字符串并得到 MySQL return 结果?
您的字符串值为:
"History,Romance,Business & Money"
你需要这个:'Romance', 'History', 'Business & Money'
试试 bad_string = " 'Romance', 'History', 'Business & Money' "
希望对您有所帮助...
试试这样的:
filter ="History,Romance,Business & Money".split(',')
sqlcommand = "SELECT * FROM book WHERE scrape_category IN ({0})".format(
', '.join(['%s'] * len(filter)))
print(sqlcommand)
cursor.execute(sqlcommand, filter)