-1

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()
7
  • That is not how you fetch results from DB, Have you read at least ONE tuto on how to do it ? sqlitetutorial.net/sqlite-python/sqlite-python-select Commented Apr 27, 2021 at 9:54
  • Does this answer your question? Python SQLite3 / Selecting rows from table Commented Apr 27, 2021 at 9:54
  • Also don't do 4 request for getting ONE attribut of the same row, retrieve in one request SELECT prixProc,coeursProc, threadsProc ,gravProc FROM Processeur WHERE nomProc=(?) Commented Apr 27, 2021 at 10:48
  • @azro yes but how i can compare each attribut after ? Commented Apr 27, 2021 at 20:01
  • No matter how many attributes, or how many rows you retrieve, all the data is in the result, just instead of having a list of 1 element, you have a list of multiple elements Commented Apr 28, 2021 at 6:37

2 Answers 2

0

You can fetch all values with one query, when the amount of value if not important, the less you fetch the DB the better it is.

  • When you fetched one attribut, for one row, you use used fetchone()[0] for : first row first field

  • Now you fetch multiple attribut, for multiples rows data came like this

    [[450, 4, 8, 16]
     [550, 8, 1, 17]] 
    
connexion = sqlite3.connect("composants.db")
cursor = connexion.cursor()
cursor.execute("PRAGMA foreign_keys = ON")

cursor.execute(
    'SELECT prixProc, coeursProc, threadsProc, gravProc FROM Processeur WHERE nomProc=? OR nomProc=?;',
    (processeur1, processeur2))
results = cursor.fetchall()

cursor.close()
connexion.close()

pagedebut = '''    '''
pagefin = '''    '''

def maxItem(values, index):
    return max(values, key=lambda x: x[index])[index]

def minItem(values, index):
    return min(values, key=lambda x: x[index])[index]

def compareProc(values):
    # min prixProc
    reponse = str(minItem(values, 0)) + " / "
    # max coeursProc
    reponse += str(maxItem(values, 1)) + " / "
    # max threadsProc
    reponse += str(maxItem(values, 2)) + " / "
    # min gravProc
    reponse += str(minItem(values, 3))

    return pagedebut + reponse + pagefin

print(compareProc(results))

Don't print(method()) if the method doesn't return anything, it'll print None which the default return value, either do

method()

# OR the method must 'return' something
print(method()) 

The following SQL query can be used to

'SELECT prixProc, coeursProc, threadsProc, gravProc FROM Processeur WHERE nomProc in (?, ?);'
Sign up to request clarification or add additional context in comments.

9 Comments

i try your code,add this before: "import sqlite3 import cgi formulaire = cgi.FieldStorage() processeur1 = formulaire.getvalue('proc1') processeur2 = formulaire.getvalue('proc2') " and it doesn't work anyway. PS: I'm a beginner
here is the error: testCompareProc.py", line 70, in <module> print(compareProc(results)) testCompareProc.py", line 60, in compareProc reponse = str(minItem(values, 0)) + " / " testCompareProc.py", line 56, in minItem return min(values, key=lambda x: x[index])[index] ValueError: min() arg is an empty sequence
maybe i should explain you more about my project, im working on a webapp and the main page is divided in 4 (for each components) and i used a php dropdown menu to show by exemple processors and the user select 2 then click on compare and launch this function. When you click it should show the same header and same footer, in the middle what the fonction should return but i just got header, footer and nothing in the middle even with your function
i tried to print(results) with processeur1 = "R5-3600" and processeur2 = "I9-10900K" which are items in my db but i get an empty list []
@VorTekS see stackoverflow.com/questions/45343175/… you may remove the parenthesis, I did not see it as I just copied your code, see my edit on the SQL query
|
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.