10. Exercise [Tax Calculation] with MySQL
![]() |
10.1. Transferring a text file to a MySQL table
The following script will transfer data from the following text file:
12620:13190:15640:24740:31810:39970:48360:55790:92970:127860:151250:172040:195000:0
0:0.05:0.1:0.15:0.2:0.25:0.3:0.35:0.4:0.45:0.5:0.55:0.6:0.65
0:631:1290.5:272.5:3309.5:4900:6898.5:9316.5:12106:16754.5:23147.5:30710:39312:49062
in the [impots] table of the following MySQL database [dbimpots]:
![]() |
The connection to the [dbimpots] database will be made using the credentials (root,"").
We will use the following architecture:
![]() |
The [console] script we are going to write will use the [ImpotsFile] class to access the data in the text file. Write access to the database will be performed using the methods discussed previously.
The script code is as follows:
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
Notes:
- lines 106–110: we instantiate the [ImpotsFile] class presented in section 8.1;
- line 113: the arrays limites, coeffR, and coeffN are transferred to a MySQL database;
- line 8: the copyToMysql function performs this transfer. The copyToMysql function creates an array of queries to be executed and has them executed by the executerCommandes function, line 25;
- lines 28–89: the executerCommandes function is the one already presented earlier in section 9.7, with one difference: instead of being in a text file, the queries are in a list;
The text file impots.txt:
12620:13190:15640:24740:31810:39970:48360:55790:92970:127860:151250:172040:195000:0
0:0.05:0.1:0.15:0.2:0.25:0.3:0.35:0.4:0.45:0.5:0.55:0.6:0.65
0:631:1290.5:272.5:3309.5:4900:6898.5:9316.5:12106:16754.5:23147.5:30710:39312:49062
Screen results:
Verification with phpMyAdmin:
![]() |
10.2. The tax calculation program
Now that the data needed to calculate the tax is in a database, we can write the tax calculation script. We are again using a three-tier architecture:
![]() |
The new [dao] layer will be connected to the MySQL DBMS and will be implemented by the [ImpotsMySQL] class. It will provide the [business] layer with the same interface as before, consisting of the single getData method that returns the tuple (limits, coeffR, coeffN). Thus, the [business] layer will remain unchanged from the previous version.
10.3. The [ImpotsMySQL] class
The [dao] layer is now implemented by the following [ImpotsMySQL] class (impots.py file):
Notes:
- line 18: the SELECT SQL query that retrieves data from the MySQL database. The result rows from the SELECT are then processed one by one using [cursor.fetchone] (lines 22 and 32) to create the limits, coeffR, and coeffN arrays (lines 28–30);
- the getData method of the [dao] layer interface.
10.4. The console script
The console script code (impots_04) is as follows:
Notes:
- Line 23: instantiation of the [dao] and [business] layers;
- The rest of the code is familiar.
The same as with previous versions of the exercise.



