🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# python3使用alchemy操作mysql完成代码 ``` <pre class="calibre15">``` <span class="token2">from</span> flask <span class="token2">import</span> Flask <span class="token2">from</span> flask_sqlalchemy <span class="token2">import</span> SQLAlchemy <span class="token2">import</span> config #以上导入所需模块 app <span class="token">=</span> <span class="token3">Flask</span><span class="token1">(</span>__name__<span class="token1">)</span> #实例化flask app<span class="token1">.</span>config<span class="token1">.</span><span class="token3">from_object</span><span class="token1">(</span>config<span class="token1">)</span>#引入数据库配置 db <span class="token">=</span> <span class="token3">SQLAlchemy</span><span class="token1">(</span>app<span class="token1">)</span> #实例化sqlalchemy db<span class="token1">.</span><span class="token3">create_all</span><span class="token1">(</span><span class="token1">)</span> #测试观察是否有错误 #创建表名 <span class="token2">class</span> <span class="token3">Article</span><span class="token1">(</span>db<span class="token1">.</span>Model<span class="token1">)</span><span class="token1">:</span> __tablename__ <span class="token">=</span> <span class="token4">'article'</span> id <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Integer<span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">,</span>autoincrement<span class="token">=</span>True<span class="token1">)</span> title <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span><span class="token3">String</span><span class="token1">(</span><span class="token5">100</span><span class="token1">)</span><span class="token1">,</span>nullable<span class="token">=</span>False<span class="token1">)</span> content <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Text<span class="token1">,</span>nullable<span class="token">=</span>False<span class="token1">)</span> db<span class="token1">.</span><span class="token3">create_all</span><span class="token1">(</span><span class="token1">)</span> @app<span class="token1">.</span><span class="token3">route</span><span class="token1">(</span><span class="token4">'/'</span><span class="token1">)</span> def <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> # # 增加数据 # article1 <span class="token">=</span> <span class="token3">Article</span><span class="token1">(</span>title<span class="token">=</span><span class="token4">'aaa'</span><span class="token1">,</span> content<span class="token">=</span><span class="token4">'bbb'</span><span class="token1">)</span> # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>article1<span class="token1">)</span> # # 事务 # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">commit</span><span class="token1">(</span><span class="token1">)</span> #查找数据 # article1 <span class="token">=</span> Article<span class="token1">.</span>query<span class="token1">.</span><span class="token3">filter</span><span class="token1">(</span>Article<span class="token1">.</span>title <span class="token">==</span> <span class="token4">'aaa'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">first</span><span class="token1">(</span><span class="token1">)</span> # <span class="token3">print</span><span class="token1">(</span><span class="token4">'title:%s'</span> <span class="token">%</span> article1<span class="token1">.</span>title<span class="token1">)</span> # <span class="token3">print</span><span class="token1">(</span><span class="token4">'content: %s'</span> <span class="token">%</span> article1<span class="token1">.</span>content<span class="token1">)</span> # #该数据 # article1 <span class="token">=</span> Article<span class="token1">.</span>query<span class="token1">.</span><span class="token3">filter</span><span class="token1">(</span>Article<span class="token1">.</span>title <span class="token">==</span><span class="token4">'aaa'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">first</span><span class="token1">(</span><span class="token1">)</span> # article1<span class="token1">.</span>title <span class="token">=</span> <span class="token4">'new title'</span> # #事务提交 # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">commit</span><span class="token1">(</span><span class="token1">)</span> #删除数据 article1 <span class="token">=</span> Article<span class="token1">.</span>query<span class="token1">.</span><span class="token3">filter</span><span class="token1">(</span>Article<span class="token1">.</span>title <span class="token">==</span> <span class="token4">'new title'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">first</span><span class="token1">(</span><span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token2">delete</span><span class="token1">(</span>article1<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">commit</span><span class="token1">(</span><span class="token1">)</span> <span class="token2">return</span> <span class="token4">'hello'</span> <span class="token2">if</span> __name__ <span class="token">==</span> <span class="token4">'__main__'</span><span class="token1">:</span> app<span class="token1">.</span><span class="token3">run</span><span class="token1">(</span>debug<span class="token">=</span>True<span class="token1">)</span> ``` ```