0

我正在尝试将大量英语单词插入数据库,然后返回结果。但是,当我查询数据库时,它什么也没返回。对于这个示例,我省略了将文本文件输入到数据库中,并且只插入了一个字符串。但是,这也没有显示在查询中。这是我的代码:

import sqlite3

def get_database_connection():
    return sqlite3.connect("myDatabase.db")

def commit():
    connection = get_database_connection()
    connection.commit()
    connection.close()

def table_exists(table):
    cursor = get_cursor()
    cursor.execute(''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='{0}' '''.format(table))
    my_bool = cursor.fetchone()[0]
    return my_bool


def get_cursor():
    connection = sqlite3.connect("myDatabase.db")
    cursor = connection.cursor()
    return cursor


def create_table(table):
    if table_exists(table):
        return
    cursor = get_cursor()
    cursor.execute("""CREATE TABLE {0}(
                        word text
                        )""".format(table))

def insert_english_words():
    #english_words = "english.txt"
    #words = process_words(english_words)
    table = "english"
    create_table(table)
    cursor = get_cursor()
    cursor.execute("INSERT INTO english VALUES ('HELLO')")
    #for word in words:
        #cursor.execute("INSERT INTO english VALUES ('{0}')".format(word))
    commit()

def get_data():
    cursor = get_cursor()
    cursor.execute("SELECT * FROM english")
    rows = cursor.fetchall()
    for row in rows:
        print(row)


def run():
    get_database_connection()
    insert_english_words()
    get_data()
    print("Done")


run()
4

1 回答 1

3

您必须在创建表后提交。在整个脚本生命周期中维护一个连接对象。您每次都在创建一个新连接。

于 2019-12-08T09:11:23.963 回答