juanquintero
Game Exploiter
LEVEL 1
300 XP
In this tutorial we will create a simple CRUD ( Create, Read ) Application using Python/SQLite. Python is a computer programming language that lets work faster and convenient because of its user - friendly environment. Python supports packages and modules, which encourage a developer to program in a modularity and reusable way. By the way this tutorial is consist of two parts, for now we will tackle only about create and read on Python with SQLite database. So let's now do the coding
Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.
Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Then copy code that I provided below and paste it inside the IDLE text editor
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor
Creating the Database Connection
Then after setting up the design we will now create the database function. To do that just simply copy the code below and paste it inside the IDLE text editor
Creating the Create Function
This where the code will send the data to database when button create is clicked. Copy the code below and paste it inside the IDLE text editor
Creating the Read Function
This where the code will retrieve the data from the database and display it in the Python widget called Treeview. Just simply copy the code below and paste it inside the IDLE text editor
Creating the Exit Function
This where the code will destroy the frame and force to close the application when button exit is clicked. To do that just simple copy the code below and paste it inside the IDLE text editor
Initializing the Application
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
There you have it we create a simple CRUD ( Create, Read ) Application using Python and SQLite. For the next part of the tutorial we will tackle on how to update and delete the data so just stay tuned to this site. I hope that this simple tutorial help you improve to your programming carrier. Enjoy Coding!!
Download
Getting started
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.
Installing SQLite Browser
After you installed Python, we will now then install the SQLite, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.
Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python.
Then copy code that I provided below and paste it inside the IDLE text editor
- from
tkinter import
*
- import
sqlite3
- import
tkinter.ttk
as
ttk
- import
tkinter.messagebox
as
tkMessageBox
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor
- root =
Tk(
)
- root.title
(
"Python: Simple CRUD Applition"
)
- screen_width =
root.winfo_screenwidth
(
)
- screen_height =
root.winfo_screenheight
(
)
- width =
900
- height =
500
- x =
(
screen_width/2
)
- (
width/2
)
- y =
(
screen_height/2
)
- (
height/2
)
- root.geometry
(
'%dx%d+%d+%d'
% (
width,
height,
x,
y)
)
- root.resizable
(
0
,
0
)
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor
- #==================================VARIABLES==========================================
- FIRSTNAME =
StringVar(
)
- LASTNAME =
StringVar(
)
- GENDER =
StringVar(
)
- ADDRESS =
StringVar(
)
- USERNAME =
StringVar(
)
- PASSWORD =
StringVar(
)
- #==================================FRAME==============================================
- Top =
Frame(
root,
width=
900
,
height=
50
,
bd=
8
,
relief=
"raise"
)
- Top.pack
(
side=
TOP)
- Left =
Frame(
root,
width=
300
,
height=
500
,
bd=
8
,
relief=
"raise"
)
- Left.pack
(
side=
LEFT)
- Right =
Frame(
root,
width=
600
,
height=
500
,
bd=
8
,
relief=
"raise"
)
- Right.pack
(
side=
RIGHT)
- Forms =
Frame(
Left,
width=
300
,
height=
450
)
- Forms.pack
(
side=
TOP)
- Buttons =
Frame(
Left,
width=
300
,
height=
100
,
bd=
8
,
relief=
"raise"
)
- Buttons.pack
(
side=
BOTTOM)
- RadioGroup =
Frame(
Forms)
- Male =
Radiobutton(
RadioGroup,
text=
"Male"
,
variable=
GENDER,
value=
"Male"
,
font=
(
'arial'
,
16
)
)
.pack
(
side=
LEFT)
- Female =
Radiobutton(
RadioGroup,
text=
"Female"
,
variable=
GENDER,
value=
"Female"
,
font=
(
'arial'
,
16
)
)
.pack
(
side=
LEFT)
- #==================================LABEL WIDGET=======================================
- txt_title =
Label(
Top,
width=
900
,
font=
(
'arial'
,
24
)
,
text =
"Python: Simple CRUD Application"
)
- txt_title.pack
(
)
- txt_firstname =
Label(
Forms,
text=
"Firstname:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_firstname.grid
(
row=
0
,
stick=
"e"
)
- txt_lastname =
Label(
Forms,
text=
"Lastname:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_lastname.grid
(
row=
1
,
stick=
"e"
)
- txt_gender =
Label(
Forms,
text=
"Gender:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_gender.grid
(
row=
2
,
stick=
"e"
)
- txt_address =
Label(
Forms,
text=
"Address:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_address.grid
(
row=
3
,
stick=
"e"
)
- txt_username =
Label(
Forms,
text=
"Username:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_username.grid
(
row=
4
,
stick=
"e"
)
- txt_password =
Label(
Forms,
text=
"Password:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_password.grid
(
row=
5
,
stick=
"e"
)
- txt_result =
Label(
Buttons)
- txt_result.pack
(
side=
TOP)
- #==================================ENTRY WIDGET=======================================
- firstname =
Entry(
Forms,
textvariable=
FIRSTNAME,
width=
30
)
- firstname.grid
(
row=
0
,
column=
1
)
- lastname =
Entry(
Forms,
textvariable=
LASTNAME,
width=
30
)
- lastname.grid
(
row=
1
,
column=
1
)
- RadioGroup.grid
(
row=
2
,
column=
1
)
- address =
Entry(
Forms,
textvariable=
ADDRESS,
width=
30
)
- address.grid
(
row=
3
,
column=
1
)
- username =
Entry(
Forms,
textvariable=
USERNAME,
width=
30
)
- username.grid
(
row=
4
,
column=
1
)
- password =
Entry(
Forms,
textvariable=
PASSWORD,
show=
"*"
,
width=
30
)
- password.grid
(
row=
5
,
column=
1
)
- #==================================BUTTONS WIDGET=====================================
- btn_create =
Button(
Buttons,
width=
10
,
text=
"Create"
,
command=
Create)
- btn_create.pack
(
side=
LEFT)
- btn_read =
Button(
Buttons,
width=
10
,
text=
"Read"
,
command=
Read )
- btn_read.pack
(
side=
LEFT)
- btn_update =
Button(
Buttons,
width=
10
,
text=
"Update"
,
state=
DISABLED)
- btn_update.pack
(
side=
LEFT)
- btn_delete =
Button(
Buttons,
width=
10
,
text=
"Delete"
,
state=
DISABLED)
- btn_delete.pack
(
side=
LEFT)
- btn_exit =
Button(
Buttons,
width=
10
,
text=
"Exit"
,
command=
Exit)
- btn_exit.pack
(
side=
LEFT)
- #==================================LIST WIDGET========================================
- scrollbary =
Scrollbar(
Right,
orient=
VERTICAL)
- scrollbarx =
Scrollbar(
Right,
orient=
HORIZONTAL)
- tree =
ttk.Treeview
(
Right,
columns=
(
"Firstname"
,
"Lastname"
,
"Gender"
,
"Address"
,
"Username"
,
"Password"
)
,
selectmode=
"extended"
,
height=
500
,
yscrollcommand=
scrollbary.set
,
xscrollcommand=
scrollbarx.set
)
- scrollbary.config
(
command=
tree.yview
)
- scrollbary.pack
(
side=
RIGHT,
fill=
Y)
- scrollbarx.config
(
command=
tree.xview
)
- scrollbarx.pack
(
side=
BOTTOM,
fill=
X)
- tree.heading
(
'Firstname'
,
text=
"Firstname"
,
anchor=
W)
- tree.heading
(
'Lastname'
,
text=
"Lastname"
,
anchor=
W)
- tree.heading
(
'Gender'
,
text=
"Gender"
,
anchor=
W)
- tree.heading
(
'Address'
,
text=
"Address"
,
anchor=
W)
- tree.heading
(
'Username'
,
text=
"Username"
,
anchor=
W)
- tree.heading
(
'Password'
,
text=
"Password"
,
anchor=
W)
- tree.column
(
'#0'
,
stretch=
NO,
minwidth=
0
,
width=
0
)
- tree.column
(
'#1'
,
stretch=
NO,
minwidth=
0
,
width=
80
)
- tree.column
(
'#2'
,
stretch=
NO,
minwidth=
0
,
width=
120
)
- tree.column
(
'#3'
,
stretch=
NO,
minwidth=
0
,
width=
80
)
- tree.column
(
'#4'
,
stretch=
NO,
minwidth=
0
,
width=
150
)
- tree.column
(
'#5'
,
stretch=
NO,
minwidth=
0
,
width=
120
)
- tree.column
(
'#6'
,
stretch=
NO,
minwidth=
0
,
width=
120
)
- tree.pack
(
)
Creating the Database Connection
Then after setting up the design we will now create the database function. To do that just simply copy the code below and paste it inside the IDLE text editor
- #==================================METHODS============================================
- def
Database(
)
:
- global
conn,
cursor
- conn =
sqlite3.connect
(
'pythontut.db'
)
- cursor =
conn.cursor
(
)
- cursor.execute
(
"CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, gender TEXT, address TEXT, username TEXT, password TEXT)"
)
Creating the Create Function
This where the code will send the data to database when button create is clicked. Copy the code below and paste it inside the IDLE text editor
- def
Create(
)
:
- if
FIRSTNAME.get
(
)
==
""
or
LASTNAME.get
(
)
==
""
or
GENDER.get
(
)
==
""
or
ADDRESS.get
(
)
==
""
or
USERNAME.get
(
)
==
""
or
PASSWORD.get
(
)
==
""
:
- txt_result.config
(
text=
"Please complete the required field!"
,
fg=
"red"
)
- else
:
- Database(
)
- cursor.execute
(
"INSERT INTO `member` (firstname, lastname, gender, address, username, password) VALUES(?, ?, ?, ?, ?, ?)"
,
(
str
(
FIRSTNAME.get
(
)
)
,
str
(
LASTNAME.get
(
)
)
,
str
(
GENDER.get
(
)
)
,
str
(
ADDRESS.get
(
)
)
,
str
(
USERNAME.get
(
)
)
,
str
(
PASSWORD.get
(
)
)
)
)
- conn.commit
(
)
- FIRSTNAME.set
(
""
)
- LASTNAME.set
(
""
)
- GENDER.set
(
""
)
- ADDRESS.set
(
""
)
- USERNAME.set
(
""
)
- PASSWORD.set
(
""
)
- cursor.close
(
)
- conn.close
(
)
- txt_result.config
(
text=
"Created a data!"
,
fg=
"green"
)
Creating the Read Function
This where the code will retrieve the data from the database and display it in the Python widget called Treeview. Just simply copy the code below and paste it inside the IDLE text editor
- def
Read(
)
:
- tree.delete
(
*tree.get_children
(
)
)
- Database(
)
- cursor.execute
(
"SELECT * FROM `member` ORDER BY `lastname` ASC"
)
- fetch =
cursor.fetchall
(
)
- for
data in
fetch:
- tree.insert
(
''
,
'end'
,
values=
(
data[
1
]
,
data[
2
]
,
data[
3
]
,
data[
4
]
,
data[
5
]
,
data[
6
]
)
)
- cursor.close
(
)
- conn.close
(
)
- txt_result.config
(
text=
"Successfully read the data from database"
,
fg=
"black"
)
Creating the Exit Function
This where the code will destroy the frame and force to close the application when button exit is clicked. To do that just simple copy the code below and paste it inside the IDLE text editor
- def
Exit(
)
:
- result =
tkMessageBox.askquestion
(
'Python: Simple CRUD Applition'
,
'Are you sure you want to exit?'
,
icon=
"warning"
)
- if
result ==
'yes'
:
- root.destroy
(
)
- exit(
)
Initializing the Application
After finishing the function save the application as 'index.py'. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
- #==================================INITIALIZATION=====================================
- if
__name__ ==
'__main__'
:
- root.mainloop
(
)
There you have it we create a simple CRUD ( Create, Read ) Application using Python and SQLite. For the next part of the tutorial we will tackle on how to update and delete the data so just stay tuned to this site. I hope that this simple tutorial help you improve to your programming carrier. Enjoy Coding!!
Download
You must upgrade your account or reply in the thread to view the hidden content.