| import os |
| import pymysql |
| db_config = { |
| 'host': '192.168.100.188', |
| 'user': 'root', |
| 'password': 'Csiq@2019', |
| 'db': 'porn', |
| 'charset': 'utf8mb4', |
| 'cursorclass': pymysql.cursors.DictCursor |
| } |
|
|
|
|
| |
| directory_path = 'C:\\games\\H\\h-corpus' |
|
|
| |
| for filename in os.listdir(directory_path): |
| if filename.endswith('.txt'): |
| file_path = os.path.join(directory_path, filename) |
| try: |
| |
| connection = pymysql.connect(max_allowed_packet=1024 * 1024 * 64, **db_config) |
| with connection.cursor() as cursor: |
| with open(file_path, 'r', encoding='utf-8') as file: |
| title = file.readline().strip() |
| content = file.read().strip() |
|
|
| |
| sql = "INSERT INTO hnote (title, content) VALUES (%s, %s)" |
| cursor.execute(sql, (title, content)) |
|
|
| |
| connection.commit() |
| except Exception as e: |
| print(f"An error occurred: {e}") |
| finally: |
| |
| if connection: |
| connection.close() |
|
|
| print("All .txt files have been processed and inserted into the database.") |