johnnierunner是活动记录模式o/r映射器的sqlalchemy包装器

JohnnieRunner的Python项目详细描述


johnnierunner是活动记录模式o/r映射器的sqlalchemy包装器

依赖关系

  • python2.7或更高版本
  • sqlalchemy>;=1.0.8
  • mysql python>;=1.2.5

支持RDBMS

  • mysql

目标

  • 添加类似activerecord api的rails
  • 数据库迁移
  • sqlalchemy rdbms的所有支持

用法

准备

  1. 生成测试数据库

    $ mysql -u root -p
    mysql> create database johnnie;
    Query OK, 1 row affected (0.01 sec)
    
    mysql> exit;
    Bye
    
  2. 定义o/r映射器结构

    fromjohnnieimportAbstractModel,create_sessionfromjohnnie.typesimportColumn,String,Integersession=create_session('localhost','root','root user password','johnnie')classRepositories(AbstractModel):classMeta:session=sessionid=Column('id',Integer(unsigned=True),primary_key=True)name=Column('name',String(255),nullable=False)author=Column('author',String(255),nullable=False)url=Column('url',String(255),nullable=False)
  3. 创建模型表

    $ python
    >>> from hoge import Repositories
    >>> Repositories.metadata.create_all(Repositories.get_session_engine())
    >>> exit()

示例

  1. 创建实体

    >>> from hoge import Repositories
    >>> entity= Repositories(name="JohnnieRunner", author="teitei-tk", url="https://github.com/teitei-tk/JohnnieRunner")
    
    or
    
    >>> entity_data={"name": "JohnnieRunner", "author": "teitei-tk", "url": "https://github.com/teitei-tk/JohnnieRunner"}
    >>> entity= Repositories.new(entity_data)
  2. 创建记录

    >>> entity.save()
    True
    
    mysql>select*fromrepositories;+----+---------------+-----------+--------------------------------------------+
    |id|name|author|url|+----+---------------+-----------+--------------------------------------------+
    |1|JohnnieRunner|teitei-tk|https://github.com/teitei-tk/JohnnieRunner|+----+---------------+-----------+--------------------------------------------+
    1rowinset(0.00sec)mysql>
  3. 读取记录

    >>> entity= Repositories.get(1)
    
    or
    
    >>> entity= Repositories.find(1)
    
    >>> entity.name
    u"JohnnieRunner"
    >>> entity.author
    u"teitei-tk"
    >>> entity.url
    u"https://github.com/teitei-tk/JohnnieRunner"
  4. 更新记录

    >>> entity.name = u"update_test"
    >>> entity.save()
    True
    
    mysql>select*fromrepositories;+----+-------------+-----------+--------------------------------------------+
    |id|name|author|url|+----+-------------+-----------+--------------------------------------------+
    |1|update_test|teitei-tk|https://github.com/teitei-tk/JohnnieRunner|+----+-------------+-----------+--------------------------------------------+
    mysql>
  5. 删除记录

    >>> entity.delete()
    True
    
    mysql>select*fromrepositories;Emptyset(0.00sec)

待办事项

  • []添加数据库数据类型
  • []轻松生成表索引
  • []简单的表关系
  • []数据库迁移
  • []RDBMS支持MySQL以外的其他功能

许可证

  • 麻省理工学院

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java需要设置框架。可设置大小(false)以重新绘制()   java我对PDF文件感到困惑   为什么是太阳。jvm。热点。调试器。DebuggerException:无法打开二进制文件`?   设置结果为textview时出现java空指针异常   我应该使用什么同步原语在Java中实现事件驱动程序框架?   java为什么WindowClosing处理程序在退出程序之前不执行后台任务?   如何将“20170712T18:43:04.000Z”转换为安卓或java中的相对时间?   Java,获取按键的时间长度,currentTimeMillies()始终为24   maven构建的java可执行Jar找不到logback。xml   java在其外部的函数中使用for循环中的值   java如何以表格格式将不同长度的数据对齐   java Play 2.5 WebSocket连接构建   maven而非eclipse的java强制转换问题   java如何在JFreeChart中使X轴上的值水平?   构建Java Windows应用程序以访问在线MySQL数据库需要什么   java添加构造函数会出错吗?这没有道理,请帮忙,编程问题   java在一个jframe中的两个JPanel中使用两个绘制方法   java数学或逻辑问题   java如何复制Androids库存摄像头方向更改