Spaces:
Sleeping
Sleeping
Create database.py
Browse files- database.py +37 -0
database.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
def initialize_database():
|
| 4 |
+
"""Initialize the SQLite database and create the 'documents' table if it doesn't exist."""
|
| 5 |
+
# Connect to the SQLite database (or create it if it doesn't exist)
|
| 6 |
+
conn = sqlite3.connect('dataset.db')
|
| 7 |
+
cursor = conn.cursor()
|
| 8 |
+
|
| 9 |
+
# Create the 'documents' table if it doesn't exist
|
| 10 |
+
cursor.execute('''
|
| 11 |
+
CREATE TABLE IF NOT EXISTS documents (
|
| 12 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 13 |
+
text TEXT NOT NULL,
|
| 14 |
+
topics TEXT
|
| 15 |
+
)
|
| 16 |
+
''')
|
| 17 |
+
|
| 18 |
+
# Commit changes and close the connection
|
| 19 |
+
conn.commit()
|
| 20 |
+
conn.close()
|
| 21 |
+
|
| 22 |
+
def save_to_db(chunks, topics=None):
|
| 23 |
+
"""Save chunks to the SQLite database."""
|
| 24 |
+
# Ensure the database and table are initialized
|
| 25 |
+
initialize_database()
|
| 26 |
+
|
| 27 |
+
# Connect to the database
|
| 28 |
+
conn = sqlite3.connect('dataset.db')
|
| 29 |
+
cursor = conn.cursor()
|
| 30 |
+
|
| 31 |
+
# Insert chunks into the database
|
| 32 |
+
for chunk in chunks:
|
| 33 |
+
cursor.execute('INSERT INTO documents (text, topics) VALUES (?, ?)', (chunk, topics))
|
| 34 |
+
|
| 35 |
+
# Commit changes and close the connection
|
| 36 |
+
conn.commit()
|
| 37 |
+
conn.close()
|