1.安装包

此处我使用的是pipenv虚拟环境,故命令如下

  1. pipenv install pymysql

2.配置连接

这里需要注意charset编码一定不要带-,比如utf-8

  1. connect = pymysql.Connect(
  2. host='localhost',
  3. port=3306,
  4. user='root',
  5. passwd='666666',
  6. db='race_flask',
  7. charset='utf8'
  8. )

3.获取连接、游标

  1. def start_conn(self):
  2. cursor = self.connect.cursor()
  3. return self.connect,cursor

4.增删改查

这里需要注意,对数据库数据进行操作时,需要调用commit()方法进行提交

  1. # 插入数据
  2. def insert_db(self,cursor,*args):
  3. sql = 'insert into test_student(sid,name,sex) values ("%s","%s","%s")'
  4. cursor.execute(sql % args)
  5. cursor.commit()
  6. # 删除数据
  7. def del_data(self,cursor,*args):
  8. sql = 'delete from test_student where "%s" = "%s" '
  9. cursor.execute(sql % args)
  10. # 修改数据
  11. def updata_db(cursor,*arg):
  12. sql = 'update test_student set name="%s",sex="%s" where id = 1 '
  13. cursor.execute(sql % arg)
  14. cursor.commit()
  15. # 查询全部
  16. def query_all(self,cursor):
  17. sql = 'select * from test_student'
  18. cursor.execute(sql)
  19. return cursor.fetchall()
  20. # 查询数据
  21. def query_data(self,cursor,* args):
  22. print(args)
  23. sql = 'select * from test_student where "%s" = "%s" '
  24. cursor.execute(sql % args)
  25. return cursor.fetchall()

5.关闭游标、连接

  1. def close_connn(self,cursor,connect):
  2. cursor.close()
  3. connect.close()

6.完整代码如下

  1. # -*- coding:utf-8 -*-
  2. # Created by ZhaoWen on 2020/10/17
  3. import pymysql
  4. class common_sql():
  5. connect = pymysql.Connect(
  6. host='localhost',
  7. port=3306,
  8. user='root',
  9. passwd='666666',
  10. db='race_flask',
  11. charset='utf8'
  12. )
  13. # 连接数据库 获取连接 游标
  14. def start_conn(self):
  15. cursor = self.connect.cursor()
  16. return self.connect,cursor
  17. # 插入数据
  18. def insert_db(self,cursor,*args):
  19. sql = 'insert into test_student(sid,name,sex) values ("%s","%s","%s")'
  20. cursor.execute(sql % args)
  21. cursor.commit()
  22. # 删除数据
  23. def del_data(self,cursor,*args):
  24. sql = 'delete from test_student where "%s" = "%s" '
  25. cursor.execute(sql % args)
  26. # 修改数据
  27. def updata_db(cursor,*arg):
  28. sql = 'update test_student set name="%s",sex="%s" where id = 1 '
  29. cursor.execute(sql % arg
  30. cursor.commit()
  31. # 查询全部
  32. def query_all(self,cursor):
  33. sql = 'select * from test_student'
  34. cursor.execute(sql)
  35. return cursor.fetchall()
  36. # 查询数据
  37. def query_data(self,cursor,* args):
  38. print(args)
  39. sql = 'select * from test_student where "%s" = "%s" '
  40. cursor.execute(sql % args)
  41. return cursor.fetchall()
  42. # 关闭连接
  43. def close_connn(self,cursor,connect):
  44. cursor.close()
  45. connect.close()

ORM映射方式-SQLAlchemy

1.安装包

  1. pipenv isntall sqlalchemy

2.创建引擎、获取数据库连接

  1. engine = create_engine('mysql://root:666666@localhost:3306/race_flask?charset=utf8')
  2. connect = engine.connect()

3.创建declarative_base实例、并创建模型类

  1. Base = declarative_base()
  1. class student(Base):
  2. __tablename__ = 'test_student'
  3. sid = Column(Integer, primary_key=True)
  4. name = Column(String(20))
  5. sex = Column(String(4))

4.创建session持久会话对象

  1. # 创建持久化session 这个对象可以和数据库进行交互
  2. Session = sessionmaker(bind=engine)
  3. session = Session()

5.增删改查

sqlalchemy对数据库中数据进行修改时,都是通过操作对象的方式进行

  1. # 增加
  2. stu = student(sid='2', name='李四', sex='男')
  3. session.add(stu)
  4. session.commit()
  5. ---
  6. # 修改 通过对象更新数据
  7. stu = session.query(student).filter(student.name=='张三').first()
  8. stu.sex = '女'
  9. session.commit()
  10. ------
  11. # 查询
  12. students = session.query(student).all()
  13. for stu in students:
  14. print(stu)
  15. stu_name = session.query(student.name).all()
  16. print(stu_name)
  17. # 删除
  18. stu = session.query(student).filter(student.sex == '男').first()
  19. session.delete(stu)
  20. session.commit()

6.完整代码

  1. # -*- coding:utf-8 -*-
  2. # Created by ZhaoWen on 2020/10/17
  3. from sqlalchemy import create_engine
  4. from sqlalchemy import Table,Column,Integer,String
  5. from sqlalchemy.ext.declarative import declarative_base
  6. from sqlalchemy.orm import sessionmaker
  7. class sqlalchemy_sql():
  8. engine = create_engine('mysql://root:666666@localhost:3306/race_flask?charset=utf8')
  9. connect = engine.connect()
  10. Base = declarative_base()
  11. Session = sessionmaker(bind=engine)
  12. session = Session()
  13. # 增加
  14. def insert_data(self):
  15. stu = student(sid='2', name='李四', sex='男')
  16. self.session.add(stu)
  17. self.session.commit()
  18. # 删除
  19. def del_Data(self):
  20. stu = self.session.query(student).filter(student.sex == '男').first()
  21. self.session.delete(stu)
  22. self.session.commit()
  23. # 修改 通过对象更新数据
  24. def update_Data(self):
  25. stu = self.session.query(student).filter(student.name == '张三').first()
  26. stu.sex = '女'
  27. self.session.commit()
  28. # 查询
  29. def query_data(self):
  30. students = self.session.query(student).all()
  31. for stu in students:
  32. print(stu)
  33. stu_name = self.session.query(student.name).all()
  34. return stu_name