Sqlite3: open delete and modify a python database

  • 2020-04-02 13:32:23
  • OfStack


#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(125), telnum varchar(128))""")
        return cur, conn
# Look up all the information 

def showalldata():
        print "------------------- After processing the data -------------------"
        hel = opendata()
        cur = hel[1].cursor()
        cur.execute("select * from tianjia")
        res = cur.fetchall()
        for line in res:
                for h in line:
                        print h,
                print
        cur.close()
# The input information 

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
#  ( add )   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 adding data ----------------"
        showalldata()
        hel[1].close()
#   Deletes the contents of the database 

def deldata():
        welcome = "------------------ You are welcome to use the delete database feature ------------------"
        print welcome
        delchoice = raw_input(" Please enter the number of the user you want to delete: ")
        hel = opendata()              #  Returns a cursor conn
        hel[1].execute("delete from tianjia where id ="+delchoice)
        hel[1].commit()
        print "----------------- Congratulations on your data deletion ----------------"
        showalldata()
        hel[1].close()
#  Modify the content 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 change :")
        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 = "-------------------- You are welcome to use the query database function -----------------"
        print welcome
        choice = str(raw_input(" Please enter the number of the user you want to inquire: "))
        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 (" The data number of your query is %s" % id1)
        print (" The name of the data you are querying is %s" % username)
        print (" The data password you are querying is %s" % passworld)
        print (" The data address you are querying is %s" % address)
        print (" The number you are calling is %s" % telnum)
        cur.close()
        hel[1].close()
#  Whether or not 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 our contact list ---------"
                print welcome
                choiceshow = """
 Please select your further option: 
( add ) Add content to the database 
( delete ) Delete the contents of the database 
( Modify the ) Modify the contents of the library 
 Query the content of the data 
 Select the operation you want to perform: 
"""
                choice = raw_input(choiceshow)
                if choice == " add ":
                        adddata()
                        contnue1(flag)
                elif choice == " delete ":
                        deldata()
                        contnue1(flag)
                elif choice == " Modify the ":
                        alter()
                        contnue1(flag)
                elif choice == " The query ":
                        searchdata()
                        contnue1(flag)
                else:
                        print " You typed wrong, please retype "


Related articles: