Sanic如何连接postgresql数据库

这篇文章主要为大家展示了“Sanic如何连接postgresql数据库”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Sanic如何连接postgresql数据库”这篇文章吧。

公司主营业务:成都网站设计、成都网站制作、外贸网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出保德免费做网站回馈大家。

1.安装系统包

# yum install postgresql-devel

2.安装Python包

peewee-2.8.5.tar.gz

psycopg2-2.6.2.tar.gz

1).安装peewee-async

# pip install peewee-async

Collecting peewee-async

  Downloading peewee_async-0.5.6-py3-none-any.whl

Requirement already satisfied: peewee>=2.8.0 in /usr/local/lib/python3.5/site-packages (from peewee-async)

Installing collected packages: peewee-async

Successfully installed peewee-async-0.5.6

#

2).安装aiopg

# pip install aiopg

Collecting aiopg

  Using cached aiopg-0.13.0-py3-none-any.whl

Requirement already satisfied: psycopg2>=2.5.2 in /usr/local/lib/python3.5/site-packages/psycopg2-2.6.2-py3.5-linux-x86_64.egg (from aiopg)

Installing collected packages: aiopg

Successfully installed aiopg-0.13.0

3.目录结构

/home/webapp

     |-- main.py

     |-- my_blueprint.py

     templates

        |-- index.html

4.文件内容:

1).main.py

# more main.py 

from sanic import Sanic

from my_blueprint import bp

app = Sanic(__name__)

app.blueprint(bp)

app.run(host='0.0.0.0', port=8000, debug=True)

2).my_blueprint.py

# more my_blueprint.py 

from sanic import Blueprint

from sanic.response import json, text, html

## Jinja2 template ####

from jinja2 import Environment, PackageLoader

env = Environment(loader=PackageLoader('my_blueprint', 'templates'))

## database ####

import uvloop, peewee

from peewee_async import PostgresqlDatabase

bp = Blueprint('my_blueprint')

# init db connection

global database

database = PostgresqlDatabase(database='webdb',

                              host='127.0.0.1',

                              user='postgres',

                              password='111111')

# router define

@bp.route('/')

async def bp_root(request):

    serialized_obj = []

    cursor = database.execute_sql('select * from t1;')

    for row in cursor.fetchall():

         serialized_obj.append({

            'id': row[0],

            'name': row[1]}

        )

    template = env.get_template('index.html')

    content=template.render(items=serialized_obj)

    return html(content)

#

3).index.html

# more index.html 

Sanic

 

 

   

   

 

  {% for item in items %}

   

   

   

 

   

  {% endfor %}

 

idname
` item`.`id ` ` item`.`name `

5.浏览器运行结果

Sanic如何连接postgresql数据库

以上是“Sanic如何连接postgresql数据库”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文题目:Sanic如何连接postgresql数据库
文章地址:http://bzwzjz.com/article/jccjgs.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设 上市集团网站建设 成都网站建设 成都网站设计 响应式网站建设 网站建设公司 营销型网站建设 营销型网站建设 达州网站设计 成都网站设计 成都网站制作 定制网站设计 成都网站制作 成都营销网站制作 定制网站设计 营销型网站建设 成都网站制作 专业网站设计 网站制作报价 成都网站建设 成都网站设计公司 重庆企业网站建设