File size: 1,773 Bytes
ce0cd06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Copyright (c) Meta Platforms, Inc. and affiliates.

# All rights reserved.

# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from tasks.longwiki.retrieval import DocDB
import sqlite3
import argparse

def get_titles_all(db):
    # get all titles only
    cursor = db.connection.cursor()
    cursor.execute("SELECT title FROM documents")
    results = cursor.fetchall()
    results_title = [r[0] for r in results]
    cursor.close()
    return results_title

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--wiki_db_path", type=str, default="data/wiki_data/.cache/enwiki-20230401.db")
    args = parser.parse_args()

    db_path=args.wiki_db_path
    db_title_path=db_path.replace(".db", "-title.db")
    db = DocDB(db_path=db_path)

    # Step 1: Get all titles
    titles = get_titles_all(db)

    # Step 2: Create an SQLite database connection
    conn = sqlite3.connect(db_title_path)
    cursor_ = conn.cursor()

    # Step 3: Create a table to store the titles
    cursor_.execute('''
    CREATE TABLE IF NOT EXISTS titles (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title_name TEXT NOT NULL
    );
    ''')

    # Step 4: Insert data from the list into the table
    cursor_.executemany('''
    INSERT INTO titles (title_name) VALUES (?)
    ''', [(title,) for title in titles])  # Tuple format for each title

    # Commit the changes and close the connection
    conn.commit()

    # Step 5: Verify the data by selecting and printing
    cursor_.execute("SELECT * FROM titles")
    i = 0
    for row in cursor_.fetchall():
        print(row)
        i +=1
        if i > 10:
            break

    # Close the connection
    conn.close()