In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly shows you "how Sanic connects to postgresql database", the content is simple and easy to understand, clear organization, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how Sanic connects to postgresql database" this article.
1. Install System Pack
# yum install postgresql-devel
2. Install Python packages
peewee-2.8.5.tar.gz
psycopg2-2.6.2.tar.gz
1). Install 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). Install 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. directory structure
/home/webapp
|-- main.py
|-- my_blueprint.py
templates
|-- index.html
4. Document content:
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
id
name
{% for item in items %}
` item`.` id `
` item`.` name `
{% endfor %}
#
5. Browser Run Results
The above is "Sanic how to connect postgresql database" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.