Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:53:31+00:00 2026-05-13T10:53:31+00:00

I would like to store Python objects into a SQLite database. Is that possible?

  • 0

I would like to store Python objects into a SQLite database. Is that possible?

If so what would be some links / examples for it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:53:32+00:00Added an answer on May 13, 2026 at 10:53 am

    You can’t store the object itself in the DB. What you do is to store the data from the object and reconstruct it later.

    A good way is to use the excellent SQLAlchemy library. It lets you map your defined class to a table in the database. Every mapped attribute will be stored, and can be used to reconstruct the object. Querying the database returns instances of your class.

    With it you can use not only sqlite, but most databases – It currently also supports Postgres, MySQL, Oracle, MS-SQL, Firebird, MaxDB, MS Access, Sybase, Informix and IBM DB2. And you can have your user choose which one she wants to use, because you can basically switch between those databases without changing the code at all.

    There are also a lot of cool features – like automatic JOINs, polymorphing…

    A quick, simple example you can run:

    from sqlalchemy import Column, Integer, Unicode, UnicodeText, String
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    
    from random import choice
    from string import letters
    
    engine = create_engine('sqlite:////tmp/teste.db', echo=True)
    Base = declarative_base(bind=engine)
    
    class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, primary_key=True)
        name = Column(Unicode(40))
        address = Column(UnicodeText, nullable=True)
        password = Column(String(20))
    
        def __init__(self, name, address=None, password=None):
            self.name = name
            self.address = address
            if password is None:
                password = ''.join(choice(letters) for n in xrange(10))
            self.password = password
    
    Base.metadata.create_all()
    
    Session = sessionmaker(bind=engine)
    s = Session()
    

    Then I can use it like this:

    # create instances of my user object
    u = User('nosklo')
    u.address = '66 Some Street #500'
    
    u2 = User('lakshmipathi')
    u2.password = 'ihtapimhskal'
    
    # testing
    s.add_all([u, u2])
    s.commit()
    

    That would run INSERT statements against the database.

    # When you query the data back it returns instances of your class:
    
    for user in s.query(User):
        print type(user), user.name, user.password
    

    That query would run SELECT users.id AS users_id, users.name AS users_name, users.address AS users_address, users.password AS users_password.

    The printed result would be:

    <class '__main__.User'> nosklo aBPDXlTPJs
    <class '__main__.User'> lakshmipathi ihtapimhskal
    

    So you’re effectively storing your object into the database, the best way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to store an object FOO in a database. Lets say FOO
I have a twenty byte hex hash that I would like to store in
In my database, I would like to store a decimal score. A score can
I have a single user java program that I would like to have store
I use a byte to store some flag like 10101010 , and I would
I have some random HTML layouts that contain important text I would like to
If I would like to store emails, but don't have a database (e.g. MySQL),
I am using Google App Engine Python. I would like to store a simple
I would like to store log4net config data in my application.config file. Based on
I would like to sign a device, and I have 64 bits to store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.