WEBVTT

1
00:00:00.260 --> 00:00:01.300 line:15% 
<v Jose>Hi, welcome back.</v>

2
00:00:01.300 --> 00:00:03.710 line:15% 
In this video, we're looking at using SQLite

3
00:00:03.710 --> 00:00:05.500 line:15% 
from within Python.

4
00:00:05.500 --> 00:00:07.170
And it's really straightforward.

5
00:00:07.170 --> 00:00:10.250
For Python comes with SQLite already installed

6
00:00:10.250 --> 00:00:14.340
as a package and all you have to do is import SQLite3.

7
00:00:16.250 --> 00:00:20.040
Open up a Python file, write import SQLite3,

8
00:00:20.040 --> 00:00:21.940
and run that file.

9
00:00:21.940 --> 00:00:24.850
If it all works and you don't get any errors,

10
00:00:24.850 --> 00:00:25.930
you're good to go.

11
00:00:25.930 --> 00:00:28.460
If it fails and you get some sort of margin not found error

12
00:00:28.460 --> 00:00:30.270
or an import error, it's possible

13
00:00:30.270 --> 00:00:33.710
that your Python installation has been made lighter

14
00:00:34.550 --> 00:00:36.120
so that it takes up less space

15
00:00:36.120 --> 00:00:38.010
in your computer and so forth.

16
00:00:38.010 --> 00:00:40.670
In that case I have to recommend that you find a way

17
00:00:40.670 --> 00:00:45.410
to instal SQLite3 or you reinstall Python 3.6 or later

18
00:00:46.280 --> 00:00:49.100
in your computer so that it comes with SQLite3.

19
00:00:49.100 --> 00:00:50.380
This normally will only happen

20
00:00:50.380 --> 00:00:52.610
on some very specific operating systems.

21
00:00:54.600 --> 00:00:57.590
Now once you're imported SQLite3,

22
00:00:57.590 --> 00:01:01.820
you can easily connect to a local database.

23
00:01:01.820 --> 00:01:05.140
That's how you do it - you do SQLite3.connect

24
00:01:05.140 --> 00:01:08.440
and then as a string, you give it the data file.

25
00:01:08.440 --> 00:01:10.240
Remember this is going to be single file letters

26
00:01:10.240 --> 00:01:14.530
going to contain all, your entire, SQLite database.

27
00:01:14.530 --> 00:01:17.100
The file is normally named data.db,

28
00:01:17.100 --> 00:01:18.040
that's just the convention.

29
00:01:18.040 --> 00:01:20.600
You can name it whatever you want, of course.

30
00:01:20.600 --> 00:01:23.430
And always after we finish with a connection

31
00:01:23.430 --> 00:01:25.400
remember to close the connection.

32
00:01:25.400 --> 00:01:27.750
Does it remind you of something?

33
00:01:27.750 --> 00:01:31.030
I hope it reminds you of files or it looks pretty similar.

34
00:01:32.090 --> 00:01:33.040
Okay.

35
00:01:33.040 --> 00:01:35.230
So after you've got the connection,

36
00:01:35.230 --> 00:01:38.630
you will want to get a cursor for that connection

37
00:01:38.630 --> 00:01:40.190
that's there in line three.

38
00:01:41.060 --> 00:01:43.410
You can see that cursor equals connection.cursor

39
00:01:43.410 --> 00:01:45.780
and that gives you a new cursor that you can use

40
00:01:45.780 --> 00:01:47.480
to execute SQL Querys.

41
00:01:48.340 --> 00:01:49.950
I'm going to explain exactly what a cursor is

42
00:01:49.950 --> 00:01:52.190
in the next video so don't worry.

43
00:01:52.190 --> 00:01:54.590
Once you've executed your SQL Query,

44
00:01:55.660 --> 00:01:58.530
you can commit the connection.

45
00:01:58.530 --> 00:02:00.600
And what that's going to do is it's going

46
00:02:00.600 --> 00:02:03.230
to save the contents of whatever changes you've made

47
00:02:03.230 --> 00:02:04.063
to the disc.

48
00:02:04.063 --> 00:02:06.720
I'm going to explain that in the next video as well.

49
00:02:06.720 --> 00:02:08.960
But this is a pretty normal flow

50
00:02:08.960 --> 00:02:12.981
for SQLite Querys being executed.

51
00:02:12.981 --> 00:02:16.410
You get your cursor, you run your Query with a cursor,

52
00:02:16.410 --> 00:02:19.170
and then you commit the connection and you close it after.

53
00:02:20.630 --> 00:02:22.790
So a few questions, of course.

54
00:02:22.790 --> 00:02:23.810
What is a cursor?

55
00:02:25.210 --> 00:02:26.043
What is commit?

56
00:02:27.180 --> 00:02:29.850
And how do we write SQL?

57
00:02:29.850 --> 00:02:31.520
We're going to look at these three questions

58
00:02:31.520 --> 00:02:32.640
in the next video.

59
00:02:32.640 --> 00:02:33.740
So I'll see you there.

