5 Feb, 2012
in Python
by Mody
I have been playing with Python & Tkinter for couple of days, then thought of sqlite3 so I can do something useful with Python Tkinter & sqlite3, after this tutorial you will know how to create basic a Python program to store notes in sqlite3 database and retrieve them, This program should work on Windows, Linux and Mac.
the final program will look like this:

You will need:
Python installed
Sqlite3 Library
Sqlite comes with a Command Line Shell for creating and managing databases
Create an empty sqlite database
If you are on windows go to Start > run , type CMD ad the command line window will appear, make sure you have an empty folder created wherever you want to build the program, in my case I created a folder on desktop called “program”, if you did like me then in windows command line windows type
cd Desktop/program
At a shell or DOS prompt, enter
sqlite3 mydb
that creates an empty sqlite3 database, now lets create a table to store notes
create table mynotes(note varchar(50) );
the above line creates an empty table inside the database “mydb”
Python file
Create an empty file inside the same dir and name it prog.py
and paste the following inside it (don’t worry I will try to explain most of it):
from Tkinter import *
import sqlite3
class Program:
def __init__(self, master):
frame = Frame(master, width=80, height=50)
frame.pack()
self.text = Label(frame, text="")
self.text.pack()
self.text.grid(row=0, sticky=W, pady=10, padx=10)
self.TextField = Entry(frame, width=45)
self.TextField.pack()
self.TextField.grid(row=1, rowspan=2, sticky=W, pady=5, padx=10)
self.btn = Button(frame, text="Add note", command=self.add_note)
self.btn.pack()
self.btn.grid(row=3, rowspan=2, sticky=W, pady=10, padx=10)
self.showbtn = Button(frame, text="Show Notes", command = self.show_notes)
self.showbtn.pack()
self.showbtn.grid(row=3, rowspan=2, pady=10, padx=80)
# create an empty Tkinter listbox
self.content = Listbox(master, width=50)
self.content.pack()
def add_note(self):
# if texfield is empty
if self.TextField.get() == "":
# then set text label text
self.text["text"] = "Please type a note"
else:
# everything is ok lets get the typed note
item = self.TextField.get()
# create sqlite connection
conn = sqlite3.connect('mynotes')
c = conn.cursor()
# insert a row
c.execute("insert into notes (mynote) values (?)", (item,))
conn.commit()
# close connection
c.close()
# clear input
self.TextField.delete(0, END)
def show_notes(self):
conn = sqlite3.connect('mynotes')
c = conn.cursor()
# select all entries from database
list = c.execute("SELECT * FROM notes")
conn.commit()
# list has an array so lets loop the array and insert each item to
# our listbox
for row in list:
self.content.insert(END, row)
c.close()
root = Tk()
application = Program(root)
root.mainloop()
Lets explain the code above
def __init__(self, master):
frame = Frame(master, width=80, height=50)
frame.pack()
def __init__(self, master):
function for initialization.
frame = frame = Frame(master, width=80, height=50)
frame.pack()
Creates the main frame
self.text = Label(frame, text="")
self.text.pack()
self.text.grid(row=0, sticky=W, pady=10, padx=10)
it creates a label where we can put any text we want, we created a label to show a message if field left empty
row , sticky, pady, padx control the position of the label
self.TextField = Entry(frame, width=45)
self.TextField.pack()
self.TextField.grid(row=1, rowspan=2, sticky=W, pady=5, padx=10)
Creates a text field, you may note there is "frame" as first argument, that's the name of Tkinter frame we want the text field in
self.btn = Button(frame, text="Add note", command=self.add_note)
self.btn.pack()
self.btn.grid(row=3, rowspan=2, sticky=W, pady=10, padx=10)
Creates a button, text argument is the text displayed on the button, command is the function to run when you click that button
self.content = Listbox(master, width=50)
self.content.pack()
Creates an empty Tkinter listbox (thats where we will display sqlite3 contents)
item = self.TextField.get()
conn = sqlite3.connect('mynotes')
c = conn.cursor()
# insert a row
c.execute("insert into notes (mynote) values (?)", (item,))
conn.commit()
c.close()
# clear input
self.TextField.delete(0, END)
where item = self.TextField.get() we are getting the text typed into Textfield.
where self.TextField.delete(0, END) we are reseting the field.
sqlite3.connect('mynotes') creates a sqlite connection to mynotes (the database).