网站建设推销,成都网站建设公司官网,免费网站生成软件,上城区网站建设价格文章目录 一、安装mysql二、SQLyog可视化操作三、python实现数据库单表类封装1. config 文件——config.py2. 封装类#xff08;model#xff09;——model.py3. 测试文件——test.py 一、安装mysql
官网安装#xff0c;或者Windows64位直接在我的资源里面上传了mysql… 文章目录 一、安装mysql二、SQLyog可视化操作三、python实现数据库单表类封装1. config 文件——config.py2. 封装类model——model.py3. 测试文件——test.py 一、安装mysql
官网安装或者Windows64位直接在我的资源里面上传了mysql不是8最新但是使用足够了。介意者转官网下载 安装之后记得在服务里面开启mysql的服务开启之后皆可以用了如果想要方便一点记得去配置一下全局路径。不详写了不知道的搜搜或者评论交流。
二、SQLyog可视化操作
不想写sql语句的不建议该写的还是要写的或者是为了方便看数据可以下载SQLyog还有其他的软件也可以直接在github上找绿色版就可以下到安装右手就会。
三、python实现数据库单表类封装
文件结构
1. config 文件——config.py
记录数据库的信息
# 数据库配置信息
HOST localhost
USER root
PASSWD
DBNAME mydb
PORT 33062. 封装类model——model.py
实现单表的增删改查
import pymysql # pip install pymysql
from db import configclass Model:单表信息操作类tab_name None # 表名link None # 数据库连接对象cursor None # 游标对象pk id # 表的主键名fields [] # 当前表的字段def __init__(self, table, configconfig):构造函数初始化表名连接数据库try:self.tab_name tableself.link pymysql.connect(hostconfig.HOST, userconfig.USER, passwordconfig.PASSWD, dbconfig.DBNAME,charsetutf8)self.cursor self.link.cursor(pymysql.cursors.DictCursor) # 不加该参数返回元组self.__getFields() #不需要自定义表字段except Exception as error:print(数据库连接异常:, error)def __getFields(self):加载当前表的字段信息私有方法sql SHOW COLUMNS FROM %s % (self.tab_name)self.cursor.execute(sql)dlist self.cursor.fetchall()for v in dlist:self.fields.append(v[Field])if v[Key] PRI:self.pk v[Field]def findAll(self):获取当前表的所有信息返回信息列表没有信息的返回空表try:sql select * from %s % (self.tab_name)print(sql)self.cursor.execute(sql)info_list self.cursor.fetchall()return info_listexcept Exception as error:print(查询数据有异常, error)def findOne(self, data_id):获取指定data_id的单条数据try:sql select * from %s where id %d % (self.tab_name, data_id)print(sql)self.cursor.execute(sql)info self.cursor.fetchone()return infoexcept Exception as error:print(查询数据有异常, error)def select(self, where[], orderNone, limitNone):带条件的信息查询try:sql select * from %s % (self.tab_name)if isinstance(where, list) and len(where) 0:sql where and .join(where)if order is not None:sql order by orderif limit is not None:sql limit str(limit)print(sql)self.cursor.execute(sql)info self.cursor.fetchall()return infoexcept Exception as error:print(error)def save(self, data{}):添加数据方法参数data为字典格式key为表字段名value为数值try:keys []values []if isinstance(data,dict):for k, v in data.items():if k in self.fields:keys.append(k)values.append(v)else:print(数据非字典格式)raise Exception# insert into 表名 字段链表values (值列表)sql insert into %s(%s) values(%s)%(self.tab_name,,.join(keys), ,.join([%s]*len(values)))print(sql)# 与其他直接执行的不太一样先使用占位符之后将列表转换为元组作为参数传入sql与其中的key值一一对应self.cursor.execute(sql,tuple(values))self.link.commit()# 返回插入数据的键值return self.cursor.lastrowidexcept Exception as error:print(添加数据出现错误)def update(self, data{}):修改数据方法参数是字典格式key为表字段名value为数值参数data中要有主键的值为修改条件try:values []if isinstance(data,dict):for k, v in data.items():if (k in self.fields) and (k ! self.pk):values.append(%s %s %(k,v))else:print(数据非字典格式)raise Exception# ,.join(values)会将所有的数值都用连接起来sql update %s set %s where %s%s %(self.tab_name,,.join(values),self.pk,data[id])print(sql)self.cursor.execute(sql)self.link.commit()# 返回插入数据的键值return self.cursor.rowcountexcept Exception as error:print(修改数据出现错误)def delete(self, id0):删除数据参数id为删除的数据主键值try:sql delete from %s where %s%s%(self.tab_name,self.pk,str(id))print(sql)# 与其他直接执行的不太一样先使用占位符之后将列表转换为元组作为参数传入sql与其中的key值一一对应self.cursor.execute(sql)self.link.commit()# 返回插入数据的键值return self.cursor.rowcountexcept Exception as error:print(SQL执行错误)def __del__(self):析构关闭游标和数据库if self.cursor ! None:self.cursor.close()if self.link ! None:self.link.close()3. 测试文件——test.py
# 测试
from db.model import Modelif __name__ __main__:m Model(stu)print(m.fields)# 查看stu表中所有数据# all_info m.findAll()# for i in all_info:# print(i)# 查看stu表中逐渐为8的数据# one_data m.findOne(8)# print(one_data)# 查看stu表格中性别为w,年龄小于50按照年龄递减排序后的前两个数据# select_info m.select(where[sexw,age50],orderage desc,limit2)# for row in select_info:# print(row)# 更新主键id为12的数据name为qq14年龄为30sex为m,classid为python03# a m.update({name:qq14,age:30,sex:m,classid:python03,id:12})# 删除主键为7的数据# print(m.delete(7))# print(m)