python简单模拟数据库程序
2013-08-28 11:31
477 查看
做个小程序,简单的模拟数据库,代码比较长,觉得可以学习的地方很多,记下来
#database.py import sys,shelve def store_person(db): """ Query user for data and store it in the shelf object """ pid = raw_input("Enter unique ID number:") person = {} person['name'] = raw_input("Enter name: ") person['age'] = raw_input("Enter age: ") person['phone']= raw_input("Enter phone") db[pid] = person def lookup_person(db): """ Query user for ID """ pid = raw_input("Enter ID number:") field = raw_input("what would you like to know ? name,age,phont") field = field.strip().lower() print field.capitalize() + ':',db[pid][field] def print_help(): print "the available commands are:" print "store : store informantion about a person" print "lookup: look up a person fron ID number" print "quit : Save changes and exit" print "? : Prints this message" def enter_command(): cmd = raw_input("Enter command (? for help):") cmd = cmd.strip().lower() return cmd def main(): database = shelve.open('/home/weichong/database.txt') try: while True: cmd = enter_command() if cmd == 'store': store_person(database) elif cmd == 'lookup': lookup_person(database) elif cmd == '?': print_help() elif cmd == 'quit': return finally: database.close() if __name__ == '__main__':main()
相关文章推荐
- Python利用shelve模块设计简单数据库程序
- 用python写的模拟系统登录三次验证简单程序
- python——模拟一个简单的登录接口程序
- Python操作CouchDB数据库简单示例
- python-简单的makefile生成程序-没有注释
- 一个简单的Python日志程序代码,支持按天滚动,限制备份保留个数
- Python源码分析2 - 一个简单的Python程序的执行
- python采用requests库模拟登录和抓取数据的简单示例
- 开始学习Python,设置环境和编译一个简单的程序
- python中最简单的多进程程序
- Moblin的简单Python程序开发
- Python端口扫描简单程序
- Visual C# 2008+SQL Server 2005 数据库与网络开发--13.1.3 简单记事本程序菜单设计
- 编写Java程序,模拟简单的计算器。 定义名为Number的类,其中有两个整型数据成员n1和n2,应声明为私有。 编写构造方法,用于初始化n1和n2,再为该类定义 加(addition)、 减(su
- python最简单的小程序-计算行车时间-学习笔记6
- Python源码分析2 - 一个简单的Python程序的执行
- 最简单的一个连接数据库的小程序
- Python网络编程 3.1 由简单的TCP服务器、客户端程序分析相关方法
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- 树莓派用Python写几个简单程序6_yeelink平台