python用csv模块和pymysql模块完成.设有数据文件student.csv,内容如下图所示,请设计程序将此文件中的数据导入SQLite数据库或MySQL数据库stu.db的表data中,并对其中的数据进行插入、删除、更改、查询操作。将以上操作封装成函数。
时间: 2024-03-26 18:38:08 浏览: 162
好的,我可以为您提供相应的代码。首先需要安装pandas、PyMySQL和sqlite3模块,您可以使用以下命令安装:
```
pip install pandas
pip install PyMySQL
```
如果您需要使用SQLite数据库,则无需安装PyMySQL模块,只需安装sqlite3模块即可。
以下是将student.csv导入MySQL数据库的代码示例:
```python
import pandas as pd
import pymysql
# 读取csv文件
data = pd.read_csv('student.csv')
# 连接MySQL数据库
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='root',
db='stu',
charset='utf8mb4'
)
# 将数据写入MySQL数据库
data.to_sql(name='data', con=conn, if_exists='replace', index=False)
# 关闭数据库连接
conn.close()
```
以下是将student.csv导入SQLite数据库的代码示例:
```python
import pandas as pd
import sqlite3
# 读取csv文件
data = pd.read_csv('student.csv')
# 连接SQLite数据库
conn = sqlite3.connect('stu.db')
# 将数据写入SQLite数据库
data.to_sql(name='data', con=conn, if_exists='replace', index=False)
# 关闭数据库连接
conn.close()
```
以下是封装了插入、删除、更改、查询操作的函数示例:
```python
import pandas as pd
import pymysql
import sqlite3
def import_data(file_path, db_type, db_name, db_host='localhost', db_port=3306, db_user='root', db_password='root'):
# 读取csv文件
data = pd.read_csv(file_path)
# 连接数据库
if db_type == 'mysql':
conn = pymysql.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
db=db_name,
charset='utf8mb4'
)
elif db_type == 'sqlite':
conn = sqlite3.connect(db_name)
# 将数据写入数据库
data.to_sql(name='data', con=conn, if_exists='replace', index=False)
# 关闭数据库连接
conn.close()
def insert_data(db_type, db_name, db_host='localhost', db_port=3306, db_user='root', db_password='root', **kwargs):
# 连接数据库
if db_type == 'mysql':
conn = pymysql.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
db=db_name,
charset='utf8mb4'
)
elif db_type == 'sqlite':
conn = sqlite3.connect(db_name)
# 插入数据
cursor = conn.cursor()
sql = 'INSERT INTO data VALUES (%s, %s, %s, %s, %s)'
cursor.execute(sql, (kwargs['id'], kwargs['name'], kwargs['gender'], kwargs['age'], kwargs['score']))
conn.commit()
# 关闭数据库连接
conn.close()
def delete_data(db_type, db_name, db_host='localhost', db_port=3306, db_user='root', db_password='root', id):
# 连接数据库
if db_type == 'mysql':
conn = pymysql.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
db=db_name,
charset='utf8mb4'
)
elif db_type == 'sqlite':
conn = sqlite3.connect(db_name)
# 删除数据
cursor = conn.cursor()
sql = 'DELETE FROM data WHERE id = %s'
cursor.execute(sql, id)
conn.commit()
# 关闭数据库连接
conn.close()
def update_data(db_type, db_name, db_host='localhost', db_port=3306, db_user='root', db_password='root', id, **kwargs):
# 连接数据库
if db_type == 'mysql':
conn = pymysql.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
db=db_name,
charset='utf8mb4'
)
elif db_type == 'sqlite':
conn = sqlite3.connect(db_name)
# 更改数据
cursor = conn.cursor()
sql = 'UPDATE data SET name = %s, gender = %s, age = %s, score = %s WHERE id = %s'
cursor.execute(sql, (kwargs['name'], kwargs['gender'], kwargs['age'], kwargs['score'], id))
conn.commit()
# 关闭数据库连接
conn.close()
def select_data(db_type, db_name, db_host='localhost', db_port=3306, db_user='root', db_password='root', id=None):
# 连接数据库
if db_type == 'mysql':
conn = pymysql.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_password,
db=db_name,
charset='utf8mb4'
)
elif db_type == 'sqlite':
conn = sqlite3.connect(db_name)
# 查询数据
cursor = conn.cursor()
if id is None:
sql = 'SELECT * FROM data'
cursor.execute(sql)
else:
sql = 'SELECT * FROM data WHERE id = %s'
cursor.execute(sql, id)
results = cursor.fetchall()
# 关闭数据库连接
conn.close()
return results
```
您可以使用以下代码调用函数示例:
```python
# 将数据导入MySQL数据库
import_data('student.csv', 'mysql', 'stu')
# 插入一条记录
insert_data('mysql', 'stu', id=1001, name='Tom', gender='M', age=18, score=90)
# 删除一条记录
delete_data('mysql', 'stu', id=1001)
# 更改一条记录
update_data('mysql', 'stu', id=1002, name='Lucy', gender='F', age=19, score=95)
# 查询所有记录
results = select_data('mysql', 'stu')
for row in results:
print(row)
# 查询一条记录
result = select_data('mysql', 'stu', id=1002)
print(result)
```
阅读全文