Python Forum
Error using mariadb select query with form in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error using mariadb select query with form in python?
#1
I want to use form with mariadb in python, i want to pass the id from form to mariadb select query to print the selected table row, this is index.html
<form action = "/cgi-bin/var.py" method = "get">
Row Id: <input type = "number" name = "idn"> <br />
<input type = "submit" value = "Submit" />
</form>

This is var.py inside the /srv/http/cgi-bin:
#!/usr/bin/python
import mariadb
import sys
import configparser
import cgi
import cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
mydb = mariadb.connect(
  host="localhost",
  user="user",
  password="",
  database="mail"
  )
mycursor = mydb.cursor()
query ="SELECT * FROM pop where id=%s"
val = form.getvalue('idn')
mycursor.execute(query, val)
myresult = mycursor.fetchall()
for row in myresult:
    print("id = ", row[0])
    print("Server = ", row[1])
    print("User = ", row[2])
    print("Password  = ", row[3], "\n")
    mycursor.close()
    mydb.close() 
After run the code get this error:
Server error!

The server encountered an internal error and was unable to complete your request.

Error message:
End of script output before headers: var.py

If you think this is a server error, please contact the webmaster.
Error 500
example.com
Apache/2.4.48 (Unix) mod_fcgid/2.3.9 mod_wsgi/4.8.0 Python/3.9 
This is the apache error log:
[Wed Jul 28 15:52:42.397653 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215: Traceback (most recent call last):: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397730 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:   File "/srv/http/cgi-bin/var.py", line 5, in <module>: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397747 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:     import cgi: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397776 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:   File "/srv/http/cgi-bin/cgi.py", line 8: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397826 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:     print "Content-Type: text/plain;charset=utf-8\\n\\n": /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397839 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215:           ^: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.397903 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] AH01215: SyntaxError: Missing parentheses in call to 'print'. Did you mean print("Content-Type: text/plain)?: /srv/http/cgi-bin/var.py, referer: http://example.com/test/index.html
[Wed Jul 28 15:52:42.403522 2021] [cgi:error] [pid 5430] [client 192.168.1.100:58034] End of script output before headers: var.py, referer: http://example.com/test/index.html
Reply
#2
There was another python code in the cgi-bin causing the error of "Missing parentheses" i remove that code from the cgi-bin now when run the index.html get the same error in the web browser but the error log now did not print any error messages.
Reply
#3
I made some changes to the var.py now when run the code get this stuff in the web browser:
4 --> -->
 
 
ProgrammingError	Python 3.9.6: /usr/bin/python
Thu Jul 29 16:51:21 2021

A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
 /srv/http/cgi-bin/var.py in <module>
     19 mycursor = mydb.cursor()
     20 query ="SELECT * FROM pop where id=%s"
=>   21 mycursor.execute(query, val)
     22 myresult = mycursor.fetchall()
     23 for row in myresult:
mycursor = <mariadb.connection.cursor object>, mycursor.execute = <built-in method execute of mariadb.connection.cursor object>, query = 'SELECT * FROM pop where id=%s', val = '4'

ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line 1
      args = ("You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1",)
      errmsg = "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1"
      errno = 1064
      msg = "You have an error in your SQL syntax; check the ...n for the right syntax to use near '%s' at line 1"
      sqlstate = '42000'
      with_traceback = <built-in method with_traceback of ProgrammingError object> 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,841 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  pyscript index error while calling input from html form pyscript_dude 2 974 May-21-2023, 08:17 AM
Last Post: snippsat
  list the files using query in python arjunaram 0 670 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Start Putty into Python Form Schlazen 5 5,463 Dec-13-2022, 06:28 AM
Last Post: divya130
  python sql query single quote in a string mg24 1 1,052 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  "SUMIF" type query in Python (help required) BlainEillimatta 0 851 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  MSSQL query not working in Python kat35601 0 913 Apr-12-2022, 06:44 PM
Last Post: kat35601
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,999 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  pymysql: insert query throws error wardancer84 12 4,562 Jan-28-2022, 06:48 AM
Last Post: wardancer84
  Query Syntax Error hammer 2 1,617 Jan-03-2022, 02:30 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020