这是我关于堆栈溢出的第一篇文章...在此先感谢您提供的所有帮助我对编程非常陌生,我在Python中创建了一个函数来动态搜索sqlite3数据库,而不用输入大量查询。我将显示代码并尝试解释我打算在所有阶段进行的操作。简而言之,即使我确定应该在数据库中找到一个值,我的cursor.fetchall()也会始终为空。
def value_in_database_check(table_column_name: str, value_to_check: str):
db, cursor = get_connection() # here i get a database and cursor connection
for tuple_item in schema(): # here i get the schema of my database from another function
if tuple_item[0] == "table": # now I check if the tuple is a table
schema_table = tuple_item[4] # this just gives me the table info of the tuple
# this lets me know the index of the column I am looking for in the table
found_at = schema_table.find(table_column_name)
# if my column value was found I will enter this block of code
if not found_at == -1:
table_name = tuple_item[1]
to_find_sql = "SELECT * FROM {} WHERE ? LIKE ?".format(table_name)
# value_to_check correlates to table_column_name
# example "email", "[email protected]"
cursor.execute(to_find_sql, (table_column_name, value_to_check))
# this always evaluates to an empty list even if I am certain that the
# information is in the database
fetch_to_find = cursor.fetchall()
if len(fetch_to_find) > 0:
return True
else:
return False
我相信?
只能用作值的占位符,不能用作表名或列名(如您尝试这样做)。可能的修补程序(虽然尚未测试):
to_find_sql = "SELECT * FROM {} WHERE {} LIKE ?".format(table_name, table_column_name)
cursor.execute(to_find_sql, (value_to_check, ))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句