【发布时间】:2016-01-31 21:27:53
【问题描述】:
我刚开始使用 Python,我必须创建一个小型数据库。我使用的代码对我来说太复杂了,而且我遇到了一些问题。 数据库结构如下:
def load_data():
global base
global magazine_base
magazine_base = shelve.open('magazine_base')
if magazine_base.has_key('base'):
base = magazine_base['base']
if not base:
base = []
else:
base = []
magazine_base['base'] = base
#####参赛作品由:
base += [{
'name': name,
'quantity': int(quantity),
'price': float(price)
}]
magazine_base['base'] = base
magazine_base.close()
##########And the problem is that the function to clear it all is not working properly, sometimes it doesn't remove, sometimes error, and sometimes data is cleared after restart.
def removeall():
global magazine_base
global base
magazine_base.clear()
magazine_base.items()
######对于单个条目它工作完美,也许以某种方式修改它以清除所有条目?
定义删除():
全球基地
全球杂志库
加载数据()
name=raw_input('产品名称:')
我们的产品 = 假
对于基础元素:
如果 elem['name'] == 名称:
del base[base.index(elem)]
print '从列表中删除的产品'
我们的产品 = 真
如果不是我们的产品:
打印“错误!杂志上没有这样的产品!”
杂志基地['基地'] =基地
magazine_base.close()
【问题讨论】:
-
请清楚详细地解释您所面临的问题。您的描述没有说明问题。
-
对不起,我在代码之间放置了 cmets,函数 removeall 根本无法正常工作,它应该立即清除基础,而不是有时不这样做,有时在重新启动后。我不知道为什么
标签: python list dictionary