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 3393374
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:02:35+00:00 2026-05-18T04:02:35+00:00

I am implementing a class that resembles a typical database table: has named columns

  • 0

I am implementing a class that resembles a typical database table:

  • has named columns and unnamed rows
  • has a primary key by which I can refer to the rows
  • supports retrieval and assignment by primary key and column title
  • can be asked to add unique or non-unique index for any of the columns, allowing fast retrieval of a row (or set of rows) which have a given value in that column
  • removal of a row is fast and is implemented as “soft-delete”: the row is kept physically, but is marked for deletion and won’t show up in any subsequent retrieval operations
  • addition of a column is fast
  • rows are rarely added
  • columns are rarely deleted

I decided to implement the class directly rather than use a wrapper around sqlite.

What would be a good data structure to use?


Just as an example, one approach I was thinking about is a dictionary. Its keys are the values in the primary key column of the table; its values are the rows implemented in one of these ways:

  1. As lists. Column numbers are mapped into column titles (using a list for one direction and a map for the other). Here, a retrieval operation would first convert column title into column number, and then find the corresponding element in the list.

  2. As dictionaries. Column titles are the keys of this dictionary.

Not sure about the pros/cons of the two.


The reasons I want to write my own code are:

  • I need to track row deletions. That is, at any time I want to be able to report which rows where deleted and for what “reason” (the “reason” is passed to my delete method).
  • I need some reporting during indexing (e.g., while an non-unique index is being built, I want to check certain conditions and report if they are violated)
  • 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-18T04:02:36+00:00Added an answer on May 18, 2026 at 4:02 am

    You might want to consider creating a class which uses an in-memory sqlite table under the hood:

    import sqlite3
    
    class MyTable(object):
        def __init__(self):
            self.conn=sqlite3.connect(':memory:')
            self.cursor=self.conn.cursor()
            sql='''\
                CREATE TABLE foo ...
            '''
            self.execute(sql)
        def execute(self,sql,args):
            self.cursor.execute(sql,args)
        def delete(self,id,reason):
            sql='UPDATE table SET softdelete = 1, reason = %s where tableid = %s'
            self.cursor.execute(sql,(reason,id,))
        def verify(self):
            # Check that certain conditions are true
            # Report (or raise exception?) if violated
        def build_index(self):
            self.verify()
            ... 
    

    Soft-delete can be implemented by having a softdelete column (of bool type).
    Similarly, you can have a column to store reason for deletion.
    Undeleting would simply involve updating the row and changing the softdelete value.
    Selecting rows that have not been deleted could be achieved with the SQL condition WHERE softdelete != 1.

    You could write a verify method to verify conditions on your data are satisfied. And you could call that method from within your build_index method.

    Another alternative is to use a numpy structured masked array.

    It’s hard to say what would be fastest. Perhaps the only sure way to tell would be to write code for each and benchmark on real-world data with timeit.

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

Sidebar

Related Questions

No related questions found

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.