Spaces:
Sleeping
Sleeping
Update database1.py
Browse filesadd method fatching tocken and file name two any to fatch the data
- database1.py +62 -39
database1.py
CHANGED
|
@@ -1,39 +1,62 @@
|
|
| 1 |
-
import sqlite3
|
| 2 |
-
import json
|
| 3 |
-
|
| 4 |
-
class create_db:
|
| 5 |
-
def __init__(self, token, chunk_json1, filename, full_content):
|
| 6 |
-
conn = sqlite3.connect('my_database.db')
|
| 7 |
-
cursor = conn.cursor()
|
| 8 |
-
|
| 9 |
-
cursor.execute("""
|
| 10 |
-
CREATE TABLE IF NOT EXISTS token_data (
|
| 11 |
-
token_id TEXT PRIMARY KEY,
|
| 12 |
-
chunk_data TEXT,
|
| 13 |
-
filename TEXT,
|
| 14 |
-
full_content TEXT
|
| 15 |
-
)
|
| 16 |
-
""")
|
| 17 |
-
|
| 18 |
-
chunk_json = json.dumps(chunk_json1)
|
| 19 |
-
|
| 20 |
-
try:
|
| 21 |
-
cursor.execute(
|
| 22 |
-
"INSERT INTO token_data (token_id, chunk_data, filename, full_content) VALUES (?, ?, ?, ?)",
|
| 23 |
-
(token, chunk_json, filename, full_content)
|
| 24 |
-
)
|
| 25 |
-
conn.commit()
|
| 26 |
-
print({"message": f"✅ {filename} uploaded and stored successfully"})
|
| 27 |
-
except sqlite3.IntegrityError:
|
| 28 |
-
print({"error": f"❌ Token already exists for: {filename}"})
|
| 29 |
-
|
| 30 |
-
conn.close()
|
| 31 |
-
|
| 32 |
-
@staticmethod
|
| 33 |
-
def get_all_filenames():
|
| 34 |
-
conn = sqlite3.connect('my_database.db')
|
| 35 |
-
cursor = conn.cursor()
|
| 36 |
-
cursor.execute("SELECT filename FROM token_data")
|
| 37 |
-
rows = cursor.fetchall()
|
| 38 |
-
conn.close()
|
| 39 |
-
return {"pdfs": [{"filename": row[0]} for row in rows]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
class create_db:
|
| 5 |
+
def __init__(self, token, chunk_json1, filename, full_content):
|
| 6 |
+
conn = sqlite3.connect('my_database.db')
|
| 7 |
+
cursor = conn.cursor()
|
| 8 |
+
|
| 9 |
+
cursor.execute("""
|
| 10 |
+
CREATE TABLE IF NOT EXISTS token_data (
|
| 11 |
+
token_id TEXT PRIMARY KEY,
|
| 12 |
+
chunk_data TEXT,
|
| 13 |
+
filename TEXT,
|
| 14 |
+
full_content TEXT
|
| 15 |
+
)
|
| 16 |
+
""")
|
| 17 |
+
|
| 18 |
+
chunk_json = json.dumps(chunk_json1)
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
cursor.execute(
|
| 22 |
+
"INSERT INTO token_data (token_id, chunk_data, filename, full_content) VALUES (?, ?, ?, ?)",
|
| 23 |
+
(token, chunk_json, filename, full_content)
|
| 24 |
+
)
|
| 25 |
+
conn.commit()
|
| 26 |
+
print({"message": f"✅ {filename} uploaded and stored successfully"})
|
| 27 |
+
except sqlite3.IntegrityError:
|
| 28 |
+
print({"error": f"❌ Token already exists for: {filename}"})
|
| 29 |
+
|
| 30 |
+
conn.close()
|
| 31 |
+
|
| 32 |
+
@staticmethod
|
| 33 |
+
def get_all_filenames():
|
| 34 |
+
conn = sqlite3.connect('my_database.db')
|
| 35 |
+
cursor = conn.cursor()
|
| 36 |
+
cursor.execute("SELECT filename FROM token_data")
|
| 37 |
+
rows = cursor.fetchall()
|
| 38 |
+
conn.close()
|
| 39 |
+
return {"pdfs": [{"filename": row[0]} for row in rows]}
|
| 40 |
+
|
| 41 |
+
# ✅ NEW: Fetch record by filename and token
|
| 42 |
+
@staticmethod
|
| 43 |
+
def fetch_by_token_or_filename(identifier):
|
| 44 |
+
conn = sqlite3.connect('my_database.db')
|
| 45 |
+
cursor = conn.cursor()
|
| 46 |
+
cursor.execute("""
|
| 47 |
+
SELECT token_id, chunk_data, filename, full_content
|
| 48 |
+
FROM token_data
|
| 49 |
+
WHERE token_id = ? OR filename = ?
|
| 50 |
+
""", (identifier, identifier))
|
| 51 |
+
result = cursor.fetchone()
|
| 52 |
+
conn.close()
|
| 53 |
+
|
| 54 |
+
if result:
|
| 55 |
+
return {
|
| 56 |
+
"token": result[0],
|
| 57 |
+
"chunk_data": json.loads(result[1]),
|
| 58 |
+
"filename": result[2],
|
| 59 |
+
"full_content": result[3]
|
| 60 |
+
}
|
| 61 |
+
else:
|
| 62 |
+
return {"error": "No matching record found for token or filename."}
|