简体   繁体   English

创建字典的有序字典(或列表)以用作SQL表

[英]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. 我需要在不实际使用sqlite的情况下创建类似于python中的表的结构。 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 现在使用2种功能,一种用于用户输入,另一种用于设置词典列表

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... 我需要执行各种DBMS操作,例如添加新记录,更新记录...

Thanks to post for the initial help: Python: Data Structure for Maintaing Tabular Data in Memory? 感谢您提供的初步帮助: Python:用于在内存中维护表格数据的数据结构吗?

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. 与“ dbm”数据库的区别在于,架子中的值(不是键!)本质上可以是任意的Python对象-pickle模块可以处理的任何对象。 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 但是您要做的任何事情在我们将其命名为MONGODB之前都已完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM