python使用sqlite3的例子
来源:互联网 发布:女童白色运动鞋淘宝 编辑:程序博客网 时间:2024/05/29 18:46
这个例子创建了一个简单的表, 表的内容为姓名和年龄, 实现了增、删、改、查的基本功能。
import sqlite3class database: def __init__(self): self.conn = sqlite3.connect('test.db') print ("Opened database successfully") self.c = self.conn.cursor() self.choice = {'1': self.new_table, '2' : \ self.add, '3': self.delete, \ '4': self.change, '5': self.check} def __del__(self): self.conn.close() def new_table(self): name = input("please enter table's name\n") self.c.execute('''CREATE TABLE ''' +name+ '''(NAME CHAR(20) NOT NULL, AGE INT NOT NULL);''') #名字不能有数字 print("Table created successfully") def add(self): table_name = input("please input table's name\n") name = input("please input user's name\n") age = input("please input user's age\n") self.c.execute('''INSERT INTO '''+table_name+''' (NAME, AGE) VALUES ("'''+ name + '''", ''' + str(age) + ''');'''); def delete(self): table_name = input("please input table's name\n") name = input("please input the name you want to delete\n") self.c.execute("delete from " + table_name + ''' where name="''' + name+'''"''') def change(self): table_name = input("please input table's name\n") name = input("please input your name\n") new_age = input("please enter the new age\n") self.c.execute("update " + table_name + " set AGE="+str(new_age) +''' where name="''' + name+'''"''') def check(self): table_name = input("please input table's name\n") name = input("please input your name\n") result = self.c.execute("select age from " + table_name + ''' where name="''' + name+'''"''') for i in result: print("\nyour age is " + str(i[0]) + "\n") def loop(self): while(1): c = input("enter 1 to create a new table\nenter 2 to add information\n"\ +"enter 3 to delete information\nenter 4 to update information\n"\ +"enter 5 to check your age\n----------------------------------\n") self.choice.get(c)() self.conn.commit()test = database()test.loop()
阅读全文
0 0
- python使用sqlite3的例子
- c++使用sqlite3的例子
- python 的sqlite3使用详解
- python sqlite3的使用及sql语句
- python使用sqlite3时游标的使用方法
- 让Python更加充分的使用Sqlite3
- 让 Python 更加充分的使用 Sqlite3
- 让 Python 更加充分的使用 Sqlite3
- 分享一个别人写的sqlite3使用总结的例子
- python 使用sqlite3
- python 使用sqlite3
- python 使用sqlite3
- python 使用sqlite3
- python 使用sqlite3
- python 使用sqlite3
- python使用sqlite3
- Python sqlite3库使用
- python 使用SQLITE3
- Ubuntu 安装 Anaconda3 详细步骤
- 进程的创建-Process⼦类
- 快速排序的C语言实现
- 异类:不一样的成功启示录(一)
- JAVA 数据库基础及不同数据库连接代码工具
- python使用sqlite3的例子
- Stirling数概念及例题
- 相对路径与绝对路径
- Python基础总结(4)
- c++ 中 operator 使用小结
- Jfinal 第一天 idea中新建项目
- 常见锁原理和使用-旋转锁和SRWLock
- java使用jbarcode生成条形码2-2
- python中的进程池Pool