Word导入MySQL数据库的实现方法
一、流程图
flowchart TD
A[开始] --> B[读取Word文件]
B --> C[解析Word文件]
C --> D[连接MySQL数据库]
D --> E[创建数据表]
E --> F[插入数据]
F --> G[关闭数据库连接]
G --> H[结束]
二、步骤和代码
步骤 | 代码 | 说明 |
---|---|---|
1. 读取Word文件 | ```python |
import docx
def read_word_file(file_path):
doc = docx.Document(file_path)
content = []
for paragraph in doc.paragraphs:
content.append(paragraph.text)
return content
| 使用`docx`库读取Word文件,将内容逐段提取出来,存放在一个列表中。 | | 2. 解析Word文件 |
python
def parse_content(content):
data = []
for line in content:
line_data = line.split('\t')
if len(line_data) == 3:
data.append(line_data)
return data
| 3. 连接MySQL数据库 | ```python
import mysql.connector
def connect_mysql(host, user, password, database):
try:
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
return connection
except mysql.connector.Error as error:
print("Failed to connect to MySQL database: {}".format(error))
return None
``` | 使用`mysql.connector`库建立与MySQL数据库的连接,传入数据库主机地址、用户名、密码和数据库名。 |
| 4. 创建数据表 | ```python
def create_table(connection):
cursor = connection.cursor()
try:
cursor.execute("CREATE TABLE IF NOT EXISTS word_data (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT, address VARCHAR(255))")
connection.commit()
print("Table created successfully")
except mysql.connector.Error as error:
print("Failed to create table: {}".format(error))
cursor.close()
``` | 使用数据库连接对象的`cursor()`方法创建游标对象,调用游标对象的`execute()`方法执行SQL语句创建数据表。 |
| 5. 插入数据 | ```python
def insert_data(connection, data):
cursor = connection.cursor()
try:
cursor.executemany("INSERT INTO word_data (name, age, address) VALUES (%s, %s, %s)", data)
connection.commit()
print("Data inserted successfully")
except mysql.connector.Error as error:
print("Failed to insert data: {}".format(error))
cursor.close()
``` | 使用游标对象的`executemany()`方法批量插入数据,将解析得到的二维列表作为参数传入。 |
| 6. 关闭数据库连接 | ```python
def close_connection(connection):
connection.close()
``` | 调用数据库连接对象的`close()`方法关闭数据库连接。 |
| 7. 结束 | 无 | |
## 三、完整代码
```python
import docx
import mysql.connector
def read_word_file(file_path):
doc = docx.Document(file_path)
content = []
for paragraph in doc.paragraphs:
content.append(paragraph.text)
return content
def parse_content(content):
data = []
for line in content:
line_data = line.split('\t')
if len(line_data) == 3:
data.append(line_data)
return data
def connect_mysql(host, user, password, database):
try:
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
return connection
except mysql.connector.Error as error:
print("Failed to connect to MySQL database: {}".format(error))
return None
def create_table(connection):
cursor = connection.cursor()
try:
cursor.execute("CREATE TABLE IF NOT EXISTS word_data (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT, address VARCHAR(255))")
connection.commit()
print("Table created successfully")
except mysql.connector.Error as error:
print("Failed to create table: {}".format(error))
cursor.close()
def insert_data(connection, data):
cursor = connection.cursor()
try:
cursor.executemany("INSERT INTO word_data (name, age, address) VALUES (%s, %s, %s)", data)
connection.commit()
print("Data inserted successfully")
except mysql.connector.Error as error:
print("Failed to insert data: {}".format(error))
cursor.close