python之数据库管理工具sandman2

 文档:Welcome to sandman2’s documentation! — sandman2 0.0.1 documentation

【安装】

pip install sandman2

 

安装成功后,就可以得到一个 sandman2ctl 命令行工具,用它来启动一个 RESTful API 服务器

sandman2ctl sqlite+pysqlite:///data.db

启动之后,默认端口是 5000,访问地址是 http://localhost:5000/admin 就能看到服务器控制台了

 

 【数据库连接】

sandman2 是基于 SQLAlchemy 的,所以使用连接 Url 来连接数据库

格式为

dialect+driver://username:password@host:port/database

  • dialect 为数据库类型,如 mysql、SQLite 等

  • driver 为数据库驱动模块名,例如 pymysql、psycopg2、mysqldb 等,如果忽略,表示使用默认驱动

 

例如:MySQL

sandman2ctl 
 mysql+pymysql://bob:bobpasswd@localhost:3306/testdb

注意点:如果环境中没有安装 pymysql 模块,必须先安装,才能正常启动,其他数据库的连接方式可参考 SQLAlchemy 的 引擎配置 章节, 在这里查看 https://docs.sqlalchemy.org/en/13/core/engines.html

 

控制台

需要快速预览数据,对数据进行简单调整的话,控制台很有用

左侧菜单除了 Home 外,其他的都是库表名称

点击相应库表名称,会在右侧显示表内数据,并且可以做增删改操作

 

 

API

以 RESTful 的角度来看,库表相当于资源(resource),一组资源相当于集合(collection)

 

查询

通过 Http GET 方法,以 JSON 格式将数据返回,例如返回 学生表 student 的所有记录:

$ curl http://localhost:5000/student/
 
 
{"resources":[{"age":18,"class":"1","id":1,"name":"\u5f20\u4e09","profile":"\u64c5\u957f\u5b66\u4e60"},...

注意:资源要以 / 结尾

 

通过参数 page 来分页,例如返回 学生表 student 的第一页数据

$ curl http://localhost:5000/student/?page=1
{"resources":[{"age":18,"class":"1"...

通过参数 limit 显示返回行数

如果要获取具体记录,可以用主键值作为节段,例如获取 id 为 3 的学生记录

$ curl http://localhost:5000/student/3
{"age":18,"class":"2","id":3,"name":"\u738b\u4e94","profile":"\u7231\u7f16\u7a0b"}

以字段名做参数,相当于查询条件,例如,查询 name 为 Tom 的学生记录:

$ curl http://localhost:5000/student/?name=Tom
{"resources":[{"age":19,"class":"1","id":7,"name":"Tom","profile":"Handsome"}]}

 

查询条件可以被组合,例如,查询班级为 1 年龄为 18 的学生:

$ curl http://localhost:5000/student/?class=1&age=19
{"resources":[{"age":19,"class":"1","id":2,"name":"\u674e\u56db","profile":"\u559c\u6b22\u7bee\u7403"},{"age":19,"class":"1","id":7,"name":"Tom","profile":"Handsome"}]}

 

修改

POST 方法用于新增,新增内容,由请求的数据部分提供,例如增加一个学生信息:

$ curl -X POST -d '{"name": "Lily", "age": 17, "class":1, "profile":"Likely"}' -H "Content-Type: application/json" http://127.0.0.1:5000/student/
{"age":17,"class":"1","id":8,"name":"Lily","profile":"Likely"}

 

注意:库表主键是自增长的,可以忽略主键字段,否则必须提供

PATCH 方法用于更新,更新内容,由请求的数据部分提供,例如将 id 为 1 的学生班级更改为 3

注意: 更新时主键信息通过 url 的主键值节段提供,而不在数据部分中

$ curl -X PATCH -d '{"class":3}' -H "Content-Type: application/json" http://127.0.0.1:5000/student/1
{"age":18,"class":"3","id":1,"name":"\u5f20\u4e09","profile":"\u64c5\u957f\u5b66\u4e60"}

 

DELETE 方法由于删除,例如删除 id 为 8 的学生记录:

$ curl -X DELETE -H "Content-Type: application/json" http://127.0.0.1:5000/student/8

 

其他接口

获取表的字段定义信息,通过 meta 节段获取,例如获取 学生表 student 的字段定义:

$ curl http://127.0.0.1:5000/student/meta
{"age":"INTEGER(11)","class":"VARCHAR(255)","id":"INTEGER(11) (required)","name":"VARCHAR(255)","profile":"VARCHAR(500)"}

  

导出数据,通过查询字段 export 获取,数据格式为 csv,例如导出学生数据,存放到 student.csv 文件中:

 

$ curl -o student.csv http://127.0.0.1:5000/student/?export
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   202  100   202    0     0   2525      0 --:--:-- --:--:-- --:--:--  2525

 

 

 

 

 

 

 

posted @ 2021-09-07 09:47  X-Wolf  阅读(474)  评论(0编辑  收藏  举报