Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write address Book by Python to realize Fuzzy query function through Database Storage

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not quite understand the knowledge points of this article "how to write an address book in Python to achieve fuzzy query function through database storage", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "how to write an address book through database storage to achieve fuzzy query function" article.

1. Request

The database stores the address book, which is required to query according to the name / phone number. There is only one input entry for the query condition, which automatically identifies whether the name or number is entered, and allows fuzzy query.

two。 Realize the function

It can be operated by entering instructions.

(1) first enter "add" to 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 () # submit, otherwise it cannot be saved

(2) enter "delete" to delete the specified contact information.

Enter a name to delete:

Cursor = c.execute ("SELECT name from TA where name ='% slots;"% I)

Enter the phone number to delete:

Cursor = c.execute ("SELECT name from TA where telenumber='% slots;"% I)

(3) enter "search", you can enter the contact person or phone number to query the contact information. Here, fuzzy query and precise query are realized.

Enter name for query:

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

Enter phone number to enquire:

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

(4) enter "searchall" to query all contact information.

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

3. Database sqlite3

Python comes with a lightweight relational database, sqlite. This database uses SQL language. As a back-end database, sqlite can be used to build websites with Python, or to create tools with data storage needs. SqlLite also has a wide range of applications in other fields, such as HTML5 and mobile. Sqlite3 in the Python standard library provides an interface to the database. Therefore, the sqlite3 database is used to store the contact information of the address book.

Source code:

Import sqlite3import re# opens the local database to store user information conn = sqlite3.connect ('mysql_telephone_book.db') c = conn.cursor () # create a table under the database, and the code to create the table needs to be commented out after the first execution Otherwise, executing the program again will always prompt:''c.execute already exists in the table ("CREATE TABLE TA (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (50), TELENUMBER TEXT)) ")''conn.commit () # commit current transaction # add user information def insert (): global conn c = conn.cursor () ID=int (input (" Please enter id number: ") name=input (" Please enter name: ") age=int (input (" Please enter age: ") address=input (" Please enter address: ") telenumber=input (" Please enter phone number: ") 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 () # submit, otherwise print cannot be saved ("submit successful!") # Delete user information def delete (): global conn c=conn.cursor () I = input ("Please enter the contact name or phone number to be deleted:") if len (I) < 11: cursor = c.execute ("SELECT name from TA where name ='% slots;"% I) for row in cursor: if I = = row [0]: c.execute ("DELETE from TA where name ='% s' "% I) conn.commit () print (" contact information deleted successfully! ") Break else: print (the contact does not exist!) Else: cursor = c.execute ("SELECT name from TA where telenumber='% slots;"% I) for row in cursor: if I = = row [0]: c.execute ("DELETE from TA where telenumber='% slots;"% I) conn.commit () print ("contact information deleted successfully!") Break else: print ("the phone number is wrong!") # query user information def search (): global conn c = conn.cursor () I = input ("Please enter the contact name or phone number you want to query:") if i.isnumeric (): sql1 = "SELECT id,name,age, address Telenumber from TA where telenumber like'% "+ I +"%'"cursor = c.execute (sql1) res=cursor.fetchall () if len (res)! = 0: for row in res: print (" id: {0} ".format (row [0])) print (" name: {0} ".format (row [1])) print (" Age: {0} ".format (row [2])) Print ("address: {0}" .format (row [3])) print ("phone number: {0}" .format (row [4])) else: print ("No phone number!") Else: sql1= "SELECT id,name,age, address, telenumber from TA where name like'%" + I + "%'" cursor = c.execute (sql1) res=cursor.fetchall () if len (res) = 0: print ("this contact does not exist!") Else: for row in res: print ("id: {0}" .format (row [0])) print ("name: {0}" .format (row [1])) print ("age: {0}" .format (row [2])) print ("address: {0}" .format (row [3])) print ("phone number: {0}" .format ( Row [4]) # display all user information def showall (): global conn c = conn.cursor () cursor = c.execute ("SELECT id" Name, age, address Telenumber from TA ") for row in cursor: print (" id: {0} ".format (row [0])) print (" name: {0} ".format (row [1])) print (" age: {0} ".format (row [2])) print (" address: {0} ".format (row [3])) print (" phone number: {0} ".fo rmat (row [4])) print (" instructions are as follows:\ N1. Enter\ "add\" to add contact information to the address book\ N2. Type\ "delete\" to delete the specified contact information\ n3 in the address book. Enter\ "searchall\" to query all users in the address book\ n4. Enter\ "search\" find information by name or mobile phone number ") while 1: temp = input (" Please enter instructions: ") if temp = =" add ": insert () print (" added successfully! ") Temp1=input ("do you want to continue to operate the address book? (y or n)") if temp1== "n": print ("exit successfully!") Break else: continue elif temp== "delete": delete () temp1 = input ("do you want to continue to operate the address book? (y or n)") if temp1 = "n": print ("exit successfully!") Break else: continue elif temp== "searchall": showall () temp1 = input ("do you want to continue operating your address book? (y or n)") if temp1 = "n": print ("exit successfully!") Break else: continue elif temp== "search": search () temp1 = input ("do you want to continue operating your address book? (y or n)") if temp1 = "n": print ("exit successfully!") Break else: continue else: print ("Please enter correct instructions!") Conn.close () # shuts down the database above is about "how Python compiles the address book to realize the fuzzy query function through the database storage" this article content, believed that everybody has the certain understanding, hoped the editor shares the content to be helpful to you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report