【Python】数据库使用案例
本文最后更新于 1531 天前,其中的信息可能已经有所发展或是发生改变。

[successbox title=”通讯录”]
设计一个学生通讯录,可以添加、修改、删除、查询里面的相关信息的学习案例。
[/successbox]

[sourcecode language=”python” title=”demo14.py”]
import sqlite3
#打开数据库
def opendb():
conn = sqlite3.connect("F:\demo\demo.db")
#三个双引号是为了定义多行字符串
cur = conn.execute("""create table if not exists tongxinlu
(usernum integer primary key,username varchar(128), passworld varchar(128),
address varchar(125), telnum varchar(128))""")
return cur, conn
#查询全部信息
def showalldb():
print("———-处理后的数据———-")
hel = opendb()
cur = hel[1].cursor()
cur.execute("select * from tongxinlu")
res = cur.fetchall()
for line in res:
for h in line:
print(h)
print
cur.close()
#输入信息
def into():
usernum = input("请输入学号:")
username = input("请输入姓名:")
passworld = input("请输入密码:")
address = input("请输入地址:")
telnum = input("请输入联系电话:")
return usernum,username,passworld,address,telnum
#往数据库中添加内容
def adddb():
print("———-欢迎使用添加数据功能———-")
person = into()
hel = opendb()
hel[1].execute("insert into tongxinlu(usernum, username, passworld, address, telnum) values (?,?,?,?,?)",(person[0], person[1], person[4], person[3], person[4]))
hel[1].commit()
print("———-恭喜你,数据添加成功———")
showalldb()
hel[1].close()
#删除数据库中的内容
def deldb():
print("———-欢迎使用删除数据库功能———-")
delchoice = input("请输入想要删除的学号:")
hel = opendb()
hel[1].execute("delete from tongxinlu where usernum = "+ delchoice)
hel[1].commit()
print("———-恭喜你,删除数据成功———-")
showalldb()
hel[1].close()
#修改数据库的内容
def alter():
print("———-欢迎使用修改数据库功能———-")
changechoice = input("请输入想要修改的学生的学号:")
hel = opendb()
person = into()
hel[1].execute("update tongxinlu set usernum = ?, username = ?, passworld = ?, address = ?, telnum = ? where usernum = "+ changechoice,(person[0],person[1],person[2],person[3],person[4]))
hel[1].commit()
showalldb()
hel[1].close()
#查询数据
def searchdb():
print("———-欢迎使用查询数据库功能———-")
choice = input("请输入要查询的学生的学号:")
hel = opendb()
cur = hel[1].cursor()
cur.execute("select * from tongxinlu where usernum = " + choice)
hel[1].commit()
print("———-恭喜你,你要查找的数据如下———-")
for row in cur:
print(row[0],row[1],row[2],row[3],row[4])
cur.close()
hel[1].close()
#是否继续
def conti(a):
choice = input("是否继续?(y or n):")
if choice == ‘y’:
a = 1
else:
a = 0
return a
if __name__ == "__main__":
flag = 1
while flag:
print("———-欢迎使用数据库通讯录———-")
choiceshow = """
请选择您的进一步选择:
(添加)往数据库添加数据
(删除)删除数据库数据
(修改)修改书库的内容
(查询)查询数据的内容
"""
choice = input(choiceshow)
if choice == "添加":
adddb()
flag = conti(flag)
elif choice == "删除":
deldb()
flag = conti(flag)
elif choice == "修改":
alter()
flag = conti(flag)
elif choice == "查询":
searchdb()
flag = conti(flag)
else:
print("您输入错误,请重新输入")
[/sourcecode]

[dangerbox title=”注意”]
1.修改conn = sqlite3.connect(“F:\demo\demo.db”)这里的存储数据文件的地址。
2.if __name__ == “__main__”:这条语句的下划线是双下划线不是单下划线。
[/dangerbox]

效果图:

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇