ran6432 / looksearch.py
WaterWorm's picture
Upload 3 files
2e2ea43 verified
import sqlite3
import pandas as pd
import os
# --- Configuration ---
db_path = r'c:\Users\xcool\Downloads\civitai\civitai.sqlite'
# --- 1. SET YOUR LOOSE SEARCH TERM HERE ---
# This will find any model with "Corneo" anywhere in its name.
search_term = "Donald trump"
# --- 2. THE SQL QUERY IS BUILT FOR A LOOSE SEARCH ---
# The '?' is a placeholder to prevent errors and security issues (SQL injection).
# The LIKE operator with '%' wildcards on both sides finds the search term anywhere.
query = "SELECT id, name, type, username FROM models WHERE name LIKE ?;"
# --- Script Logic ---
output_filename = f'search_results_for_{search_term}.csv'
if not os.path.exists(db_path):
print(f"Error: The file was not found at the path: {db_path}")
else:
conn = None
try:
conn = sqlite3.connect(db_path)
print(f"Successfully connected. Searching for models with '{search_term}' in the name...")
# We pass the query and the parameters separately.
# The f-string formats our search term with the '%' wildcards.
df = pd.read_sql_query(query, conn, params=(f'%{search_term}%',))
if df.empty:
print(f"\nNo models found matching '{search_term}'.")
else:
print(f"\n--- Found {len(df)} Matching Models ---")
# Display results directly in the terminal
print(df)
# Save the full results to a CSV file for easy viewing
df.to_csv(output_filename, index=False)
print(f"\n✅ Success! The results have been saved to '{output_filename}'")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if conn:
conn.close()
print("\nDatabase connection closed.")