fantom98158
Battle Mage
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial we will create a Update SQLite Data using Python. This code will update the table row of SQLite database when user click double click a row and submit the entry form. The code use tkinter module to design a layout and call a specific functions. A function will automatically call when you double click a row and submit the designated entry widget then it will run the SQLite UPDATE query in order to safely change the data in the database.
We will be using Python programming language because it is widely used as an advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem.
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.
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, then save it as connection.py.
Assigning Variables
This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.
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 Main Function
This is where the code that contains the main funcitions. This code will update SQLite data when the button is clicked. To do that just copy and write these blocks of code.
Initializing the Application
After finishing the function save the application as index.py. This function will run the code and check if the main application is initialized properly. To do that copy the code below and paste it inside the IDLE text editor.
There you have it we just created a Update SQLite Data Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Download
We will be using Python programming language because it is widely used as an advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem.
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
tkinter.ttk
as
ttk
- import
tkinter.messagebox
as
tkMessageBox
- import
connection
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 - Update SQLite Data"
)
- 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
)
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, then save it as connection.py.
- import
sqlite3
- def
Database(
)
:
- global
conn,
cursor
- conn =
sqlite3.connect
(
'db_member.db'
)
- cursor =
conn.cursor
(
)
- cursor.execute
(
"CREATE TABLE IF NOT EXISTS `member` (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT, username TEXT, password TEXT)"
)
Assigning Variables
This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.
- #==================================VARIABLES==========================================
- FIRSTNAME =
StringVar(
)
- LASTNAME =
StringVar(
)
- ADDRESS =
StringVar(
)
- USERNAME =
StringVar(
)
- PASSWORD =
StringVar(
)
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.
- #==================================FRAME==============================================
- Top =
Frame(
root,
width=
900
,
height=
50
,
bd=
8
,
relief=
"raise"
)
- Top.pack
(
side=
TOP)
- Left =
Frame(
root,
width=
600
,
height=
500
,
bd=
8
,
relief=
"raise"
)
- Left.pack
(
side=
LEFT)
- Right =
Frame(
root,
width=
300
,
height=
500
,
bd=
8
,
relief=
"raise"
)
- Right.pack
(
side=
RIGHT)
- Forms =
Frame(
Right,
width=
300
,
height=
450
)
- Forms.pack
(
side=
TOP)
- Buttons =
Frame(
Right,
width=
300
,
height=
100
,
bd=
8
,
relief=
"raise"
)
- Buttons.pack
(
side=
BOTTOM)
- RadioGroup =
Frame(
Forms)
- #==================================LABEL WIDGET=======================================
- txt_title =
Label(
Top,
width=
900
,
font=
(
'arial'
,
24
)
,
text =
"Python - Update SQLite Data"
)
- 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_address =
Label(
Forms,
text=
"Address:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_address.grid
(
row=
2
,
stick=
"e"
)
- txt_username =
Label(
Forms,
text=
"Username:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_username.grid
(
row=
3
,
stick=
"e"
)
- txt_password =
Label(
Forms,
text=
"Password:"
,
font=
(
'arial'
,
16
)
,
bd=
15
)
- txt_password.grid
(
row=
4
,
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
)
- address =
Entry(
Forms,
textvariable=
ADDRESS,
width=
30
)
- address.grid
(
row=
2
,
column=
1
)
- username =
Entry(
Forms,
textvariable=
USERNAME,
width=
30
)
- username.grid
(
row=
3
,
column=
1
)
- password =
Entry(
Forms,
textvariable=
PASSWORD,
show=
"*"
,
width=
30
)
- password.grid
(
row=
4
,
column=
1
)
- #==================================BUTTONS WIDGET=====================================
- btn_update =
Button(
Buttons,
width=
10
,
text=
"Update"
,
command=
Update,
state=
DISABLED)
- btn_update.pack
(
side=
LEFT)
- btn_exit =
Button(
Buttons,
width=
10
,
text=
"Exit"
,
command=
Exit)
- btn_exit.pack
(
side=
LEFT)
- #==================================LIST WIDGET========================================
- scrollbary =
Scrollbar(
Left,
orient=
VERTICAL)
- scrollbarx =
Scrollbar(
Left,
orient=
HORIZONTAL)
- tree =
ttk.Treeview
(
Left,
columns=
(
"MemID"
,
"Firstname"
,
"Lastname"
,
"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
(
'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=
0
)
- tree.column
(
'#2'
,
stretch=
NO,
minwidth=
0
,
width=
80
)
- tree.column
(
'#3'
,
stretch=
NO,
minwidth=
0
,
width=
120
)
- tree.column
(
'#4'
,
stretch=
NO,
minwidth=
0
,
width=
80
)
- tree.column
(
'#5'
,
stretch=
NO,
minwidth=
0
,
width=
150
)
- tree.column
(
'#6'
,
stretch=
NO,
minwidth=
0
,
width=
120
)
- tree.pack
(
)
- tree.bind
(
'<Double-Button-1>'
,
selectedRow)
Creating the Main Function
This is where the code that contains the main funcitions. This code will update SQLite data when the button is clicked. To do that just copy and write these blocks of code.
- #==================================METHODS============================================
- def
selectedRow(
event)
:
- global
mem_id;
- curItem =
tree.focus
(
)
- contents =
(
tree.item
(
curItem)
)
- selecteditem =
contents[
'values'
]
- mem_id =
selecteditem[
0
]
- FIRSTNAME.set
(
""
)
- LASTNAME.set
(
""
)
- ADDRESS.set
(
""
)
- USERNAME.set
(
""
)
- PASSWORD.set
(
""
)
- FIRSTNAME.set
(
selecteditem[
1
]
)
- LASTNAME.set
(
selecteditem[
2
]
)
- ADDRESS.set
(
selecteditem[
3
]
)
- USERNAME.set
(
selecteditem[
4
]
)
- PASSWORD.set
(
selecteditem[
4
]
)
- btn_update.config
(
state=
NORMAL)
- def
Update(
)
:
- connection.Database
(
)
- tree.delete
(
*tree.get_children
(
)
)
- connection.cursor
.execute
(
"UPDATE `member` SET `firstname` = ?, `lastname` = ?, `address` = ?, `username` = ?, `password` = ? WHERE `mem_id` = ?"
,
(
str
(
FIRSTNAME.get
(
)
)
,
str
(
LASTNAME.get
(
)
)
,
str
(
ADDRESS.get
(
)
)
,
str
(
USERNAME.get
(
)
)
,
str
(
PASSWORD.get
(
)
)
,
int
(
mem_id)
)
)
- connection.conn
.commit
(
)
- connection.cursor
.execute
(
"SELECT * FROM `member` ORDER BY `lastname` ASC"
)
- fetch =
connection.cursor
.fetchall
(
)
- for
data in
fetch:
- tree.insert
(
''
,
'end'
,
values=
(
data[
0
]
,
data[
1
]
,
data[
2
]
,
data[
3
]
,
data[
4
]
,
"****"
)
)
- connection.cursor
.close
(
)
- connection.conn
.close
(
)
- FIRSTNAME.set
(
""
)
- LASTNAME.set
(
""
)
- ADDRESS.set
(
""
)
- USERNAME.set
(
""
)
- PASSWORD.set
(
""
)
- btn_update.config
(
state=
DISABLED)
- def
displayData(
)
:
- tree.delete
(
*tree.get_children
(
)
)
- connection.Database
(
)
- connection.cursor
.execute
(
"SELECT * FROM `member` ORDER BY `lastname` ASC"
)
- fetch =
connection.cursor
.fetchall
(
)
- for
data in
fetch:
- tree.insert
(
''
,
'end'
,
values=
(
data[
0
]
,
data[
1
]
,
data[
2
]
,
data[
3
]
,
data[
4
]
,
'****'
)
)
- connection.cursor
.close
(
)
- connection.conn
.close
(
)
- def
Exit(
)
:
- result =
tkMessageBox.askquestion
(
'Python - Update SQLite Data'
,
'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 application is initialized properly. To do that copy the code below and paste it inside the IDLE text editor.
- #==================================INITIALIZATION=====================================
- displayData(
)
- if
__name__ ==
'__main__'
:
- root.mainloop
(
)
There you have it we just created a Update SQLite Data Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Download
You must upgrade your account or reply in the thread to view the hidden content.