# coding: utf-8
import sqlite3
# 導入數據庫
def connect_sql():
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
cursor.execute("""
CREATE TABLE studentsab51
(
id INTEGER PRIMARY KEY,
name TEXT,
sex TEXT,
age INTEGER,
phone TEXT
);
""")
connect.commit()
cursor.close()
connect.close()
# 展示學生列表
def show_students():
print('ID','\t\t', '姓名''\t\t', '性別''\t\t', '年齡''\t\t', '電話')
print('---'*20)
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
cursor.execute("""
SELECT * FROM students;
""")
students_list = cursor.fetchall()
for index, student in enumerate(students_list):
print(f'{index+1}\t\t\t{student[1]}\t\t\t{student[2]}\t\t\t{student[3]}\t\t\t{student[4]}')
connect.commit()
cursor.close()
connect.close()
# 添加學生
def add_student():
name = input('新學生姓名:')
sex = input('新學生性別:')
age = input('新學生年齡:')
phone = input('新學生電話')
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
# sql=("""
# INSERT INTO students(name, sex, age, phone) VALUES ("%s", "%s",%s, "%s")
# """%(name, sex, age , phone))
sql = f"""
INSERT INTO students (name, sex, age, phone) VALUES ("{name}","{sex}",{age},"{phone}")
"""
print(sql)
cursor.execute(sql)
connect.commit()
connect.close()
# 修改
def update_student():
show_students()
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
new_id = input("請輸入你想要修改的編號:")
new_name=input('請輸入修改后的姓名:')
new_sex=input('請輸入修改后的性別:')
new_age = input('請輸入新的年齡:')
new_phone = input('請輸入新電話')
# 先查詢輸入的學生id是否存在,存在的話更新,不存在的給出用戶提示
sql = f"""
SELECT 1 FROM students WHERE id="{new_id}";
"""
cursor.execute(sql)
student = cursor.fetchall()
if student:
sql2 = f"""
UPDATE students SET name = "{new_name}",sex="{new_sex}",age="{new_age}",phone="{new_phone}" WHERE id="{new_id}";
"""
cursor.execute(sql2)
connect.commit()
else:
print('學生不存在,請重新操作。')
connect.close()
print('學生修改成功')
# 下面的方法也行 看你用哪種
# cursor.execute("""UPDATE students SET name=?, sex= ?,age=?,phone=? WHERE id="""+new_id,(new_name, new_sex, new_age, new_phone))
# connect.commit()
# connect.close()
# 刪除
def delete_student():
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
print(""" 刪除> 請輸入子操作編號:
1)按學生編號刪除
2)刪除全部學生(clear)
""")
sub_select = int(input('請選擇子操作:'))
if sub_select == 1:
show_students()
stu_name = input('要刪除的學生姓名:')
delete_name = f""" delete from students where name='{stu_name}'"""
cursor.execute(delete_name)
connect.commit()
connect.close()
print('刪除成功')
elif sub_select == 2:
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
confirm = input('要刪除全部學生?( Y/y):')
if confirm == 'y':
clear_as = f"""DELETE FROM main.students;"""
cursor.execute(clear_as)
connect.commit()
connect.close()
print('刪除全部成功')
def main():
# 主函數,程序入口
while True:
print("""
歡迎使用學生管理系統
1-查看學員姓名
2-添加學員姓名
3-修改學員姓名
4-刪除學員姓名
0-退出程序
""")
num = int(input('請輸入操作編號:'))
if num == 1:
show_students()
elif num == 2:
add_student()
elif num == 3:
update_student()
elif num == 4:
delete_student()
elif num == 0:
break
if __name__ == '__main__':
main()
# 可能出現的錯誤:
# 插入功能
# sql = """
# INSERT INTO students (name, sex, age, phone) VALUES (%s, %s, %d, %s);
# """ % (name, sex, int(age), phone)
# print(sql)
# 報錯 sqlite3.OperationalError: no such column: aaa
# 原因 sql INSERT INTO students (name, sex, age, phone) VALUES (aaa, nan, 13, 13000); 值並不是sql解釋器理解的字符串。
# 解決 %s兩側加引號。 INSERT INTO students (name, sex, age, phone) VALUES ("%s", %s, %d, %s) % (name, sex, int(age), phone);
# sql補充
# SELECT 1 FROM students WHERE name="{stu_name}"; -- 只關心一行數據是否存在。效率比select * 高。
# coding: utf-8
import sqlite3
# 導入數據庫
def connect_sql():
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
cursor.execute("""
CREATE TABLE studentsab51
(
id INTEGER PRIMARY KEY,
name TEXT,
sex TEXT,
age INTEGER,
phone TEXT
);
""")
connect.commit()
cursor.close()
connect.close()
# 展示學生列表
def show_students():
print('ID','\t\t', '姓名''\t\t', '性別''\t\t', '年齡''\t\t', '電話')
print('---'*20)
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
cursor.execute("""
SELECT * FROM students;
""")
students_list = cursor.fetchall()
for index, student in enumerate(students_list):
print(f'{index+1}\t\t\t{student[1]}\t\t\t{student[2]}\t\t\t{student[3]}\t\t\t{student[4]}')
connect.commit()
cursor.close()
connect.close()
# 添加學生
def add_student():
name = input('新學生姓名:')
sex = input('新學生性別:')
age = input('新學生年齡:')
phone = input('新學生電話')
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
# sql=("""
# INSERT INTO students(name, sex, age, phone) VALUES ("%s", "%s",%s, "%s")
# """%(name, sex, age , phone))
sql = f"""
INSERT INTO students (name, sex, age, phone) VALUES ("{name}","{sex}",{age},"{phone}")
"""
print(sql)
cursor.execute(sql)
connect.commit()
connect.close()
# 修改
def update_student():
show_students()
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
new_id = input("請輸入你想要修改的編號:")
new_name=input('請輸入修改后的姓名:')
new_sex=input('請輸入修改后的性別:')
new_age = input('請輸入新的年齡:')
new_phone = input('請輸入新電話')
# 先查詢輸入的學生id是否存在,存在的話更新,不存在的給出用戶提示
sql = f"""
SELECT 1 FROM students WHERE id="{new_id}";
"""
cursor.execute(sql)
student = cursor.fetchall()
if student:
sql2 = f"""
UPDATE students SET name = "{new_name}",sex="{new_sex}",age="{new_age}",phone="{new_phone}" WHERE id="{new_id}";
"""
cursor.execute(sql2)
connect.commit()
else:
print('學生不存在,請重新操作。')
connect.close()
print('學生修改成功')
# 下面的方法也行 看你用哪種
# cursor.execute("""UPDATE students SET name=?, sex= ?,age=?,phone=? WHERE id="""+new_id,(new_name, new_sex, new_age, new_phone))
# connect.commit()
# connect.close()
# 刪除
def delete_student():
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
print(""" 刪除> 請輸入子操作編號:
1)按學生編號刪除
2)刪除全部學生(clear)
""")
sub_select = int(input('請選擇子操作:'))
if sub_select == 1:
show_students()
stu_name = input('要刪除的學生姓名:')
delete_name = f""" delete from students where name='{stu_name}'"""
cursor.execute(delete_name)
connect.commit()
connect.close()
print('刪除成功')
elif sub_select == 2:
connect = sqlite3.connect("test_sqlite.db")
cursor = connect.cursor()
confirm = input('要刪除全部學生?( Y/y):')
if confirm == 'y':
clear_as = f"""DELETE FROM main.students;"""
cursor.execute(clear_as)
connect.commit()
connect.close()
print('刪除全部成功')
def main():
# 主函數,程序入口
while True:
print("""
歡迎使用學生管理系統
1-查看學員姓名
2-添加學員姓名
3-修改學員姓名
4-刪除學員姓名
0-退出程序
""")
num = int(input('請輸入操作編號:'))
if num == 1:
show_students()
elif num == 2:
add_student()
elif num == 3:
update_student()
elif num == 4:
delete_student()
elif num == 0:
break
if __name__ == '__main__':
main()
# 可能出現的錯誤:
# 插入功能
# sql = """
# INSERT INTO students (name, sex, age, phone) VALUES (%s, %s, %d, %s);
# """ % (name, sex, int(age), phone)
# print(sql)
# 報錯 sqlite3.OperationalError: no such column: aaa
# 原因 sql INSERT INTO students (name, sex, age, phone) VALUES (aaa, nan, 13, 13000); 值並不是sql解釋器理解的字符串。
# 解決 %s兩側加引號。 INSERT INTO students (name, sex, age, phone) VALUES ("%s", %s, %d, %s) % (name, sex, int(age), phone);
# sql補充
# SELECT 1 FROM students WHERE name="{stu_name}"; -- 只關心一行數據是否存在。效率比select * 高。