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
<!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.浏览器运行结果
本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1882032
Sanic 连接postgresql数据库相关推荐
- Entity Freamwork 6连接PostgreSql数据库
原文 Entity Freamwork 6连接PostgreSql数据库 开发环境 VS 2015 Update 1 Postgre Sql 9.4 使用过程 1.使用Nuget在项目中添加对E ...
- java编译POSTGRESQL_Java连接PostgreSQL数据库(安装环境 + 简易测试代码)
--[楼主比较啰嗦, 会列出几个异常情况以供参考]-- 1, 安装环境 1.0 Win7 32位 1.2 编译器: Spring Tool Suite 3.7.1 官网下载地址:http://sp ...
- typescript-koa-postgresql 实现一个简单的rest风格服务器 —— 连接 postgresql 数据库...
接上一篇,这里使用 sequelize 来连接 postgresql 数据库 1.安装 sequelize,数据库驱动 pg yarn add sequelize sequelize-typescri ...
- python连接postgresql数据库
连接postgresql数据库 pip install psycopg2 ## 导入psycopg2包 import psycopg2 ## 连接到一个给定的数据库 conn = psycopg2.c ...
- 视频教程--ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库
说好的给园子里的朋友们录制与<ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库> 这篇博客相对应的视频,由于一个月一来没有时 ...
- jdbc postgresql mysql_jdbc无法连接postgresql数据库
使用jdbc连接postgresql数据库时报错如下: org.mybatis.spring.MyBatisSystemException: nested exception is org.apach ...
- java jdbc 链接pg_Java使用JDBC连接postgresql数据库示例
本文实例讲述了Java使用JDBC连接postgresql数据库.分享给大家供大家参考,具体如下: package tool; import java.sql.Connection; import j ...
- python通过ssh通道连接PostgreSQL数据库(mysql等类同)
目录 一.问题需求 二.解决办法 一.问题需求 对于有ssh通道设置的数据库连接,遇到已经有一段时间了.之前老是疑惑,使用Navicat之类的工具就可以连接数据库,但用python就是连接不到.之前写 ...
- Python连接postgresql数据库入门
关于Python及pycharm的安装参考:1. python+pycharm 安装及测试_Hehuyi_In的博客-CSDN博客_pycharm安装成功测试 首先需要安装 psycopg2模块(已经 ...
最新文章
- 计算 sigmoid 函数的导数
- 列出本地git仓库中的文件?
- unix网络编程 ubuntu下搭建环境编译源码
- 无法连接本地计算机,xp系统电脑本地连接连不上如何解决
- Centos6.5 安装apache2.4.33部署教程
- 计算机专业 操作系统,计算机操作系统
- Mac上安装fastboot等工具与烧写images
- 关于mac下连接mysql和mysql workbench连接mysql的异常
- C#网易云音乐中需付费歌曲的下载助手。
- 【小游戏】2D游戏你比划我来猜(unity和陀螺仪交互)
- Cisco Packet Tracer 思科模拟器三层交换机配置
- java日志脱敏_java 日志脱敏框架 sensitive,优雅的打印脱敏日志
- PHP的面向对象编程思想
- 进击的Android之manifests
- 使用VScode简易编程
- 2022-04-30前端周报 巴厘岛项目交接.md
- CPT-205 lab04 task
- 小样本不符合正态_抽样检验到底需要多少样本,你真的知道吗?
- jis拉伸试棒图纸_一种加工拉伸试棒的装卡装置的制作方法
- 中科蓝讯-库文件的选择
热门文章
- C/C++socket send函数MSG_NOSIGNAL
- P2转P3时出现‘utf-8‘ codec can‘t decode byte 0xb3 in position 0: invalid start byte(\x、decode解码)
- linux备份还原系统程序
- 进程与multiprocessing模块
- .net大型分布式电子商务架构说明(转载来自头条)
- 工作中遇到的问题--使用注解进行增加删除修改的验证
- PHP实现http与https转化
- 利用CRT库函数检查内存泄漏
- java list分批_Java实用笔记——mybatis批量导入
- rust如何进枪战服_rust手机版