日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Sanic 连接postgresql数据库

發(fā)布時間:2025/3/17 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sanic 连接postgresql数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.安裝系統(tǒng)包

# 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.目錄結(jié)構(gòu)

/home/webapp

? ? ?|--?main.py

? ? ?|--?my_blueprint.py

? ? ?templates

? ? ? ? |--?index.html



4.文件內(nèi)容:


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?

<!doctype html>

<title> Sanic </title>

<div class=page>

? <table border="1" cellpadding="10">

? <tr>

? ? <th>id</th>

? ? <th>name</th>

? </tr>

? {% for item in items %}

? ? <tr>

? ? <td> ` item`.`id ` </td>

? ? <td> ` item`.`name ` </td>?

? ? </tr>

? {% endfor %}

? </table>

</div>

#?


5.瀏覽器運行結(jié)果



本文轉(zhuǎn)自 pgmia 51CTO博客,原文鏈接:http://blog.51cto.com/heyiyi/1882032



新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!

總結(jié)

以上是生活随笔為你收集整理的Sanic 连接postgresql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。