Python 创建 SQLite 词典

2024-04-25 23:14:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个英语单词的数据库,但是没有任何值被输入数据库

我从一个文件中读取每个单词,并使用它的sha256散列来表示单词的定义,但是当我执行脚本时,数据库保持不变,没有填充单词,数据库的大小也保持不变。在

为什么数据库中没有填充这些值?在

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3 as lite
import sys
import hashlib


word_file = open("words.txt", "r")

con = lite.connect('words.db')
cur = con.cursor()  

cur.execute("CREATE TABLE IF NOT EXISTS words(word TEXT PRIMARY KEY, definition  TEXT);")

for word in word_file.read().split():
    cur.execute("INSERT OR REPLACE INTO words VALUES(\"%s\", \"%s\")" % (word, hashlib.sha256(word).hexdigest()))

word_file.close()

# cur.execute("SELECT * FROM words WHERE word = 'hello'")
# print cur.fetchall()
con.close()

Tags: textimport数据库closeexecutelite单词con