I am developing a Flex based dashboard and I am considering SQLAlchemy or Django to interact with the database.
I am new to both these framew开发者_如何学编程orks. I know that Django can create the database with syncdb command and SQLAlchemy can do the same with *metadata.create_all(engine)* statement. How do I use these frameworks with an existing database?
For Django take a look at Integrating Django with a legacy database. I have no experience with SQLAlchemy, though I would assume they have something similar.
define a class for mapper function e.g
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()
精彩评论