Python Operations database Sqlite3 Open the database, delete, modify the example _python

Source: Internet
Author: User

Copy Code code as follows:

#coding =utf-8
__auther__ = ' Xianbao '
Import Sqlite3
# Open Database
Def opendata ():
conn = Sqlite3.connect ("mydb.db")
cur = Conn.execute ("" "CREATE table if not exists Tianjia (
ID Integer PRIMARY key autoincrement, username varchar (128), Passworld varchar (128),
Address varchar (telnum), varchar (128)) "" "
Return CUR, conn
#查询全部的信息


def showalldata ():
        Print------------------- Data after processing-------------------"
        hel = OpenData ()
         cur = hel[1].cursor ()
        cur.execute ("SELECT * From Tianjia ")
        res = Cur.fetchall ()
         for line in res:
                 for H on line:
                         print H,
                 Print
         Cur.close ()
#输入信息


def into ():
username1 = str (raw_input ("Please enter your username:")
Passworld1 = str (raw_input ("Please enter your password:")
Address1 = str (raw_input ("Please enter your address:")
TELNUM1 = str (raw_input ("Please enter your contact number:")
Return username1, Passworld1, Address1, TELNUM1
# (added) to add content to the database


Def adddata ():
Welcome = ""-------------------Welcome to the Add Data feature---------------------""
Print Welcome
person = into ()
Hel = OpenData ()
Hel[1].execute ("INSERT into Tianjia (username, Passworld, address, Telnum) VALUES (?,?,?,?)",
(Person[0], person[1], person[2], person[3))
Hel[1].commit ()
Print "-----------------Congratulations on your data, add success----------------"
ShowAllData ()
Hel[1].close ()
# (delete) Delete the contents of the database


def deldata ():
        welcome = "------------------ You are welcome to use the Remove database feature------------------
        print Welcome
         Delchoice = raw_input ("Please enter the number of the user you want to delete:")
         hel = OpenData ()               # RETURN Cursor conn
        hel[1].execute ("Delete from Tianjia where id =" +delchoice)
         hel[1].commit ()
        print------- ----------congratulate you. Data, delete successful----------------"
        showalldata ()
         hel[1].close ()
# (Modify) Modify the contents of the data


Def Alter ():
        welcome = "-------------------- You are welcome to use the Modify Database feature-----------------
        print Welcome
         Changechoice = raw_input ("Please enter the number of the user you want to modify:")
         Hel =opendata ()
        person = into ()
         hel[1].execute ("Update Tianjia set username=?, passworld=?, address=?,telnum=?" where id= "+changechoice,
                                 ( Person[0], person[1], person[2], person[3])
        hel[1].commit ()
        showalldata ()
        hel[1]. Close ()
# query data


Def searchdata ():
Welcome = "--------------------welcome you to use the query database feature-----------------"
Print Welcome
Choice = str (raw_input ("Please enter the number of the user you want to query:"))
Hel = OpenData ()
cur = hel[1].cursor ()
Cur.execute ("select * from Tianjia where id=" +choice)
Hel[1].commit ()
row = Cur.fetchone ()
ID1 = str (row[0])
Username = str (row[1])
Passworld = str (row[2])
Address = str (row[3])
Telnum = str (row[4])
Print "-------------------Congratulations, the data you are looking for is as follows---------------------"
Print ("Your query data number is%s"% id1)
Print ("Your query's data name is%s"% username)
Print ("Your query data password is%s"% Passworld)
Print ("Your query's data address is%s"%)
Print ("Your query data phone is%s"% telnum)
Cur.close ()
Hel[1].close ()
# whether to continue


Def contnue1 (a):
Choice = Raw_input (do you want to continue?) (Y or N): ")
if choice = = ' Y ':
A = 1
Else
A = 0
Return a


if __name__ = = "__main__":
Flag = 1
While flag:
Welcome = "---------Welcome to use the Xian Bao database address Book---------"
Print Welcome
Choiceshow = "" "
Please select your further selections:
(add) Add content to the database
(delete) Delete the contents of the database
(modify) Modify the contents of the stack
(query) Query the contents of the data
Select the action you want to do:
"""
Choice = Raw_input (choiceshow)
if choice = = "Add":
AddData ()
CONTNUE1 (flag)
elif Choice = = "Delete":
Deldata ()
CONTNUE1 (flag)
elif Choice = = "Modify":
Alter ()
CONTNUE1 (flag)
elif Choice = = "Query":
Searchdata ()
CONTNUE1 (flag)
Else
Print "You entered the error, please re-enter"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.