厦门做网站公司,哈尔滨网站制作方案定制,建设网站必须要服务器,wordpress 新闻模板除了使用图形化工具以外#xff0c;我们也可以使用编程语言来执行SQL从而操作数据库。在Python中#xff0c;使用第三方库: pymysql来完成对MySQL数据库的操作。 安装第三方库pymysql 使用命令行,进入cmd#xff0c;输入命令pip install pymysql. 创建到MySQL的数据库连接… 除了使用图形化工具以外我们也可以使用编程语言来执行SQL从而操作数据库。在Python中使用第三方库: pymysql来完成对MySQL数据库的操作。 安装第三方库pymysql 使用命令行,进入cmd输入命令pip install pymysql. 创建到MySQL的数据库连接 这里的8.0.36是MySQL的版本。 使用python执行非查询性质的SQL语句 代码块 执行前数据库 执行后数据库 使用python执行查询性质的SQL语句
from pymysql import Connection # 导包# 构建到MySQL数据库的连接
coon Connection(hostlocalhost, # 主机名(IP)port3306, # 端口(默认端口是3306)userroot, # 账户password123456 # 密码
)# 执行非查询性质SQL
cursor coon.cursor() # 获取到游标对象
# 选择数据库
coon.select_db(mydb1)
# 执行SQL
cursor.execute(select * from product)
result cursor.fetchall()
for r in result:print(r)# 关闭连接
coon.close()运行效果 数据库里面的数据 向MySQL中插入数据
from pymysql import Connection # 导包# 构建到MySQL数据库的连接
coon Connection(hostlocalhost, # 主机名(IP)port3306, # 端口(默认端口是3306)userroot, # 账户password123456 # 密码
)# 执行非查询性质SQL
cursor coon.cursor() # 获取到游标对象
# 选择数据库
coon.select_db(mybase)
# 执行SQL
cursor.execute(insert into student values(8,李华,男,99,96,95))
# 确认提交
coon.commit()
# 关闭连接
coon.close()运行代码前 运行代码后的效果 如果不想要每次手动通过commit语句提交数据我们可以设置自动提交如下 coon Connection(hostlocalhost, # 主机名(IP)port3306, # 端口(默认端口是3306)userroot, # 账户password123456, # 密码autocommitTrue # 设置为自动提交
) 【综合案例】 我们将前面提到的销售额的数据全部导入MySQL中数据来源在前面的博客中提到过http://t.csdnimg.cn/96mXK 一.创建数据库创建表 二.读取数据类的设计 读取数据的代码设计我们沿用前面的面向对象的设计的代码http://t.csdnimg.cn/96mXK data_define
数据定义的类class Record:def __init__(self,data,order_id,money,province):self.data data # 订单日期self.order_id order_id # 订单idself.money money # 订单金额self.province province # 销售省份def __str__(self):return f{self.data},{self.order_id},{self.money},{self.province}file_define
和文件相关的定义from data_define import *
import json
# 先定义一个抽象类用来做顶层设计,确定有那些需要实现的功能
class FileReader:def read_data(self) - list[Record]:读取文件的数据读到的每一条数据都转换为Order对象将他们封装到list内返回即可:return:pass # 抽象方法class TextFileReader(FileReader): # 用来读取普通文件数据的方法def __init__(self,path):self.path path # 定义成员变量记录文件路径# 复写实现抽象方法父类的方法def read_data(self) - list[Record]:f open(self.path,r,encodingUTF-8)record_list:list[Record] []for line in f.readlines(): # readlines()一次性读取文件的每一行内容返回的是列表line line.strip() # 消除读取到的每一行的换行符data_list line.split(,)record Record(data_list[0],data_list[1],int(data_list[2]),data_list[3]) # 构建为Order对象record_list.append(record)f.close()return record_listclass JsonFileReader(FileReader): # 用来读取JSON文件数据的方法def __init__(self,path):self.path pathdef read_data(self) - list[Record]:f open(self.path,r,encodingUTF-8)record_list:list[Record] []for line in f.readlines(): # readlines()一次性读取文件的每一行内容返回的是列表data_dict json.loads(line)record Record(data_dict[date],data_dict[order_id],data_dict[money],data_dict[province],) # 构建为Order对象record_list.append(record)f.close()return record_listif __name__ __main__:text_file_reader TextFileReader(D:/网盘下载的文件/2011年1月销售数据.txt)jison_file_reader JsonFileReader(D:/网盘下载的文件/2011年2月销售数据JSON.txt)list1 text_file_reader.read_data()list2 jison_file_reader.read_data()for l1 in list1:print(l1)for l2 in list2:print(l2)三.读取数据插入到数据库MySQL
1.设计一个类可以完成数据封装2.设计一个抽象类定义文件读取的相关功能并使用子类实现具体功能3.读取文件生产数据对象4.进行数据需求的逻辑计算计算每一天的销售额5.插入到MySQL# 导包
from file_define import *
from data_define import *
from pymysql import Connection
# 创建文件对象获取文件
text_file_reader TextFileReader(D:/网盘下载的文件/2011年1月销售数据.txt)
jison_file_reader JsonFileReader(D:/网盘下载的文件/2011年2月销售数据JSON.txt)jen_data:list[Record] text_file_reader.read_data() # 一月份的数据
feb_data:list[Record] jison_file_reader.read_data() # 二月份的数据# 将两个月份的数据合并
all_data:list[Record] jen_datafeb_dataconn Connection(hostlocalhost,port3306,userroot,passwd123456,autocommitTrue
)
# 获取游标对象
cursorconn.cursor()
# 选择数据库
conn.select_db(py_sql)
# 组织SQL语句
for record in all_data:sqlfinsert into orders values({record.data},{record.order_id},{record.money},{record.province})# 执行SQL语句cursor.execute(sql)
# 关闭连接
conn.close()运行效果