有没有可能要使用数据库的时候临时指定使用哪个呢?

8 views
Skip to first unread message

Chunlin Zhang

unread,
Apr 26, 2015, 11:10:01 AM4/26/15
to uli...@googlegroups.com
其实我是想知道能不能做到这样:比如说我的应用里有很多项目(这些项目可能会动态创建)那么能不能做到每个项目对应一个数据库(比如说每个项目自己一个sqlite文件)

看多数据库的说明应该目前是不好做到,但有可能做到这样么?

limodou

unread,
Apr 27, 2015, 10:16:42 AM4/27/15
to uliweb
以前我实现的主要就是预先在settings.ini中定义好,然后直接使用,通过Model.use(engine_name)或在结果集上 Result.use(engine_name)来动态切换数据库连接。那么对于数据库连接本身是动态的的确没有试过,不过可以考虑和engine_name一样的方式,你可以试试:

1. 先向engine_manager中添加新的数据库连接,如:

        x = {'connection_string':d.get('CONNECTION', ''),
            'debug_log':d.get('DEBUG_LOG', None),
            'connection_args':d.get('CONNECTION_ARGS', {}),
            'strategy':d.get('STRATEGY', 'threadlocal'),
            'connection_type':d.get('CONNECTION_TYPE', 'long'),
            'duplication':d.get('DUPLICATION', False),
        }
        orm.engine_manager.add(name, x)

这个是uliweb.contrib.orm/__init__.py中的代码上面主要是connection_string有用,其它的可以根据情况选择。

有了engine对象了,就可以通过use来切换了。name自已起。

可以试一下。

On Sun, Apr 26, 2015 at 11:10 PM, Chunlin Zhang <zhangc...@gmail.com> wrote:
其实我是想知道能不能做到这样:比如说我的应用里有很多项目(这些项目可能会动态创建)那么能不能做到每个项目对应一个数据库(比如说每个项目自己一个sqlite文件)

看多数据库的说明应该目前是不好做到,但有可能做到这样么?

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+un...@googlegroups.com.
To post to this group, send email to uli...@googlegroups.com.
Visit this group at http://groups.google.com/group/uliweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/uliweb/CAG2rzAmFzNOghGrk8Q6wRj-seEHFeYuUhqy%2B%2BR4o8d3_NKEaxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

Chunlin Zhang

unread,
Apr 27, 2015, 8:02:06 PM4/27/15
to uli...@googlegroups.com
看起来是有可行性的哈,有空试试看
谢谢!
Reply all
Reply to author
Forward
0 new messages