概述
这个例子创建了一个简单的表, 表的内容为姓名和年龄, 实现了增、删、改、查的基本功能。
import sqlite3
class 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 namen")
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 namen")
name = input("please input user's namen")
age = input("please input user's agen")
self.c.execute('''INSERT INTO '''+table_name+''' (NAME, AGE) VALUES ("'''+ name + '''", ''' + str(age) + ''');''');
def delete(self):
table_name = input("please input table's namen")
name = input("please input the name you want to deleten")
self.c.execute("delete from " + table_name + ''' where name="''' + name+'''"''')
def change(self):
table_name = input("please input table's namen")
name = input("please input your namen")
new_age = input("please enter the new agen")
self.c.execute("update " + table_name + " set AGE="+str(new_age) +''' where name="''' + name+'''"''')
def check(self):
table_name = input("please input table's namen")
name = input("please input your namen")
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 tablenenter 2 to add informationn"
+"enter 3 to delete informationnenter 4 to update informationn"
+"enter 5 to check your agen----------------------------------n")
self.choice.get(c)()
self.conn.commit()
test = database()
test.loop()
最后
以上就是开心战斗机为你收集整理的python使用sqlite3的例子的全部内容,希望文章能够帮你解决python使用sqlite3的例子所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复