百度空间 | 百度首页 
 
查看文章
 
How to display table creation SQL statement in SQLAlchemy
2009-02-04 12:01
I asked this question in sqlalchemy mail list, and got the answer, you can find in sqlalchemy FAQ.

I copied the example in here:

    from sqlalchemy import *

    from StringIO import StringIO

    buf = StringIO()
    engine = create_engine('postgres://', strategy='mock', executor=lambda s, p='': buf.write(s + p))
    meta = MetaData()
    t1 = Table('sometable', meta, Column('foo', String(30)))
    meta.create_all(engine)

    print buf.getvalue()

The most important thing in above example is the create_engine invoking.

The example uses 'postgress://', and I've tested it with 'sqlite://', and it's different in some column definition. And the strategy='mock' will prevent the tables creation exactly.

So with this help I also added an 'sql' command in Uliweb, so that you can see the creation SQL.

类别:Uliweb | | 添加到搜藏 | 分享到i贴吧 | 浏览() | 评论 (2)
 
最近读者:
 
网友评论:
1
2009-02-04 16:11 | 回复
sqlalchemy是好东西,但是有些功能弱了一点,还是要用 engine.execute() 来实现.
 
2
2009-02-04 16:15 | 回复
这不是挺好的吗?ORM解决不了所有的问题。uliweb orm在简单意义上来说可以认为是另一种构建表的方式。每一个Model都有一个table属性,就是对应的实现的sqlalchemy中的Table。所以可以方便使用纯sqlalchemy来对其进行操作。
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2010 Baidu