增加数据

  1. from sqlalchemy import create_engine
  2. from sqlalchemy.ext.declarative import declarative_base
  3. from sqlalchemy import Column, Integer, String
  4. from sqlalchemy.orm import sessionmaker
  5. # localhost
  6. HOSTNAME = '127.0.0.1'
  7. DATABASE = 'demo'
  8. PORT = 3306
  9. USERNAME = 'root'
  10. PASSWORD = 'root'
  11. DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
  12. engine = create_engine(DB_URL)
  13. Base = declarative_base(engine)
  14. class Article(Base):
  15. __tablename__ = 'article'
  16. id = Column(Integer, primary_key=True, autoincrement=True)
  17. name = Column(String(50), nullable=False)
  18. Base.metadata.create_all()
  19. article = Article(name='ecithy')
  20. article1 = Article(name='fullStack')
  21. print(article.name)
  22. # None
  23. print(article.id)
  24. # 保存到数据库中
  25. # 类的实例化 __call__ 将类变成方法去调用
  26. Session = sessionmaker(bind=engine)
  27. session = Session()
  28. # 添加
  29. session.add(article1)
  30. session.add_all([article, article1])
  31. # 提交
  32. session.commit()

image.png

CURD

  1. # @ Time : 2021/1/31
  2. # @ Author : ecithy
  3. from sqlalchemy import create_engine
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy import Column, Integer, String
  6. from sqlalchemy.orm import sessionmaker
  7. # localhost
  8. HOSTNAME = '127.0.0.1'
  9. DATABASE = 'demo'
  10. PORT = 3306
  11. USERNAME = 'root'
  12. PASSWORD = 'root'
  13. DB_URL = 'mysql+mysqlconnector://{}:{}@{}:{}/{}?charset=utf8'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)
  14. engine = create_engine(DB_URL)
  15. Base = declarative_base(engine)
  16. class Article(Base):
  17. __tablename__ = 'art'
  18. id = Column(Integer, primary_key=True)
  19. title = Column(String(50), nullable=False)
  20. content = Column(String(50))
  21. author = Column(String(50))
  22. def __str__(self):
  23. return "Article(title:{},content:{},author:{})".format(self.title, self.content, self.author)
  24. Base.metadata.create_all()
  25. Session = sessionmaker(bind=engine)
  26. session = Session()
  27. def add_data():
  28. article = Article(title='Python', content='人生苦短,我用Python', author='龟叔')
  29. article1 = Article(title='fullStack', content='人生苦短,我玩全栈', author='ecithy')
  30. # session.add(article)
  31. session.add_all([article, article1])
  32. session.commit()
  33. def search_data():
  34. # all 查询所有
  35. # data = session.query(Article).all()
  36. # for item in data:
  37. # print(item)
  38. # print(item.title)
  39. # print(item.content)
  40. # print(data)
  41. # 条件 filter
  42. data = session.query(Article).filter(Article.title=='Python').all()
  43. for item in data:
  44. print(item, item.title)
  45. # data = session.query(Article).filter_by(title='JAVA').all()
  46. # for item in data:
  47. # print(item)
  48. # print(data)
  49. # 查询第一条
  50. # data = session.query(Article).first()
  51. # print(data)
  52. # get 方法 传的ID 不存在 返回None
  53. # data = session.query(Article).get(1)
  54. # print(data)
  55. def update_data():
  56. # 查询出要修改的这条记录
  57. article = session.query(Article).first()
  58. print(article)
  59. article.title = 'Python'
  60. print(article.title)
  61. # session.rollback() 用于try异常后
  62. # print(article.title)
  63. session.commit()
  64. # 回滚
  65. # A 100 B 0
  66. # A 0 B 100
  67. # 撤回
  68. def delete_data():
  69. # 查询出要修改的这条记录
  70. article = session.query(Article).first()
  71. # 误操作
  72. # is_delete 1 未删除 0 删除
  73. # 修改操作 is_delete 1=>0
  74. session.delete(article)
  75. session.commit()
  76. if __name__ == '__main__':
  77. pass
  78. # add_data()
  79. # search_data()
  80. # update_data()
  81. # delete_data()