Search code examples
pythondatabasefunctionsqliteweb-applications

Python/sqlite3 function compare : TypeError: 'NoneType' object is not subscriptable


I'm new here can somebody help me please. I'm working on a components comparator webapp, the first page works normally but when I click on 'compare' ( that would launch the function and show the page) i just see the 'body' and not what the function would do. The server does not show an error message. When I try to launch manually the function in VsCode i got this error :

aprixProc1 = connexion.execute('SELECT prixProc FROM Processeur WHERE nomProc=(?);',(processeur1,)).fetchone()[0] 
TypeError: 'NoneType' object is not subscriptable

Here is my code:

import sqlite3
import cgi

formulaire = cgi.FieldStorage()
processeur1 = formulaire.getvalue('proc1')
processeur2 = formulaire.getvalue('proc2')

connexion = sqlite3.connect("composants.db")
connexion.execute("PRAGMA foreign_keys = ON")

#Recupere le prix de proc1 et proc2  
aprixProc1 = connexion.execute('SELECT prixProc FROM Processeur WHERE nomProc=(?);'(processeur1,)).fetchone()[0]
aprixProc2 = connexion.execute("SELECT prixProc FROM Processeur WHERE nomProc=(?);" (processeur2,)).fetchone()[0]


#Recupere le nb de coeurs de proc1 et proc2  
anbCoeurs1 = connexion.execute("SELECT coeursProc FROM Processeur WHERE nomProc=(?)", (processeur1,)).fetchone()[0]  
anbCoeurs2 = connexion.execute("SELECT coeursProc FROM Processeur WHERE nomProc=(?)",(processeur2,)).fetchone()[0]

#Recupere le nb de threads de proc1 et proc2  
athreadsProc1 = connexion.execute("SELECT threadsProc FROM Processeur WHERE nomProc=(?)",(processeur1,)).fetchone()[0]  
athreadsProc2 = connexion.execute("SELECT threadsProc FROM Processeur WHERE nomProc=(?)",(processeur2,)).fetchone()[0]  

#Recupere la taille de gravure de proc1 et proc2  
agravProc1 = connexion.execute("SELECT gravProc FROM Processeur WHERE nomProc=(?)",(processeur1,)).fetchone()[0]  
agravProc2 = connexion.execute("SELECT gravProc FROM Processeur WHERE nomProc=(?)",(processeur2,)).fetchone()[0]




pagedebut = '''
<html>
<head>

    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@600&display=swap" rel="stylesheet">
    <title> Components Stock Tracker </title>
    <link rel='stylesheet' href="css/fichier_css_html.css"/>
</head>

<body>
    <div class="entete">
        <p class='phead'>Components Stock Tracker</p>
        <img src ="images/logo_cst.jpg">
    </div>

    <div class="main">

'''

pagefin='''
</div>

    <div class="footer">
        <p class="texteFooter">
        Pied de Page <br> Crédits: Baptiste Lecerf </br>
        </p>
    </div>
</body>
</html> 
'''

def compareProc(prixProc1, prixProc2, nbCoeurs1, nbCoeurs2, threadsProc1, threadsProc2, gravProc1, gravProc2):    
reponse = ''  
if prixProc1 < prixProc2:  
    reponse += str(prixProc1)  
else:  
    reponse+= str(prixProc2)  
if nbCoeurs1 < nbCoeurs2:  
    reponse+= str(nbCoeurs2)  
else:  
    reponse+= str(nbCoeurs1)  
if threadsProc1 < threadsProc2:  
    reponse+= str(threadsProc2)  
else:  
    reponse+= str(threadsProc1)  
if gravProc1 < gravProc2:  
    reponse+= str(gravProc1)  
else:  
    reponse+= str(gravProc2)  

print(pagedebut+reponse+pagefin)

print(compareProc(aprixProc1, aprixProc2, anbCoeurs1, anbCoeurs2, athreadsProc1, athreadsProc2, agravProc1, agravProc2))  
connexion.close()

Solution

  • I tried this code and some other things but I always get ' [(0,)] '

    (with parenthesis around ? or without it's the same)

    import sqlite3
    import cgi
    
    formulaire = cgi.FieldStorage()
    #processeur1 = formulaire.getvalue('proc1')
    processeur1 = "R5-3600"
    #processeur2 = formulaire.getvalue('proc2')
    processeur2 = "I9-10900K"
    
    conn = sqlite3.connect("composants.db")
    cursor = conn.cursor()
    conn.execute("PRAGMA foreign_keys = ON")
    
    
    sql = "select exists(SELECT prixProc, coeursProc, threadsProc, gravProc FROM Processeur WHERE nomProc= (?) OR nomProc= (?))"
    args = (processeur1, processeur2)
    cursor = conn.execute(sql, args)
    results = cursor.fetchall()
    
    
    pagedebut ='''
    
    '''    
    
    pagefin ='''    
    
    '''
    
    print(results)