2011-05-03 56 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