aboutsummaryrefslogtreecommitdiffstats
path: root/sources/pyside6/doc/tutorials/portingguide/chapter1/chapter1.rst
blob: 20b11065a30b621ce443eacb900f89b6b7c3eab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Chapter 1: ``initDb.h`` to ``createDb.py``
*******************************************

To begin with, port the C++ code that creates an SQLite
database and tables, and adds data to them. In this case,
all C++ code related to this lives in ``initdb.h``. The
code in this header file is divided into following parts:

* ``initDb`` - Creates a db and the necessary tables
* ``addBooks`` - Adds data to the **books** table.
* ``addAuthor`` - Adds data to the **authors** table.
* ``addGenre`` - Adds data to the **genres** table.

To start with, add these following ``import`` statements at
the beginning of ``createdb.py``:

.. literalinclude:: createdb.py
   :language: python
   :linenos:
   :lines: 40-44

The ``initDb`` function does most of the work needed to
set up the database, but it depends on the ``addAuthor``,
``addGenre``, and ``addBook`` helper functions to populate
the tables. Port these helper functions first. Here is how
the C++ and Python versions of these functions look like:

C++ version
------------

.. literalinclude:: initdb.h
   :language: c++
   :linenos:
   :lines: 55-81

Python version
---------------

.. literalinclude:: createdb.py
   :language: python
   :linenos:
   :lines: 44-65

Now that the helper functions are in place, port ``initDb``.
Here is how the C++ and Python versions of this function
looks like:

C++ version
------------

.. literalinclude:: initdb.h
   :language: c++
   :linenos:
   :lines: 81-159

Python version
---------------

.. literalinclude:: createdb.py
   :language: python
   :linenos:
   :lines: 65-

.. note:: The Python version uses the ``check`` function to
   execute the SQL statements instead of the ``if...else``
   block like in the C++ version. Although both are valid
   approaches, the earlier one produces code that looks
   cleaner and shorter.

Your Python code to set up the database is ready now. To
test it, add the following code to ``main.py`` and run it:

.. literalinclude:: main.py
   :language: python
   :linenos:
   :lines: 40-

Use the following command from the prompt to run:

.. code-block::

    python main.py

Your table will look like this:

.. image:: images/chapter1_books.png

Try modifying the SQL statment in ``main.py`` to get data
from the ``genres`` or ``authors`` table.