#!/usr/bin/python  
#coding:gbk  
import pymysql  
from builtins import int  
  
#½«MysqlHelperµÄ¼¸¸öº¯ÊýÐ´³öÀ´  
  
def  connDB():
#Á¬½ÓÊý¾Ý¿â
    conn=pymysql.connect(host="localhost",user="root",passwd="",db="student",charset='utf8mb4',);
    cur=conn.cursor();  
    return (conn,cur);
def exeUpdate(conn,cur,sql):
#¸üÐÂ»ò²åÈë²Ù×÷
    sta=cur.execute(sql);  
    conn.commit();  
    return (sta);  
  
def exeDelete(conn,cur,IDs):
#É¾³ý²Ù×÷
    sta=0;  
    for eachID in IDs.split(' '):  
        sta+=cur.execute("delete from students where Id=%d"%(int(eachID)));  
    conn.commit();  
    return (sta);  
          
def exeQuery(cur,sql):
#²éÕÒ²Ù×÷
    cur.execute(sql);  
    return (cur);  
      
def connClose(conn,cur):
#¹Ø±ÕÁ¬½Ó£¬ÊÍ·Å×ÊÔ´
    cur.close();  
    conn.close();  
  
result=True;  
print("ÇëÑ¡ÔñÒÔÉÏËÄ¸ö²Ù×÷£º1¡¢ÐÞ¸Ä¼ÇÂ¼£¬2¡¢Ôö¼Ó¼ÇÂ¼£¬3¡¢²éÑ¯¼ÇÂ¼£¬4¡¢É¾³ý¼ÇÂ¼.(°´qÎªÍË³ö)");  
conn,cur=connDB();  
number=input();  
while(result):  
    if(number=='q'):  
        print("½áÊø²Ù×÷");  
        break;  
    elif(int(number)==1):  
        sql=input("ÇëÊäÈë¸üÐÂÓï¾ä£º");  
        try:  
            exeUpdate(conn, cur, sql);  
            print("¸üÐÂ³É¹¦");  
        except Exception:  
            print("¸üÐÂÊ§°Ü");  
            raise;  
    elif(int(number)==2):  
            sql=input("ÇëÊäÈëÐÂÔöÓï¾ä:");  
            try:  
                exeUpdate(conn, cur, sql);  
                print("ÐÂÔö³É¹¦");  
            except Exception:  
                print("ÐÂÔöÊ§°Ü");  
                raise;  
    elif(int(number)==3):  
        sql=input("ÇëÊäÈë²éÑ¯Óï¾ä£º");  
        try:  
            cur=exeQuery(cur, sql);  
            for item in cur:  
                print("Id="+str(item[0])+" name="+item[1]);  
        except Exception:  
            print("²éÑ¯³ö´í");  
            raise;  
    elif(int(number)==4):  
        Ids=input("ÇëÊäÈëId£¬²¢ÓÃ¿Õ¸ñ¸ô¿ª");  
        try:  
            exeDelete(conn, cur, Ids);  
            print("É¾³ý³É¹¦");  
        except Exception:  
            print("É¾³ýÊ§°Ü");  
            raise;  
    else:  
        print("·Ç·¨ÊäÈë£¬½«½áÊø²Ù×÷!");  
        result=False;  
        break;  
    print("ÇëÑ¡ÔñÒÔÉÏËÄ¸ö²Ù×÷£º1¡¢ÐÞ¸Ä¼ÇÂ¼£¬2¡¢Ôö¼Ó¼ÇÂ¼£¬3¡¢²éÑ¯¼ÇÂ¼£¬4¡¢É¾³ý¼ÇÂ¼.(°´qÎªÍË³ö)");  
    number=input("ÇëÑ¡Ôñ²Ù×÷");  