化妆品品牌网站如何做,建筑网人才,广州做外贸网站多少钱,wordpress 取消评论一、环境
工作中需要用到python和mysql数据库#xff0c;本次文档记录相关操作。
环境#xff1a;windows10、python 3.11.7 mysql版本#xff1a;5.7 二、MySQL的连接和使用
本人使用过的两种方式
2.1方式一#xff1a;sql为主
2.1.1创建连接
import sqlalchemy
fro…一、环境
工作中需要用到python和mysql数据库本次文档记录相关操作。
环境windows10、python 3.11.7 mysql版本5.7 二、MySQL的连接和使用
本人使用过的两种方式
2.1方式一sql为主
2.1.1创建连接
import sqlalchemy
from sqlalchemy.orm import scoped_session, sessionmakerUSERNAME root # 用户名
PASSWORD 123456 # 密码
ADDR localhost # 连接地址 本地localhost 服务器就是服务器的地址XXX
PORT 3306 # mysql端口号
DATABASE test # 连接的数据库名class MysqlSession:def __init__(self):self.create_connection()def create_connection(self):engine sqlalchemy.create_engine(fmysqlpymysql://{USERNAME}:{PASSWORD}{ADDR}:{PORT}/{DATABASE}?charsetutf8,max_overflow50, # 超过连接池大小外最多创建的连接pool_size50, # 连接池大小pool_recycle30 # 多久之后对线程池中的线程进行一次连接的回收重置,, echoFalse)self.connection scoped_session(sessionmaker(bindengine))def get_connection(self):return self.connectiondef ins(self, sql): # 新增数据return self.connection.execute(sql).lastrowiddef arr(self, sql): # 查询多条数据return self.connection.execute(sql).fetchall()def obj(self, sql): # 查询单个数据return self.connection.execute(sql).fetchone()def upd(self, sql): # 修改单个数据return self.connection.execute(sql)def dlt(self, sql): # 删除数据return self.connection.execute(sql)def commit(self):self.connection.commit() # 提交self.connection.remove() # 结束会话
2.1.2 表结构 2.1.3 新增数据
mysql_session MysqlSession()
connection mysql_session.get_connection()
table_name test.user # 表名这里是数据库名表名
# 新增数据返回的该条数据的id
name_remark [{name: Alice, remark: 可能是个女生},{name: Bob, remark: 可能是个男生},{name: Tammi}
]
new_ids []
for p in name_remark:name p.get(name)remark p.get(remark)sql finsert into {table_name} (name,remark) values ({name},{remark})user_id mysql_session.ins(sql)new_ids.append(user_id)
print(new_ids)
mysql_session.commit() 2.1.4 查看数据
# 查看所有数据
sql fselect * from {table_name}
users mysql_session.arr(sql)
for u in users:print(u) # 查看指定数据
sql fselect * from {table_name} where nameAlice
user mysql_session.obj(sql)
print(user) 2.1.5 修改数据
# 修改指定数据
sql fupdate {table_name} set nameAlice_changed where id16
mysql_session.upd(sql)
mysql_session.commit()
# 查看刚才修改的数据
sql fselect * from {table_name} where id16
user mysql_session.obj(sql)
print(user) 2.1.6 删除数据
# 删除指定数据
sql fdelete from {table_name} where id16
mysql_session.dlt(sql)
mysql_session.commit()
# 查看所有数据
users show_test(mysql_session, table_name)
print(users) 2.2方式二orm对象关系映射
因为现目前工作中没有用到这个以前用django的时候有用到过orm这里就简单记录一下测试情况。
2.2.1 mysql连接
mysql_session.py
import sqlalchemy
from sqlalchemy.orm import sessionmakerUSERNAME root # 用户名
PASSWORD 123456 # 密码
ADDR localhost # 连接地址 本地localhost 服务器就是服务器的地址XXX
PORT 3306 # mysql端口号
DATABASE test # 连接的数据库名class MysqlSession:def __init__(self):self.create_connection()def create_connection(self):self.engine sqlalchemy.create_engine(fmysqlpymysql://{USERNAME}:{PASSWORD}{ADDR}:{PORT}/{DATABASE}?charsetutf8,max_overflow50, # 超过连接池大小外最多创建的连接pool_size50, # 连接池大小pool_recycle30, # 多久之后对线程池中的线程进行一次连接的回收重置,echoFalse)Session sessionmaker(bindself.engine)self.session Session()def get_session(self):return self.sessiondef get_engine(self):return self.engine2.2.2 创建表
models.py
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_basefrom mysql_session import MysqlSessionBase declarative_base()class User(Base):__tablename__ personid Column(Integer, primary_keyTrue, autoincrementTrue) # 主键 自增name Column(String(255))age Column(Integer)engine MysqlSession().get_engine()
Base.metadata.create_all(engine) #创建上面的表运行一次即可2.2.3 新增数据
from models import Person
from mysql_session import MysqlSessionsession MysqlSession().get_session()# 新增几条数据
new_user Person(name张三, age30)
session.add(new_user)
new_user Person(name李四, age40)
session.add(new_user)
new_user Person(name王五, age50)
session.add(new_user)
session.commit() 2.2.4 查询数据
# 查询所有数据
users session.query(Person).all()
for user in users:print(user.name, user.age)
print(**************************************)
# 指定信息
user session.query(Person).filter_by(name李四).first()
print(user.name, user.age) 2.2.5 修改数据
# 修改数据
user session.query(Person).filter_by(name李四).first()
if user:user.age 400 # 将年龄修改为400session.commit()print(updated success.)print(user.name, user.age)
else:print(not found.) 2.2.6 删除数据
# 删除所有年龄大于100的用户
users session.query(Person).filter(Person.age 100).all()
for u in users:print(u.name, u.age)
for user in users:session.delete(user)
session.commit() 文章转载自: http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.zmpsl.cn.gov.cn.zmpsl.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.srbmc.cn.gov.cn.srbmc.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.gnwse.com.gov.cn.gnwse.com http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.rswfj.cn.gov.cn.rswfj.cn http://www.morning.jrhcp.cn.gov.cn.jrhcp.cn http://www.morning.dpsyr.cn.gov.cn.dpsyr.cn http://www.morning.kxrld.cn.gov.cn.kxrld.cn http://www.morning.ctfwl.cn.gov.cn.ctfwl.cn http://www.morning.rchsr.cn.gov.cn.rchsr.cn http://www.morning.jrkzk.cn.gov.cn.jrkzk.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.pngph.cn.gov.cn.pngph.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.qfgwx.cn.gov.cn.qfgwx.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.nnmnz.cn.gov.cn.nnmnz.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.nysjb.cn.gov.cn.nysjb.cn http://www.morning.ghcfx.cn.gov.cn.ghcfx.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.lrplh.cn.gov.cn.lrplh.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.bnzjx.cn.gov.cn.bnzjx.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.pudejun.com.gov.cn.pudejun.com http://www.morning.kxbry.cn.gov.cn.kxbry.cn http://www.morning.dpbgw.cn.gov.cn.dpbgw.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.mhsmj.cn.gov.cn.mhsmj.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.nlkhr.cn.gov.cn.nlkhr.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.pxmyw.cn.gov.cn.pxmyw.cn http://www.morning.prddj.cn.gov.cn.prddj.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.nrrzw.cn.gov.cn.nrrzw.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.dnmgr.cn.gov.cn.dnmgr.cn http://www.morning.kkhf.cn.gov.cn.kkhf.cn http://www.morning.mbrbg.cn.gov.cn.mbrbg.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.qpsft.cn.gov.cn.qpsft.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.rlxnc.cn.gov.cn.rlxnc.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.rjmg.cn.gov.cn.rjmg.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.ytbr.cn.gov.cn.ytbr.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.fyglg.cn.gov.cn.fyglg.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn