2011-05-03 37 views
0

我正在开发一个基于Flex的仪表板,我正在考虑SQLAlchemy或Django与数据库进行交互。Python ORM SQLAlchemy和Django的用法

我对这两个框架都很陌生。我知道Django可以使用syncdb命令创建数据库,并且SQLAlchemy可以使用* metadata.create_all(engine)*语句执行相同的操作。我如何在现有数据库中使用这些框架?

回答

0

对于Django,请查看Integrating Django with a legacy database。我没有使用SQLAlchemy的经验,但我会认为他们有类似的东西。

+0

谢谢,我会尝试一下 – arjunurs 2011-05-03 20:59:09

+0

提示清理模型可以在http://www.djangobook.com/en/2.0/找到第18/ – solartic 2011-05-03 22:16:13

+0

谢谢solartic – arjunurs 2011-05-04 00:59:10

0

定义映射函数的类 如

engine = create_engine("mysql+mysqldb://root:@localhost/testdb",echo = True) 
#testbd is ur database#use password->> "password"@localhost 

metadata = MetaData(engine) 
session = create_session() 

person_table = Table('person', metadata, 
        Column('id', Integer, primary_key = True), 
        Column('name', String(40)), 
        Column('age', Integer), 
        Column('about', String(100)) 
        ) 

metadata.create_all(engine) 

class Person(object): 
    def __init__(self,name,age,about): 
     self.name = name 
     self.age = age 
     self.about = about 
    def __repr__(self): 
     return self.name, self.age, self.about 

mapper(Person, person_table) 

#for insert do the below 
a_person = Person('Name','Age','About') 
session.add(a_person) 
session.flush() 
+0

如需更多信息,请通知。我会尽力的 – Sabbir 2011-05-29 11:02:53