Original: Python written contacts, support fuzzy queries, using the database storage

1. Requirements

       Contacts database storage requirements by name / phone number inquiry, only one input query entry, automatic identification number or name is entered, allowing fuzzy search.

2. realize the function

       It can be operated through the input command.

(1) First enter the "add", can add contact information to the address book.

sql1 = 'insert into TA(ID,NAME,AGE,ADDRESS,TELENUMBER)'
sql1 += 'values("%d","%s","%d","%s","%s");' % (ID,name, age, address, telenumber)
conn.execute(sql1)
conn.commit()#提交,否则无法保存

(2) Enter the "delete", you can delete the specified contact information.

Enter your name deleted:

cursor = c.execute("SELECT name from TA where name = '%s';"%i)

Enter the phone number deleted:

cursor = c.execute("SELECT name from TA where telenumber= '%s';" % i)

(3) Enter the "search", or you can enter the contact telephone number, contact information inquiries, realized here Fuzzy query and precise queries.

Enter the name of the query:

sql1 = "SELECT id,name,age, address, telenumber from TA where telenumber like '%" + i + "%'"
cursor = c.execute(sql1)

Enter the phone number inquiries:

sql1="SELECT id,name,age, address, telenumber from TA where name like '%"+i+"%'"
cursor = c.execute(sql1)

(4) Enter "searchall", all queries contact information.

cursor = c.execute("SELECT id, name, age, address, telenumber  from TA")

 

3. sqlite3 database

       Python comes with a lightweight relational database sqlite. This database using SQL language. sqlite as the back-end database, it can be used with Python construction site, or making tool data storage needs. sqlLite also have wide application in other areas, such as HTML5 and mobile terminal. Sqlite3 Python standard library provides an interface to the database. Therefore, the use of contact information sqlite3 database stored address book.

Source:

the sqlite3 Import 
Import Re 
# open local database for storing user information 
Conn = sqlite3.connect ( 'mysql_telephone_book.db') 
C = conn.cursor () 
# create a table in the database, create a code table in the first after you do need to comment, otherwise the program will always prompt again: this table already exists 
' '' c.execute ( "the CREATE tABLE TA 
       (ID INT the NOT NULL PRIMARY KEY, 
       NAME TEXT the NOT NULL, 
       AGE INT the NOT NULL, 
       ADDRESS CHAR ( 50), 
       TELENUMBER the TEXT); ") '' ' 

conn.commit () to submit the current transaction # 
# increase user information 
DEF INSERT (): 
    Global Conn 
    C = conn.cursor () 
    ID = int (iNPUT (" Please enter id number: ")) 
    name = the iNPUT (" Please enter your name: ") 
    Age = int (the iNPUT (" Please enter the Age: ")) 
    Address the INPUT = ( "Please enter the address:")
    telenumber = input ( "Enter a phone number:") 
    SQL1 = 'INSERT INTO the TA (ID, NAME, of AGE, ADDRESS, TELENUMBER)' 
    SQL1 + = 'values ( "% D", "% S", "% D" , "% S", "% S"); '% (ID, name, Age, address, telenumber) 
    conn.execute (SQL1) 
    conn.commit () # submit, or can not save 
    print ( "submit success !!" ) 
# delete user information 
DEF the delete (): 
    , Ltd. Free Join conn 
    c = conn.cursor () 
    i = the iNPUT ( "contact name or phone number to be deleted, enter:") 
    IF len (i) <11: 
        the Cursor = c. Execute ( "the TA from the SELECT name WHERE name = '% S';"% I) 
        for in Row Cursor: 
            IF I == Row [0]: 
                c.execute ( "the TA from the DELETE WHERE name = '% S';"% i) 
                conn.commit () 
                Print (" Successfully deleted contact information !! ")
                BREAK 
        else:
            Print ( "the contact does not exist !!") 
    the else: 
        Cursor = c.execute ( "WHERE telenumber the TA from the SELECT name = '% S';"% I) 
        for in Row Cursor: 
            IF I == Row [0 ]: 
                c.execute ( "TA dELETE from the WHERE telenumber = '% S';"% i) 
                conn.commit () 
                Print ( "successfully deleted contact information !!") 
                BREAK 
        the else: 
            Print ( "the wrong phone number! ! ") 
# query user information 
DEF Search (): 
    , Ltd. Free Join conn 
    c = conn.cursor () 
    i = the iNPUT (" contact name or phone number you want to enter the query: ") 
    IF i.isnumeric (): 
        SQL1 =" the SELECT the above mentioned id, name, Age, address , telenumber from TA where telenumber like ' % "+ i + "%'"
        cursor = c.execute(sql1)
        cursor.fetchall = RES () 
        IF len (RES) = 0:! 
            for RES in Row: 
                Print ( "ID: {0}". format (Row [0])) 
                Print ( "Name: {0}". format (Row [. 1])) 
                Print ( "Age: {0}." the format (Row [2])) 
                Print ( "address: {0}." the format (Row [. 3])) 
                Print ( "p: { } 0 "the format (Row [. 4])). 
        the else: 
            Print (" no such telephone number !! ") 
                Print (" ID: {0}. "the format (Row [0])) 
                Print (" name: {0}. "format(row[1]))
    else:
        sql1="SELECT id,name,age, address, telenumber from TA where name like '%"+i+"%'"
        cursor = c.execute(sql1)
        cursor.fetchall = RES () 
        IF len (RES) == 0: 
            Print ( "the contact does not exist !!") 
        the else: 
            for RES in Row: 
                Print ( "Age: {0}". format ( row [2 ])) 
                Print ( "address: {0}" the format (Row [. 3])). 
                Print ( "phone number: {0}." the format (Row [. 4])) 
# all user information 
def showall (): 
    Conn Global 
    C = conn.cursor () 
    Cursor = c.execute ( "the SELECT ID, name, Age, address, telenumber from the TA") 
    for in Row Cursor: 
        Print ( "ID: {0}." the format (Row [0 ])) 
        Print ( "name: {0}." the format (Row [. 1])) 
        Print ( "Age: {0}." the format (Row [2])) 
        Print ( "address:{0}".format(row[3])) 
        Print ( "phone number: {0}." the format (Row [. 4])) 
Print ( "instructions are as follows:. \ n1 input \" add \ "add contact information to a phonebook \ n2 input \. "delete \" to delete the specified address book contact information \ n3. enter \ "searchall \" all user queries in the address book \ n4. enter \ "search \"According to a name or phone number to find information ") 
the while 1: 
    the TEMP = the INPUT (" Please enter command: ")
    the TEMP == IF "the Add": 
        INSERT () 
        Print ( "! added successfully") 
        temp1 = the INPUT ( "whether to continue contacts (the y-or the n-)?") 
        IF temp1 == "the n-": 
            Print ( "successful exit !! ") 
            BREAK 
        the else: 
            the continue 
    elif the TEMP ==" the Delete ": 
        the Delete () 
        temp1 = the INPUT (" whether to continue contacts (the y-or the n-?) ") 
        iF temp1 ==" the n-": 
            Print (" successful exit !! ") 
            BREAK 
        the else: 
            the continue 
    elif temp=="searchall":
        showAll () 
        temp1 = the INPUT (" Do you want to continue contacts (the y-or the n-?) ") 
        iF temp1 ==" the n-":
            print ( "successful exit !!") 
            BREAK 
        the else:
            the Continue 
    elif the TEMP == "Search": 
        Search () 
        temp1 = the INPUT ( "Do you want to continue contacts (the y-or the n-)?") 
        IF temp1 == "the n-": 
            Print ( "successful exit !!") 
            BREAK 
        the else: 
            the Continue 
    the else: 
        Print ( "Please enter the correct command !!") 
conn.Close () # close the database

 

 

Guess you like

Origin www.cnblogs.com/momoli/p/11202060.html