数据库操作

2022-09-14 18:36:17 浏览数 (1)

一、数据库迁移

1、安装

pip install flask-script

pip install flask-migrate

2、创建迁移对象

代码语言:javascript复制
<span class="hljs-keyword">from</span> flask_script <span class="hljs-keyword">import</span> Manager
<span class="hljs-keyword">from</span> flask_migrate <span class="hljs-keyword">import</span> Migrate, MigrateCommand

migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command(<span class="hljs-string">"db"</span>, MigrateCommand)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">"__main__"</span>:
    manager.run()

3、创建迁移文件目录

python manage.py db init

结果:在工程目录下自动生成一个名为migrations的目录

4、生成迁移文件

python manage.py db migrate -m “说明信息,注意不能有中文”

代码语言:javascript复制
python manage.py db migrate -m <span class="hljs-string">"first"</span>

结果:在versions目录下生成迁移文件

5、执行迁移(更新数据库)

python manage.py db upgrade

6、查看迁移历史版本

python manage.py db history

二、普通方式数据增删改

注意

  • sqlalchemy默认是开启了事务处理
  • 每次操作完需要db.session.commit 或者 db.session.rollback()

1、添加数据

添加一条数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/insert/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">try</span>:
        u = User(uusername=<span class="hljs-string">'lucky'</span>, usex=<span class="hljs-keyword">True</span>, uage=<span class="hljs-number">18</span>, uinfo=<span class="hljs-string">'lucky个人信息'</span>)
        db.session.add(u)
        db.session.commit()
    <span class="hljs-keyword">except</span>:
        db.session.rollback()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'添加一条数据'</span>

添加多条数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/insert_many/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_many</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">try</span>:
    	  u1 = User(uusername=<span class="hljs-string">'lucky'</span>, usex=<span class="hljs-keyword">True</span>, uage=<span class="hljs-number">18</span>, uinfo=<span class="hljs-string">'lucky个人信息'</span>)
        u1 = User(uusername=<span class="hljs-string">'迪丽热巴'</span>, usex=<span class="hljs-keyword">False</span>, uage=<span class="hljs-number">28</span>, uinfo=<span class="hljs-string">'迪丽热巴个人信息'</span>)
        db.session.add_all([u1, u2])
    		db.session.commit()
     <span class="hljs-keyword">except</span>:
        db.session.rollback()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'添加多条数据'</span>

2、修改数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/update/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update</span><span class="hljs-params">()</span>:</span>
    u = User.query.get(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">try</span>:
        u.uusername = <span class="hljs-string">'杨幂'</span>
        db.session.add(data)
        db.session.commit()
    <span class="hljs-keyword">except</span>:
        db.session.rollback()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'修改'</span>

3、删除数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/delete/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">try</span>:
        data = User.query.get(<span class="hljs-number">1</span>)
        db.session.delete(data)
        db.session.commit()
    <span class="hljs-keyword">except</span>:
        db.session.rollback()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'删除数据'</span>

三、定义增删改基础类与使用

1、定义类

db.py

代码语言:javascript复制
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DBParent</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-comment"># 添加和修改的方法</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">save</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">try</span>:
            db.session.add(self) <span class="hljs-comment"># 添加对象到数据库 self代表当前类的实例化的对象</span>
            db.session.commit()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span> <span class="hljs-comment">#成功 返回 True</span>
        <span class="hljs-keyword">except</span>:
            db.session.rollback()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span> <span class="hljs-comment"># 失败回滚返回False</span>
    <span class="hljs-comment"># 添加所有的方法</span>
<span class="hljs-meta">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">saveAll</span><span class="hljs-params">(*args)</span>:</span>
        <span class="hljs-keyword">try</span>:
            db.session.add_all(*args)
            db.session.commit()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span>
        <span class="hljs-keyword">except</span>:
            db.session.rollback()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
    <span class="hljs-comment"># 删除的方法</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">try</span>:
            db.session.delete(self)
            db.session.commit()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">True</span>
        <span class="hljs-keyword">except</span>:
            db.session.rollback()
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">False</span>
db.DBParent = DBParent

模型继承DBParent

代码语言:javascript复制
<span class="hljs-keyword">from</span> exts <span class="hljs-keyword">import</span> db
<span class="hljs-comment"># 继承db.DBParent,就拥有了自定义类的方法</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span><span class="hljs-params">(db.Model, db.DBParent)</span>:</span>
		...

2、使用

添加数据

添加一条数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/insert/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert</span><span class="hljs-params">()</span>:</span>
    u = User(uusername=<span class="hljs-string">'lucky'</span>, usex=<span class="hljs-keyword">True</span>, uage=<span class="hljs-number">18</span>, uinfo=<span class="hljs-string">'lucky个人信息'</span>)
    u.save() 
    <span class="hljs-keyword">return</span> <span class="hljs-string">'添加一条数据'</span>

添加多条数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/insert_many/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">insert_many</span><span class="hljs-params">()</span>:</span>
		u1 = User(uusername=<span class="hljs-string">'lucky'</span>, usex=<span class="hljs-keyword">True</span>, uage=<span class="hljs-number">18</span>, uinfo=<span class="hljs-string">'lucky个人信息'</span>)
    u1 = User(uusername=<span class="hljs-string">'迪丽热巴'</span>, usex=<span class="hljs-keyword">False</span>, uage=<span class="hljs-number">28</span>, uinfo=<span class="hljs-string">'迪丽热巴个人信息'</span>)
    User.saveAll(u1, u2)
    <span class="hljs-keyword">return</span> <span class="hljs-string">'添加多条数据'</span>

修改数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/update/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">update</span><span class="hljs-params">()</span>:</span>
    u = User.query.get(<span class="hljs-number">1</span>)
    u.uusername = <span class="hljs-string">'lucky'</span>
    u.save()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'修改'</span>

删除数据

代码语言:javascript复制
<span class="hljs-meta">@view.route('/delete/')</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">delete</span><span class="hljs-params">()</span>:</span>
    u = User.query.get(<span class="hljs-number">1</span>)
    u.delete()
    <span class="hljs-keyword">return</span> <span class="hljs-string">'删除'</span>

0 人点赞