Chetandalal
Chetandalal

Reputation: 684

Create ordered dictionary(or list) of dictionary to work as SQL table

I need to create structure similar to a table in python without actually using sqlite. So, to do such a thing i came across possibility of using dictionary of dictionary.

This gives an structure like:

{'7865':{'Name':'john','age':'24','Index':'7865'},'Index':{'Name':[],'age':[],'Index'='ID'}}

This values is never in order and the column names will always change based on user input.( cannot hard-code the key values pairs to match)

Using 2 functions now, one for user input and other for setting the list of dictionary

set user input:

def CreateSchemaOnly():
    my_dict1=defaultdict(list)
    name=input("enter the DB name")
    count=int(input("enter the column count"))
    columnName=list()
    for x in range(count):
        a=input("enter column name")
        columnName.append(a)
        my_dict1[columnName[x]]=[]
    index=input("Enter the Index")
    my_dict1['Index']=index
    Schema.Schemas().addRow(my_dict1,name)

Setup the schema

class Schemas(object):
    '''
    classdocs
    '''
    def __init__(self):
        '''
        Constructor
        '''
        self.rows =  []# list of row objects, we assume if order of id
        self.nameMap = {}# for faster direct lookup for row by name


    def addRow(self, row,dbname):
        self.rows.append(row)
        self.nameMap['Index'] = row
        output=open(dbname+'.pkl','wb')# to store schema in file and load when reqiured
        pickle.dump(self.nameMap,output)
        output.close()
        print("Database Schema Created Successfully")

I need to perform all kinds of DBMS operations such as Add new record,Update record...

Thanks to post for the initial help:Python: Data Structure for Maintaing Tabular Data in Memory?

Upvotes: 0

Views: 2064

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

I suggest use shelf for this aim.

A “shelf” is a persistent, dictionary-like object. The difference with “dbm” databases is that the values (not the keys!) in a shelf can be essentially arbitrary Python objects — anything that the pickle module can handle. This includes most class instances, recursive data types, and objects containing lots of shared sub-objects. The keys are ordinary strings.

but any thing that you want to do has been done before that we call it MONGODB

Upvotes: 2

Related Questions