python - Python Sqlite3 在使用 LIKE 提取所有匹配项时引发错误
问题描述
我在从数据库中提取数据时遇到问题,当使用 audio_name = '' 的默认参数调用以下函数时,会出现错误:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied.
这是功能:
def extract_audio(self, audio_name = ''):
with self.connection:
sql = "SELECT * FROM Audio WHERE audio LIKE '%'||?||'%'"
self.my_cursor.execute(sql, audio_name)
audio = self.my_cursor.fetchall()
print(audio)
return audio
我从教程中制作了这个函数,对他们来说它工作得很好,所以问题可能是传递的空字符串。我需要一个解决方案!
解决方案
所以我基本上将执行查询的方式更改为:
self.my_cursor.execute(sql, (audio_name,))
现在它开始工作了!