1

I'd like to create a function makeTable to create a sqlite table from a python class. I have this class:

class Foo(object):
    def __init__(self):
        self.a
        self.b
        self.c

I tried defining the function in this way:

def makeTable(obj):
    conn = sqlite3.connect(sqlfilename)
    cursor = conn.cursor()
    cursor.execute(sqlString)

But I have to create the sqlString such as:

"""CREATE TABLE foo (a text, b text)"""

How can I create this sqlString automatically from a given class?

4
  • 4
    Use an ORM, like sqlalchemy? Commented Sep 4, 2013 at 12:31
  • 2
    @marcos-da-silva-sampaio, alecxe does not ask if you use one, but asks if you shouldnt use one as sqlalechmy does this job Commented Sep 4, 2013 at 12:37
  • Thanks. I didn't knew the OMR. I will try it to solve the question. Commented Sep 4, 2013 at 12:39
  • its ORM for Object Relational Mapping a tool that does what you expect ;) Commented Sep 4, 2013 at 12:44

1 Answer 1

1

Alecxe was not really asking a question, but proposes you to use an ORM like SqlAlchemy. Your approach is quite too simple and limited. In real live you have to figure out, which data type a column should have, which attributes/properties should go to the database and many other details. An ORM will do that for you and SqlAlchemy is the best one in my opinion.

I case you think that you're happy with a simpler solution, you have to describe that simple solution to us. Or even better: Show some code you wrote and which did not work.

Sign up to request clarification or add additional context in comments.

1 Comment

do you mean In a real life?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.