Welcome back.
Previously, we added this table with four columns to our interface.
Now we're going to add data here.
So by the end of this video, we're going to have some rows with actual data.
Let's begin.
Now, to add rows, we're going to use SQL.
So we're going to use an skew L library called a skew light three.
This is a standard library.
And this library has some functions which allow you to interact with the database.
Now, since these are functions of the SQLite library, then it makes sense that we call those functions
in a separate methods such as inside load data.
Instead of writing them in here, this could also be the case.
You could also do this if you like.
You can write those functions in here instead of in here.
But keeping those functions inside this method makes things more organized.
So that's what we're going to do now.
Go inside, load data and connect to a database.
Now I do have some data already in a database file, so please don't load the database file from the
lecture resources and then paste it in your project directory.
So this is what I'm doing.
Add to it?
Perhaps.
So that's my database, the DB file.
Just to quickly show you beforehand what this database has inside, I can use the DB browser for SQLite
program, which you can find online, download and install it, but you don't have to install this program.
You can just look here.
So if I go to browse data and I go to students table, so this database has a student table, right?
A database is made of one or more tables.
So this has an ID column name course and mobile.
So these are the data that we want to put on our pie sheet program.
These data are inside database DB.
Therefore, what we need to do is.
Create a connection variable.
And use the XQ three Connect method and provide the database DB file.
So make sure that database DB is located in the same directory with your main p file.
Right.
That allows you to simply provide the name of the DB file in here.
Then we want to extract the data out of that database.
To do that you need to refer to connection, the connection object.
So that's a specific escalate three object which contains an execute method.
And inside the execute methods you write an SQL query such as select all from students.
Students is the name of the table.
Inside that database, I mentioned that a database can have multiple tables.
So the table which have the data is named students.
The next, we're going to insert the results into the table.
But before we do that, let's check what result looks like in the terminal.
So run the program.
Expand the run tab and check if you got.
Anything printed out.
There's nothing in my case because that's because I haven't called the load methods yet.
So we defined it here, but we're not calling it anywhere.
So where should we call that method?
Well, there are two ways to do that.
You either call it insights here with cells that load data.
Or you can also call it somewhere in here after you have created the main window.
So age calculator, age calculator show, then you do age calculator that load data.
So let's try this version.
Either way, it's fine.
Let's rerun this.
And yeah, so that's the results variable, right?
It doesn't look anything useful for.
No, because it's a cursor object.
So you need to convert it into for example, into a list just like this and rerun.
And now we see some actual data.
So that's a list of tuples.
The first tuple represents the first row of the table.
So the student with ID won.
The name of the student is John Smith.
Math is the course The student has a role to have as a phone number of a student.
And of course, in the interface we will add more students.
To the table and these students will be added to the database as well.
Right.
So we can delete these students.
But for now, we have some initial data and these are those let's add those data to the table.
So remember the structure of this.
This is a list of tuples, right?
So let's go here, delete that print function and let's add the actual code which populates the table
with data.
This code will look like this.
Let me show you that to you graphically.
So what we'll do is we're going to iterate for this to every row.
So we're going to go to every row with the first full loop.
Then inside this full loop, we're going to have another for loop, a nested for loop for cell in row.
So basically the first loop will go to the first row.
So it's going to get the first row and the second for loop is going to go to each of the cells.
So it's going to iterate over the cells of the first row.
Then the main loop will go to the second row.
The second loop will go to the cells of the second row and so on, until we populate all the rows with
all the cells.
Yeah, let's do that.
Four.
Row number row data in enumerates results.
Enumerate.
Results.
So row number is zero one, two, three, four.
Depending on the row and row data is going to be the tuple with the data.
Right.
Such as one join math the number and then we say self that table.
So basically this that we're keeping here self the table that insert row.
Row.
Number.
So what we're doing here is we are inserting an empty row in a particular index in the table.
So we're starting with the row with index zero.
So we insert the first row, which has an index of zero, and then we say full column.
So that's the second loop.
Now number data in enumerate.
This time we have row data, this data rate.
So we're working on that tuple.
And we tried to insert that table into the first row.
So we do self dot, table, dot set item.
So insert row was used to insert a row, an empty row.
Now set item is used to populate cells of that row with actual data.
So set item at row number.
So we want to specify which row and which column using column number.
So we specify the coordinates of the cell in the table.
And then here comes a third argument, which is queue.
Table widget item.
This is a class we need to import.
So next to queue table widget, we import queue table, widget item.
And then we use parenthesis to call that class.
And inside we say string data.
And there's one last thing we need to perform before the loop.
So between the result variable and the for loop here in the same indentation level, we do solve the
table that set row count.
Zero.
This will make sure that whenever you load the program or do something, these data will not be added
on top of the existing data.
So you'll get duplicate data if you don't have this line.
So this sort of resets the table and loads the data as fresh.
And just before we execute this in the same indentation level with the main code, we say connection
that close to close the connection and then rerun the program.
So, yeah, we've got something here.
So these are the data.
And that's how it works.
So if you want to understand what's going on here, perhaps you want to insert some print functions
such as print road data.
And execute.
So see what you've got here.
It's raw data.
So these are the raw data that are being printed out.
So tuples.
So that's the code to add data to the table.
We could do a small improvement just before we close this video.
This index column here is unnecessary.
You notice the first column, which is part of the table, not part of the data.
We can disable that by going to the init method.
So you know that we set a header for the horizontal space.
So these were the column names.
But we can do the same by doing self that table dot vertical header.
You call that method.
So this has to be a small v lowercase v, So you call the method and after the parentheses you do set.
Visible and set it to false.
So that's who will hide that column.
So let's execute it again.
And there we go.
So now we don't have that duplicate index column there and the table is cleaner.
So there we go.
Next, we need to add some action to this so that we're able to add more data.
So how do you insert data into this table as a user from the interface?
Let's do that in the next videos.
I'll see you there.