Example of opening, deleting, and modifying a database using sqlite3 in python

Source: Internet
Author: User
This article describes how to use python to operate sqlite3 to open, delete, and modify a database. For more information, see The code is as follows:


# Coding = UTF-8
_ Auther _ = 'xianbao'
Import sqlite3
# Open a database
Def opendata ():
Conn = sqlite3.connect ("mydb. db ")
Cur = conn.exe cute ("" 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
# Querying all information


Def showalldata ():
Print "------------------- processed data -------------------"
El = opendata ()
Cur = El [1]. cursor ()
Cur.exe cute ("select * from tianjia ")
Res = cur. fetchall ()
For line in res:
For h in line:
Print h,
Print
Cur. close ()
# Input information


Def ():
Username1 = str (raw_input ("enter your username :"))
Passworld1 = str (raw_input ("enter your password :"))
Address1 = str (raw_input ("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 data adding function ---------------------"""
Print welcome
Person = ()
El = opendata ()
Hel1000012.16.exe cute ("insert into tianjia (username, passworld, address, telnum) values (?,?,?,?) ",
(Person [0], person [1], person [2], person [3])
El [1]. commit ()
Print "----------------- congratulations on your data ----------------"
Showalldata ()
El [1]. close ()
# (Delete) delete database content


Def deldata ():
Welcome = "------------------ welcome to use the database deletion function ------------------"
Print welcome
Delchoice = raw_input ("Enter the number of the user you want to delete :")
El = opendata () # return cursor conn
Helstm1cmd.exe cute ("delete from tianjia where id =" + delchoice)
El [1]. commit ()
Print "----------------- congratulations on your data, deleted successfully ----------------"
Showalldata ()
El [1]. close ()
# (Modify) modify data content


Def alter ():
Welcome = "-------------------- welcome to use the database modification function -----------------"
Print welcome
Changechoice = raw_input ("Enter the user ID you want to modify :")
El = opendata ()
Person = ()
Helstm1cmd.exe cute ("update tianjia set username = ?, Passworld = ?, Address = ?, Telnum =? Where id = "+ changechoice,
(Person [0], person [1], person [2], person [3])
El [1]. commit ()
Showalldata ()
El [1]. close ()
# Querying data


Def searchdata ():
Welcome = "-------------------- welcome to use the database query function -----------------"
Print welcome
Choice = str (raw_input ("Enter the user ID you want to query :"))
El = opendata ()
Cur = El [1]. cursor ()
Cur.exe cute ("select * from tianjia where id =" + choice)
El [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 you queried is % s" % id1)
Print ("The data name you query is % s" % username)
Print ("your data password is % s" % passworld)
Print ("The data address you query is % s" % address)
Print ("your data phone number is % s" % telnum)
Cur. close ()
El [1]. close ()
# Continue


Def contnue1 ():
Choice = raw_input ("continue? (Y or n ):")
If choice = 'y ':
A = 1
Else:
A = 0
Return


If _ name _ = "_ main __":
Flag = 1
While flag:
Welcome = "--------- welcome to Xianbao database address book ---------"
Print welcome
Choiceshow = """
Select your further options:
(Add) add content to the database
(Delete) delete database content
(Modify) modify the library content
(Query) query data content
Select the operation you want:
"""
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 "Your input is incorrect. please enter it again"

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.