Greetings everyone, I have a question about ways of passing data to DB, meaning that I would like to receive an explanation/advice about the approaches. So that I'll keep doing my things right way.
Though I'm a DB novice, so largely my questions raise because of the lack of experience in this field. In addition, this text may seem as too long... My apologies for that, I tried hard to make it concise without loosing a context. In order to explain myself I'll describe my situation and the goals I need to reach. My code snippets taken from the following locations: 1. http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#create-an-instance-of-the-mapped-class 2. http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#adding-new-objects 3. http://www.pythoncentral.io/introductory-tutorial-python-sqlalchemy/ 4. http://stackoverflow.com/questions/8187082/how-can-you-set-class-attributes-from-variable-arguments-kwargs-in-python So here is verbose part ^_^ goes: In my project (EE student) I need to read data from the excel files and then pass this data into DB. I've written the code that read some excel sheet and returns a dictionary. For instance: {'key_1': val_1, 'key_2': val_2, 'key_3': val_3} These key/val pairs can be assigned automatically, (one of the possible implementations): class Foo: def read_attrs(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) The problem is that I don't know if such approach is possible in sqlalchemy? All the examples I've seen till now, describe only "static" cases (like this one): ************************************************************************************************* class User(Base): __tablename__ = 'users' id = Column(Integer, Sequence('user_id_seq'), primary_key=True) name = Column(String(50)) fullname = Column(String(50)) password = Column(String(12)) def __repr__(self): return "<User(name= '%s', fullname= '%s', password= '%s')>" %(self.name, self.fullname, self.password) ************************************************************************************************* The data writing is described in two ways: Way 1 (with instance initialization): ************************************************************************************************* >>> ed_user = User(name='ed', fullname='Ed Jones', password='edspassword') >>> session.add(ed_user) ************************************************************************************************* or Way 2(without instance intialization) : ************************************************************************************************* >>> session.add_all([ ... User(name='wendy', fullname='Wendy Williams', password='foobar'), ... User(name='mary', fullname='Mary Contrary', password='xxg527'), ... User(name='fred', fullname='Fred Flinstone', password='blah')]) ************************************************************************************************* Furthermore from link #3 I know that these "predefined classes" can be stored in a separate file (I suppose for convenience purpose only), and then,when someone want to use these classes, he can just import this module into main routine. The main question is: if I can implement this dynamic approach with the classes that will interact with DB? Ideally it should look like that (pseudo code): ************************************************************************************************* my_dict = {'key_1': val_1, 'key_2': val_2, 'key_3': val_3} class Foo: def create_attrs(self, my_dict.keys()): just create self.attributes using keys from my_dict def read_attrs(self, my_dict): create self.attributes and assign value to them ************************************************************************************************* By this, it would possible "multi-use" my class. The case when I read my data from excel file and then initialize it to DB: ************************************************************************************************* >>> Foo.read_attrs(my_dict) >>> session.add(Foo) ************************************************************************************************* Or the other option(for future management of the values in existing DB): ************************************************************************************************* >>>Foo.create_attr(my_dict.keys()) >>> updating the Attributes here.... >>>session.add(Foo) ************************************************************************************************* One last question: In case I want to store a whole "dictionary/list/tuple" as a value of a single attribute, how can I make this? How it will be stored in DB? What will be returned after query for that value, a dictionary or a string that looks like a dictionary? This question arose, because all the examples I've seen, require a specific types for each attribute (i.e. "String(length)", "Integer" or etc) Hope you haven't got annoyed too much ^_^ Any help/advices/code snippets will be highly appreciated. Sincerely, Ivan. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
