SQL query with variables in python -


I am creating a program that is a user interface for quizes fixed by teachers in a primary school. I'm trying to use the data typed by the user on the previous page. It is searching for people in the database that match the related user name and quiz number. This is because the teacher can see how well the students are doing on some poems.

This is my code.

  dbDatabase = sqlite3.connect ('c: \\ xampp \\ cgi-bin \\ Makingableable.db') cuDatabase = dbDatabase cursor () Fieldstorage = cgi.FieldStorage () # Quizno = Fieldstorage.getvalue ("quizno") typed on the webpage executable for UserID = Fieldstorage.getvalue ("username") # print (Quizno) # print (UserID) in cuDatabase (line) in CuDatabase (from "result" As a result, WHERE QuizID = '"+ + str (Quizno) +" "" and UserID =' "+ UserID +" ''): Print (line) DbDatabase.commit () cuDatabase.close ()  < / Pre> 

I'm getting the error message when I run my webpage:

  result from 40 Type 41 WHERE quizid = '"" + + (quisionone) + "" = "> = 42> and UserID =' '" + User ID + "43 43 for the line (in line): and undefined, user id =' hpa 1 'Operational Error: Near Hupa 1 ": Syntax Error Args = (' 'near Hupa 1' ':   

In addition to or code> and instead of AND . Code> so that the user has not asked this question whether it will show any quiz to the user or if a lot of people do a quiz, the teacher will see everyone, for example, Quiz 1?

You should use the SQL parameters:

  cuDatabase.execute ("" Resulting in WHERE QuizID =? And UserID =? "", (Quizno, UserID))   

? Placeholders will be replaced by your values, automatically cited to prevent SQL injection attacks (and operational errors). Quoted from

:

Usually your SQL operations will need to use values ​​from Python variables. You should gather your query by string operation of Python, Because doing this is unsafe; This makes your program weak for attack of a SQL injection (see a humorous example of what might be wrong).

Instead, use the parameter replacement of the DB-API ? Enter as a placeholder, wherever you want to use a value, and then provide a tupe of values ​​as the second argument of the cursor execute () method. (Other database modules can use a different placeholder, such as % s or : 1 .)

to ask a Use different queries for other queries, if no query results from this database; If you otherwise use or instead of and , then you will get both quiz results which other users have done, and This user has completed anything.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -