Skip to content

Commit

Permalink
create and insert words into enlish.sqlite3 db use Python 3.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyuwei committed Apr 5, 2024
1 parent 0390a3c commit eddcbd1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions dictionary/sqlite/create_english_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sqlite3

# Step 1: Connect to the SQLite database (it will be created if it doesn't exist)
conn = sqlite3.connect('english.sqlite3')
c = conn.cursor()

# Step 2: Create the table
c.execute('''
CREATE TABLE IF NOT EXISTS words (
word TEXT NOT NULL,
freq INTEGER NOT NULL
);
''')

# Step 3: Create an index on the 'word' column for efficient prefix matching
c.execute('CREATE INDEX IF NOT EXISTS idx_word ON words(word);')

# Step 4: Read from english.txt and insert data into the database
with open('../google_227800_words.txt', 'r', encoding='utf-8') as file:
for line in file:
parts = line.strip().split('\t')
if len(parts) == 2:
word, frequency = parts
c.execute('INSERT INTO words (word, freq) VALUES (?, ?)', (word, int(frequency)))

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

print("Database created, table and index created, and data inserted successfully.")
Binary file added dictionary/sqlite/english.sqlite3
Binary file not shown.

0 comments on commit eddcbd1

Please sign in to comment.