Wu Yuxiong --python study notes: student management interface for text sqlite3

import sqlite3

conn = sqlite3.connect('E:\\student.db')
print("Opened database successfully")
c = conn.cursor()
c.execute('''CREATE TABLE if not exists STUDENT
      (ID INT PRIMARY KEY     NOT NULL,
      STU_NAME           CHAR(20),
      AGE            INT     NOT NULL,
      ADDRESS        CHAR(50));''')

print ("Table created successfully")
conn.commit()
Opened database successfully
Table created successfully
print("Opened database successfully")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071115,'dch',22,'1-223')")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071112,'fwx',22,'1-222') ")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071111,'cg',22,'1-223') ")
c.execute("INSERT INTO STUDENT(ID,STU_NAME,AGE,ADDRESS) VALUES(20154071110,'wyf',22,'1-222') ")
conn.commit()
print("Records created successfully")
Opened database successfully
Records created successfully
cursor = c.execute("SELECT id,STU_NAME,address FROM student")
for row in cursor:
    print("ID = ",row[0])
    print("NAME = ",row[1])
    print("ADDRESS = ",row[2])
print("Operation done successfully")
#conn.close()
def display_menu():
    print("学生表操作界面")
    Print ( " --------------------- " )
     Print ( " 1. add student information " )
     Print ( " 2. Query student information " )
     Print ( " 3. modify the student information " )
     Print ( " 4. delete student information " )
     Print ( " 5. inquiry now student information " )
     Print ( " 0. exit " )
     Print ( " --------- ------------ " )
ID =  20154071115
NAME =  dch
ADDRESS =  1-223
ID =  20154071112
NAME =  fwx
ADDRESS =  1-222
ID =  20154071111
NAME =  cg
ADDRESS =  1-223
ID =  20154071110
NAME =  wyf
ADDRESS =  1-222
Operation done successfully
DEF append_data (): 
    the above mentioned id = int (the INPUT ( " Please enter a new student's Student ID: " )) 
    name = str (the INPUT ( " Please enter a new student's name " )) 
    Age = int (the INPUT ( " Please enter a new student Age " )) 
    address = str (the iNPUT ( " Please enter a new address students " )) 
    sqlstr = " the SELECT * student from the WHERE the above mentioned id = {}; " .format (the above mentioned id) 
    the Cursor = conn.execute (sqlstr)
     IF len (cursor.fetchall ())> 0:
         Print ( "List have the student " )
     the else : 
        sqlstr = " INSERT INTO Student (ID, STU_NAME, of AGE, ADDRESS) the VALUES ({}, '{}', {}, '{}') " .format (ID, name, Age, address) 
        conn.execute (sqlstr) 
        conn.commit ()
DEF UPDATE_DATE (): 
    ID = int (INPUT ( " Please enter your student number to be modified: " )) 
    sqlstr = " SELECT * WHERE from Student ID = {}; " .format (ID) 
    Cursor = conn.execute (sqlstr ) 
    rows = cursor.fetchall ()
     IF len (rows)> 0:
         Print ( " the student's name is " , rows [0] [1 ]) 
        name = the iNPUT ( " Please enter the new name of the student " ) 
        Age = int (the iNPUT ( " Please enter a new age of students " ))
        address = INPUT ( " Please enter a new address student " ) 
        sqlstr = " Update Student SET STU_NAME = '{}', Age = '{}', address = '{}' {} WHERE ID = " .format (name, Age, address, the above mentioned id) 
        conn.execute (sqlstr) 
        conn.commit () 
        Print ( " modified successfully " )
     the else :
         Print ( " the absence of the student " )
DEF delete_data (): 
    id = int (the INPUT ( " Enter student id to be deleted: " )) 
    sqlstr = " the SELECT * Student from the WHERE id = {}; " .format (id) 
    the Cursor = conn.execute (sqlstr) 
    rows = cursor.fetchall ()
     IF len (rows)> 0:
         Print ( " the student's name is " , rows [0] [1 ]) 
        S = int (iNPUT ( " Please confirm deletion (remove if enter '1 'not deleted enter' 0 '): " ))
         IF S ==. 1 :
            sqlStr = "delete from student where id = {}".format(id)
            conn.execute(sqlStr)
            print("删除成功")
        else:
            return display_menu()
DEF select_data (): 
    ID = int (INPUT ( " Please enter an update Student ID: " )) 
    sqlstr = " SELECT * WHERE from Student ID = {}; " .format (ID) 
    Cursor = conn.execute (sqlstr) 
    rows = cursor.fetchall ()
     IF len (rows)> 0:
         Print ( " the student information is as follows: " )
         Print (rows)
     the else :
         Print ( " the student does not exist " )
def display_data():
    cursor = conn.execute('SELECT * FROM student;')
    for row in cursor:
        print(row)
while True:
    display_menu()
    choice = int(input("请输入你的选择"))
    if choice == 0:
        conn.close()
        break
    elif choice == 2:
        select_data()
    elif choice == 3:
        update_date()
    elif choice == 4:
        delete_data()
    elif choice == 5:
        display_data()
    elif choice == 1:
        append_data()
    else:break

 

Guess you like

Origin www.cnblogs.com/tszr/p/11932957.html