Matthieu Choplin
import sqlite3
connection = sqlite3.connect("test_database.db")
NB: the data created will be stored in the file test_database.db that is actually the database
We then need a cursor to execute commands on the database
import sqlite3
connection = sqlite3.connect("test_database.db")
cursor = connection.cursor()
# We create our first TABLE People that will
# store the field FirstName, LastName and Age
cursor.execute(
"CREATE TABLE People("
"FirstName TEXT, "
"LastName TEXT, "
"AGE INT)")
Imagine that the TABLE we have created is like a spreadsheet file ready to take data
It means that we can now insert data into this table with the "INSERT" command
cursor.execute("INSERT INTO People "
"VALUES ('Ron', 'Obvious', 42)")
# we have to commit to actually
# save the record in database
connection.commit()
When working with database, it is a good idea to use the with keyword to simplify your code, similar to how we used the with to open files
with sqlite3.connect("test_database.db") as connection:
# perform any SQL operation
Also, you will no longer need to use the commit() explicitly
Imagine that you want to concatenate a string with a SQL command
Do not do this:
first_name, last_name, age = 'John', 'Doe', 21
with sqlite3.connect("test_database.db") as connection:
cursor = connection.cursor()
cursor.execute(
"INSERT INTO People VALUES"
"('"+ first_name + "', '" + last_name + "', " + str(age) + ")")
The database is correctly updated, you can check that with the following command
with sqlite3.connect("test_database.db") as connection:
cursor = connection.cursor()
cursor.execute("SELECT * FROM People")
rows = cursor.fetchall()
print(rows)
Using the same method as 2 slides before, what happen if we try to add a user with the LastName "O'Connor"?
first_name, last_name, age = 'John', 'O\'Connor', 21
with sqlite3.connect("test_database.db") as connection:
cursor = connection.cursor()
cursor.execute(
"INSERT INTO People VALUES"
"('"+ first_name + "', '" + last_name + "', " + str(age) + ")")
We will get an error because the "'"
To avoid SQL injection, use the following instead:
first_name, last_name, age = 'John', 'O\'Connor', 21
with sqlite3.connect("test_database.db") as connection:
cursor = connection.cursor()
cursor.execute(
"INSERT INTO People VALUES"
"(?, ?, ?)", (first_name, last_name, age))
cursor.execute("SELECT * FROM People")
rows = cursor.fetchall()
print(rows)
The question marks act as a placeholder for the (first_name, last_name, age) tuple; this is called a parameterized statement. You should always used parameterized SQL statement
File used for the example: test_db.py
import pdb; pdb.set_trace()
To go further: https://pymotw.com/3/pdb/
import random
def sort_list(my_list):
my_list = my_list.sort()
return my_list
if __name__ == '__main__':
# create and shuffle a list
my_list = list(range(9))
random.shuffle(my_list)
# sort the list
my_list = sort_list(my_list)
print(my_list) # [0, 1, 2, 3, 4, 5, 6, 7, 8]
python3 -m venv /path/to/new/virtual/environment
pyvenv /path/to/new/virtual/environment
pip install virtualenv
virtualenv -p /path/to/python2.7 venv
source venv/bin/activate
pip install openpyxl
We have a function sum_two_numbers(a, b) that takes 2 numbers in arguments and returns their sum
# simple_function.py
def sum_two_numbers(a, b):
return a + b
We call it like this, and put the result in a variable:
sum_nb = sum_two_numbers(1, 2)
print(sum_nb)
How to test it automatically?
# test_simple_functions.py
import unittest
from simple_function import sum_two_numbers
class SimpleTestClass(unittest.TestCase):
def test_sum_two_numbers(self):
self.assertEqual(sum_two_numbers(1, 2), 3)
if __name__ == '__main__':
unittest.main()
Create a test for the sort_list() function of the 1st exercise
if __name__ == '__main__':
unittest.main()
Method called to prepare the test fixture. This is called immediately before calling the test method. The default implementation does nothing.
Method called immediately after the test method has been called and the result recorded. This is called even if the test method raised an exception. The default implementation does nothing.
NB: Pycharm can also help you write unittest: see here
You can use the module coverage
$ coverage run my_program.py arg1 arg2
$ coverage report -m
The best way to learn is by doing a python project
Look at the popular python modules and try to build something around them
You can also train yourself by doing some mathematical challenges on the Project Euler or the Python Challenge website.
You can host your project on Github or Bitbucket and learn the git command