• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jjgomera / pychemqt / 180e362a-f058-4ebe-9290-01fd4a8d53fc

17 Jul 2025 06:02PM UTC coverage: 73.449% (+0.01%) from 73.435%
180e362a-f058-4ebe-9290-01fd4a8d53fc

push

circleci

jjgomera
Disable copy element if no selected element in database

29315 of 39912 relevant lines covered (73.45%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.85
/lib/sql.py
1
#!/usr/bin/python3
2
# -*- coding: utf-8 -*-
3

4
'''Pychemqt, Chemical Engineering Process simulator
5
Copyright (C) 2009-2025, Juan José Gómez Romera <jjgomera@gmail.com>
6

7
This program is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11

12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16

17
You should have received a copy of the GNU General Public License
18
along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
Module with actions to manipulate compound database
21

22
    * :func:`transformElement`: Transform values from dialog in valid list to \
23
save in databank
24
    * :func:`inserElementsFromArray`: Insert element to a database
25
    * :func:`updateElement`: Update element with indice in database
26
    * :func:`deleteElement`: Delete Element with indice from custom Database
27
    * :func:`getElement`: Get element from database
28
    * :func:`copyElement`: Create a copy of element of indice in custom Database
29

30
API reference
31
-------------
32

33
'''
34

35

36
import os
1✔
37
import sqlite3
1✔
38

39

40
databank_name = os.path.join(os.environ["pychemqt"], 'dat', 'databank.db')
1✔
41
databank = sqlite3.connect(databank_name).cursor()
1✔
42
databank.execute("SELECT COUNT(*) AS Total FROM compuestos")
1✔
43
N_comp = databank.fetchone()[0]
1✔
44

45
conf_dir = os.path.join(os.path.expanduser('~'), ".pychemqt")
1✔
46
databank_Custom_name = conf_dir + os.sep + 'databank.db'
1✔
47
if os.path.isfile(databank_Custom_name):
1✔
48
    databank_Custom = sqlite3.connect(databank_Custom_name).cursor()
1✔
49
    databank_Custom.execute("SELECT COUNT(*) AS Total FROM compuestos")
1✔
50
    N_comp_Custom = databank_Custom.fetchone()[0]
1✔
51
else:
52
    N_comp_Custom = 0
×
53

54

55
def transformElement(elemento):
1✔
56
    """Transform list generated in dialog in valid list to save in database"""
57
    vals = []
×
58
    vals.append(str(elemento[0]))   # formula
×
59
    vals.append(str(elemento[1]))   # name
×
60
    vals.append(elemento[2])        # M
×
61
    vals.append(elemento[3])        # tc
×
62
    vals.append(elemento[4])        # pc
×
63
    vals.append(elemento[5])        # vc
×
64
    vals.append(elemento[6])        # API
×
65

66
    if elemento[7]:                 # Cp_ideal
×
67
        vals += elemento[7]
×
68
    else:
69
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
70

71
    if elemento[8]:                 # Antoine
×
72
        vals += elemento[8]
×
73
    else:
74
        vals += [0.0, 0.0, 0.0]
×
75

76
    if elemento[9]:                 # Henry
×
77
        vals += elemento[9]
×
78
    else:
79
        vals += [0.0, 0.0, 0.0, 0.0]
×
80

81
    if elemento[10]:                # Visco
×
82
        vals += elemento[10]
×
83
    else:
84
        vals += [0.0, 0.0]
×
85

86
    if elemento[11]:                # tension
×
87
        vals += elemento[11]
×
88
    else:
89
        vals += [0.0, 0.0]
×
90

91
    if elemento[12]:                # rhoS_DIPPR
×
92
        vals += elemento[12][0:8]
×
93
    else:
94
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
95

96
    if elemento[13]:                # rhoL_DIPPR
×
97
        vals += elemento[13][0:8]
×
98
    else:
99
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
100

101
    if elemento[14]:                # Pv_DIPPR
×
102
        vals += elemento[14][0:8]
×
103
    else:
104
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
105

106
    if elemento[15]:                # Hv_DIPPR
×
107
        vals += elemento[15][0:8]
×
108
    else:
109
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
110

111
    if elemento[16]:                # Cps_DIPPR
×
112
        vals += elemento[16][0:8]
×
113
    else:
114
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
115

116
    if elemento[17]:                # CpL_DIPPR
×
117
        vals += elemento[17][0:8]
×
118
    else:
119
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
120

121
    if elemento[18]:                # CpG_DIPPR
×
122
        vals += elemento[18][0:8]
×
123
    else:
124
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
125

126
    if elemento[19]:                # muL_DIPPR
×
127
        vals += elemento[19][0:8]
×
128
    else:
129
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
130

131
    if elemento[20]:                # muG_DIPPR
×
132
        vals += elemento[20][0:8]
×
133
    else:
134
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
135

136
    if elemento[21]:                # ThCondL_DIPPR
×
137
        vals += elemento[21][0:8]
×
138
    else:
139
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
140

141
    if elemento[22]:                # ThCondG_DIPPR
×
142
        vals += elemento[22][0:8]
×
143
    else:
144
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
145

146
    if elemento[23]:                # tension_DIPPR
×
147
        vals += elemento[23][0:8]
×
148
    else:
149
        vals += [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
×
150

151
    vals.append(elemento[24])       # Dipole
×
152
    vals.append(elemento[25])       # V_liq
×
153
    vals.append(elemento[26])       # Rackett
×
154
    vals.append(elemento[27])       # SG
×
155
    vals.append(elemento[28])       # f_acent
×
156
    vals.append(elemento[29])       # SolubilityParameter
×
157
    vals.append(elemento[30])       # watson
×
158

159
    # if elemento[31]:                # MSRK
160
        # vals += elemento[31][0:2]
161
    # else:
162
        # vals += [0.0, 0.0]
163

164
    vals.append(elemento[31])       # Stiehl
×
165
    vals.append(elemento[32])       # Tb
×
166
    vals.append(elemento[33])       # Tf
×
167
    vals.append(str(elemento[34]))  # CAS
×
168
    vals.append(str(elemento[35]))  # alternateFormula
×
169
    vals.append(elemento[36])       # UNIFAC
×
170
    vals.append(elemento[37])       # Dm
×
171
    vals.append(elemento[38])       # Eps_k
×
172
    vals.append(elemento[39])       # UNIQUAC_area
×
173
    vals.append(elemento[40])       # UNIQUAC_volumen
×
174
    vals.append(elemento[41])       # f_acent
×
175
    vals.append(elemento[42])       # Hf
×
176
    vals.append(elemento[43])       # Gf
×
177
    vals.append(elemento[44])       # volumen_wilson
×
178
    vals.append(elemento[45])       # NetHeating
×
179
    vals.append(elemento[46])       # GrossHeating
×
180
    vals.append(str(elemento[47]))  # Synonyms
×
181
    vals.append(elemento[48])       # volumen_caracteristico
×
182
    vals.append(elemento[49])       # calor_formacion_solido
×
183
    vals.append(elemento[50])       # energia_libre_solido
×
184
    vals.append(elemento[51])       # PolarParameter
×
185
    vals.append(str(elemento[52]))  # smile
×
186

187
    if elemento[53]:                # antoine_extended
×
188
        vals += elemento[53][0:4]
×
189
    else:
190
        vals += [0.0, 0.0, 0.0, 0.0]
×
191

192
    if elemento[54]:                # wagner
×
193
        vals += elemento[54][0:4]
×
194
    else:
195
        vals += [0.0, 0.0, 0.0, 0.0]
×
196

197
    return vals
×
198

199

200
def inserElementsFromArray(name, lista):
1✔
201
    """Insert element to a database
202
    lista: array with component data in text format"""
203
    conn = sqlite3.connect(name)
×
204
    curs = conn.cursor()
×
205
    curs.execute("SELECT COUNT(*) AS Total FROM compuestos")
×
206
    numero = curs.fetchone()[0]
×
207
    if name == databank_Custom_name:
×
208
        numero += 10000
×
209
    query = "INSERT INTO compuestos VALUES "
×
210
    for indice, elemento in enumerate(lista):
×
211
        vals = transformElement(elemento)
×
212
        vals.insert(0, numero+indice+1)
×
213
        curs.execute(query+str(tuple(vals)))
×
214
    conn.commit()
×
215
    conn.close()
×
216

217

218
def updateElement(elemento, indice):
1✔
219
    """Update element with indice in custom Database"""
220
    variables = [
×
221
        "formula", "name", "M", "tc", "pc", "vc", "API",
222
        "Cp_ideal_A", "Cp_ideal_B", "Cp_ideal_C", "Cp_ideal_D", "Cp_ideal_E",
223
        "Cp_ideal_F", "antoine_A", "antoine_B", "antoine_C", "henry_A",
224
        "henry_B", "henry_C", "henry_D", "visco_A", "visco_B", "tension_A",
225
        "tension_B", "rhoS_DIPPR_EQ", "rhoS_DIPPR_A", "rhoS_DIPPR_B",
226
        "rhoS_DIPPR_C", "rhoS_DIPPR_D", "rhoS_DIPPR_E", "rhoS_DIPPR_tmin",
227
        "rhoS_DIPPR_tmax", "rhoL_DIPPR_EQ", "rhoL_DIPPR_A", "rhoL_DIPPR_B",
228
        "rhoL_DIPPR_C", "rhoL_DIPPR_D", "rhoL_DIPPR_E", "rhoL_DIPPR_tmin",
229
        "rhoL_DIPPR_tmax", "Pv_DIPPR_EQ", "Pv_DIPPR_A", "Pv_DIPPR_B",
230
        "Pv_DIPPR_C", "Pv_DIPPR_D", "Pv_DIPPR_E", "Pv_DIPPR_tmin",
231
        "Pv_DIPPR_tmax", "Hv_DIPPR_EQ", "Hv_DIPPR_A", "Hv_DIPPR_B",
232
        "Hv_DIPPR_C", "Hv_DIPPR_D", "Hv_DIPPR_E", "Hv_DIPPR_tmin",
233
        "Hv_DIPPR_tmax", "CpS_DIPPR_EQ", "CpS_DIPPR_A", "CpS_DIPPR_B",
234
        "CpS_DIPPR_C", "CpS_DIPPR_D", "CpS_DIPPR_E", "CpS_DIPPR_tmin",
235
        "CpS_DIPPR_tmax", "CpL_DIPPR_EQ", "CpL_DIPPR_A", "CpL_DIPPR_B",
236
        "CpL_DIPPR_C", "CpL_DIPPR_D", "CpL_DIPPR_E", "CpL_DIPPR_tmin",
237
        "CpL_DIPPR_tmax", "CpG_DIPPR_EQ", "CpG_DIPPR_A", "CpG_DIPPR_B",
238
        "CpG_DIPPR_C", "CpG_DIPPR_D", "CpG_DIPPR_E", "CpG_DIPPR_tmin",
239
        "CpG_DIPPR_tmax", "muL_DIPPR_EQ", "muL_DIPPR_A", "muL_DIPPR_B",
240
        "muL_DIPPR_C", "muL_DIPPR_D", "muL_DIPPR_E", "muL_DIPPR_tmin",
241
        "muL_DIPPR_tmax", "muG_DIPPR_EQ", "muG_DIPPR_A", "muG_DIPPR_B",
242
        "muG_DIPPR_C", "muG_DIPPR_D", "muG_DIPPR_E", "muG_DIPPR_tmin",
243
        "muG_DIPPR_tmax", "ThcondL_DIPPR_EQ", "ThcondL_DIPPR_A",
244
        "ThcondL_DIPPR_B", "ThcondL_DIPPR_C", "ThcondL_DIPPR_D",
245
        "ThcondL_DIPPR_E", "ThcondL_DIPPR_tmin", "ThcondL_DIPPR_tmax",
246
        "ThcondG_DIPPR_EQ", "ThcondG_DIPPR_A", "ThcondG_DIPPR_B",
247
        "ThcondG_DIPPR_C", "ThcondG_DIPPR_D", "ThcondG_DIPPR_E",
248
        "ThcondG_DIPPR_tmin", "ThcondG_DIPPR_tmax", "tension_DIPPR_EQ",
249
        "tension_DIPPR_A", "tension_DIPPR_B", "tension_DIPPR_C",
250
        "tension_DIPPR_D", "tension_DIPPR_E", "tension_DIPPR_tmin",
251
        "tension_DIPPR_tmax", "dipole", "V_liq",
252
        "Rackett", "SG", "f_acent",
253
        "SolubilityParameter", "watson", "Stiehl",
254
        "Tb", "Tf", "CAS", "alternateFormula", "UNIFAC",
255
        "Dm", "Eps_k", "UNIQUAC_area", "UNIQUAC_volumen",
256
        "f_acent_MSRK", "Hf",
257
        "Gf", "volumen_wilson", "NetHeating",
258
        "GrossHeating", "Synonyms",
259
        "volumen_caracteristico", "calor_formacion_solido",
260
        "energia_libre_solido", "PolarParameter", "smile", "antoine_to",
261
        "antoine_n", "antoine_E", "antoine_F", "wagner_a", "wagner_b",
262
        "wagner_c", "wagner_d"]
263
    vals = transformElement(elemento)
×
264

265
    conn = sqlite3.connect(databank_Custom_name)
×
266
    curs = conn.cursor()
×
267
    for variable, valor in zip(variables, vals):
×
268
        if isinstance(valor, int):
×
269
            curs.execute(
×
270
                f'UPDATE compuestos SET {variable}={valor} WHERE id=={indice}')
271
        elif isinstance(valor, float):
×
272
            curs.execute(
×
273
                f'UPDATE compuestos SET {variable}={valor} WHERE id=={indice}')
274
        elif isinstance(valor, str):
×
275
            valor = '"'+valor+'"'
×
276
            curs.execute(
×
277
                f'UPDATE compuestos SET {variable}={valor} WHERE id=={indice}')
278
    conn.commit()
×
279
    conn.close()
×
280

281

282
def deleteElement(indice):
1✔
283
    """Delete Element with indice from custom Database"""
284
    conn = sqlite3.connect(databank_Custom_name)
×
285
    curs = conn.cursor()
×
286
    curs.execute(f"DELETE FROM compuestos WHERE id={indice}")
×
287
    conn.commit()
×
288
    conn.close()
×
289

290

291
def getElement(indice):
1✔
292
    """Get element from database
293
    indice: index in databank of element"""
294
    if indice > 10000:
1✔
295
        db_Custom = sqlite3.connect(databank_Custom_name).cursor()
×
296
        db_Custom.execute(f"select * from compuestos where id=={indice}")
×
297
        componente = db_Custom.fetchone()
×
298
    else:
299
        db = sqlite3.connect(databank_name).cursor()
1✔
300
        db.execute(f"select * from compuestos where id=={indice}")
1✔
301
        componente = db.fetchone()
1✔
302

303
    # Change none values for 0s
304
    componente = list(componente)
1✔
305
    while None in componente:
1✔
306
        componente[componente.index(None)] = 0
1✔
307

308
    return tuple(componente)
1✔
309

310

311
def copyElement(indice):
1✔
312
    """Create a copy of element of indice in custom Database"""
313
    elemento = getElement(indice)
×
314
    vals = elemento[1:]
×
315
    conn = sqlite3.connect(databank_Custom_name)
×
316
    curs = conn.cursor()
×
317
    curs.execute("INSERT INTO compuestos VALUES"
×
318
                 + str((10001+N_comp_Custom, ) + vals))
319
    conn.commit()
×
320
    conn.close()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc