将文本数据插入数据库
两文本文件为day10面向对象练习案例
将data_define.py文件复制过来(导入失败,疑惑)
新建数据库,建表orders
-- CREATE DATABASE py_sql charset utf8;
use py_sql;
create table orders(
order_date date,
order_id VARCHAR(255),
money int,
province VARCHAR(10)
);
导包
data_define无法导入啊啊啊啊
# from day10.practice.file_define import FileReader, JsonFileReader, TextFileReader
# from day10.practice.data_define import Record
# from day10.practice import data_define
from day10.practice import file_define
from pymysql import Connection
数据准备
text_file_reader = file_define.TextFileReader("D:/2011年1月销售数据.txt")
json_file_reader = file_define.JsonFileReader("D:/2011年2月销售数据JSON.txt")
data_1 = text_file_reader.read_data()
data_2 = json_file_reader.read_data()
data_1.extend(data_2)
print(data_1)
for record in data_1:
print(record)
构建mysql连接对象
conn = Connection(
host="localhost",
port=3306,
autocommit=True,
user="root",
password="****"
)
获取游标对象
cursor = conn.cursor()
选择数据库
conn.select_db("py_sql")
组织SQL语句!!!注意此处格式化写法 f"字符串str字符串{变量}"
# 组织SQL语句!!!注意此处格式化写法 f"字符串str字符串{变量}"
for record in data_1:
sql = f"insert into orders(order_date,order_id,money,province) " \
f"values('{record.date}','{record.order_id}',{record.money},'{record.province}') "
# sql语句:insert into orders(order_date,order_id,money,province) values('2011-02-28','5b03fc64-cb93-4735-81cc-a08315f8d014',593,'山西省')
print(sql)
执行插入数据操作,已自动确认
cursor.execute(sql)
关闭数据库
# 关闭数据库
PS:完整代码
# from day10.practice.file_define import FileReader, JsonFileReader, TextFileReader
# from day10.practice.data_define import Record
# from day10.practice import data_define
from day10.practice import file_define
from pymysql import Connection
text_file_reader = file_define.TextFileReader("D:/2011年1月销售数据.txt")
json_file_reader = file_define.JsonFileReader("D:/2011年2月销售数据JSON.txt")
data_1 = text_file_reader.read_data()
data_2 = json_file_reader.read_data()
data_1.extend(data_2)
# print(data_1)
# for record in data_1:
# print(record)
# 构建mysql连接对象
conn = Connection(
host="localhost",
port=3306,
autocommit=True,
user="root",
password="####"
)
# 获取游标对象
cursor = conn.cursor()
# 选择数据库
conn.select_db("py_sql")
# 组织SQL语句!!!注意此处格式化写法 f"字符串str字符串{变量}"
for record in data_1:
sql = f"insert into orders(order_date,order_id,money,province) " \
f"values('{record.date}','{record.order_id}',{record.money},'{record.province}') "
# sql语句:insert into orders(order_date,order_id,money,province) values('2011-02-28','5b03fc64-cb93-4735-81cc-a08315f8d014',593,'山西省')
# print(sql)
# 执行插入数据操作,已自动确认
cursor.execute(sql)
# 关闭数据库
data_define
"""
数据定义的类
"""
class Record:
def __init__(self, date, order_id, money, province):
self.date = date # 日期
self.order_id = order_id # 订单id
self.money = money # 订单金额
self.province = province # 省份
# 魔术方法,构建对象时自动运行
def __str__(self):
return f"{self.date},{self.order_id},{self.money},{self.province}"
file_define
"""
文件相关的类定义
"""
import json
from data_define import Record
class FileReader:
def read_data(self) -> list[Record]:
# 读文件数据,将读到数据转为Record对象,将其封装到list内返回
pass
class TextFileReader(FileReader):
# 构造方法定义成员变量
def __init__(self, path):
self.path = path # 文件路径
# 复写(实现抽象方法)
def read_data(self) -> list[Record]:
f = open(self.path, "r", encoding="utf-8")
record_list: list[Record] = [] # 类型注解
# lines = f.readlines()
# print(lines)
for line in f.readlines():
# print(line)
line = line.strip() # 去除首尾空格换行符
data_list = line.split(',')
# print(data_list)
# 将数据转为Record类对象 ['2011-01-01', '4b34218c-9f37-4e66-b33e-327ecd5fb897', '1689', '湖南省']
record = Record(data_list[0], data_list[1], int(data_list[2]), data_list[3])
record_list.append(record)
# 关闭文件流
f.close()
# print(record_list)
return record_list
class JsonFileReader(FileReader):
# 构造方法定义成员变量
def __init__(self, path):
self.path = path # 文件路径
def read_data(self) -> list[Record]:
f = open(self.path, "r", encoding="utf-8") # 成员变量勿忘self
record_list: list[Record] = list() # 类型注解
# print(f.read())
for line in f:
data_dict = json.loads(line)
# print(data_dict)
# 将数据转为Record类对象
"""
{'date': '2011-02-01',
'order_id': 'caf99222-53d6-427b-925d-3187fc71a86a',
'money': 1805,
'province': '江西省'}
"""
record = Record(data_dict["date"], data_dict["order_id"], int(data_dict["money"]), data_dict["province"])
record_list.append(record)
# 关闭文件流
f.close()
# print(record_list)
return record_list
if __name__ == '__main__':
text_file_reader = TextFileReader("D:/2011年1月销售数据.txt") # 传入形参path
list_1 = text_file_reader.read_data()
json_file_reader = JsonFileReader("D:/2011年2月销售数据JSON.txt")
list_2 = json_file_reader.read_data()
print(list_1) # [<data_define.Record object at 0x0000020F03DD32B0>,.....]
print(list_2) # 直接打印,显示内存地址
# 一月数据
for l in list_1:
print(l)
# 二月数据
for l in list_2:
print(l)