diff --git a/cppy/cp_util.py b/cppy/cp_util.py index 76defb6..56fe658 100644 --- a/cppy/cp_util.py +++ b/cppy/cp_util.py @@ -10,6 +10,8 @@ testfilename = 'test.txt' testfilename = 'pride-and-prejudice.txt' testfilename = 'Prey.txt' +db_filename = "tf.db" + site_packages = site.getsitepackages() for package in site_packages: if 'package' in package: diff --git a/计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc b/计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc deleted file mode 100644 index 9de75d7..0000000 Binary files a/计算设备/数据库/ORM/__pycache__/createDb.cpython-38.pyc and /dev/null differ diff --git a/计算设备/数据库/tf-26.py b/计算设备/数据库/tf-26.py index 4c3584a..8288778 100644 --- a/计算设备/数据库/tf-26.py +++ b/计算设备/数据库/tf-26.py @@ -1,5 +1,5 @@ import sqlite3, os.path -from cppy.cp_util import * +from cppy.cp_util import testfilepath,db_filename,extract_file_words # 数据库表结构 @@ -45,17 +45,26 @@ def load_file_into_database(path_to_file, connection): connection.commit() c.close() - +####################################################### # 建数据库,处理数据入库 -db_path = 'tfdb' -if not os.path.isfile(db_path): - with sqlite3.connect(db_path) as connection: +####################################################### + +# 获取当前文件所在的目录 +current_dir = os.path.dirname(os.path.abspath(__file__)) +# 构造数据库文件的完整路径 +db_file_path = os.path.join(current_dir, db_filename) + +if os.path.exists(db_file_path): + os.remove(db_file_path) + +if not os.path.isfile(db_file_path): + with sqlite3.connect(db_file_path) as connection: create_db_schema(connection) load_file_into_database(testfilepath, connection) # 查询输出 -with sqlite3.connect(db_path) as connection: +with sqlite3.connect(db_file_path) as connection: c = connection.cursor() c.execute("SELECT value, COUNT(*) as C FROM words GROUP BY value ORDER BY C DESC LIMIT 10") for row in c.fetchall():