辞書を作りたいです。 ユーザーは単語を入力し、翻訳を行い、DictのようにRAMに保存し、データベースに保存します。問題:データはデータベースに格納されません。プログラムはSQLテーブルにデータを送信しません。
import sqlite3 as lite
con = lite.connect('Dictionary.db')
cur = con.cursor()
cur.execute('create table if not exists Dict (WORD TEXT, TRANSLATION TEXT)')
dictionary = {}
def load_dict():
""" From SQL transfers values to RAM """
cur = con.cursor()
cur.execute("SELECT * FROM Dict")
while True:
row = cur.fetchone()
if row == None: break
dictionary[row[0]] = row[1]
def add_word():
""" Add word to dict. """
word = str(input('Word:'))
translation = str(input('Translate: '))
for key in dictionary:
if key == word:
print('This word is in the dictionary ')
break
else:
dictionary[word] = translation
cur = con.cursor()
cur.execute("INSERT INTO Dict VALUES(?, ?)", (word, translation))
load_dict()
add_word()